robust-in-sphere
Version:
Exact arithmetic test to check if point is contained in sphere
62 lines (51 loc) • 1.01 kB
JavaScript
"use strict"
var inSphere = require("../in-sphere.js")
var twoProduct = require("two-product")
function sgn(x) {
if(x < 0) {
return -1
}
if(x > 0) {
return 1
}
return 0
}
require("tape")(function(t) {
t.equals(sgn(inSphere([0], [1], [0.5])), -1)
t.equals(sgn(inSphere([0], [1], [1.5])), 1)
t.equals(sgn(inSphere(
[],
[],
[],
[-0.5,0])), 1)
t.equals(sgn(inSphere(
[],
[],
[],
[-1,0])), 0)
t.equals(sgn(inSphere(
[],
[],
[],
[-1.5,0])), -1 )
var x = 1e-64
for(var i=0; i<128; ++i) {
t.equals(sgn(inSphere(
[],
[-x,-x],
[],
[])), 1, "sphere test:" + x)
t.equals(sgn(inSphere(
[],
[-x,-x],
[],
[])), -1, "sphere test:" + x)
t.equals(sgn(inSphere(
[],
[-x,-x],
[],
[])), 0, "sphere test:" + x)
x *= 10
}
t.end()
})