Reputation: 101
I was thinking that equality in ES6 is closed case, like this basic example:
x = 0.2;
y = 0.3;
z = 0.1;
equal = (Math.abs(x - (y - z)) < Number.EPSILON); // true
console.log(equal)
But it does not work in this(multiplying) case:
x = 2.3;
y = 23;
z = 0.1;
equal = (Math.abs(x - (y * z))) < Number.EPSILON; // false
console.log(equal)
Am I thinking wrong? Is it only designed for plus and minus operations? How can I safely fix it (it is true when you multiple epsilon by anything bigger than 2)?
Upvotes: 1
Views: 362
Reputation: 5543
Try printing the value of Math.abs(x - (y * z))
and EPSILON
:
x = 2.3;
y = 23;
z = 0.1;
equal = (Math.abs(x - (y * z))) < Number.EPSILON; // false
console.log(equal)
console.log(Math.abs(x - (y * z)))
console.log(Number.EPSILON)
console.log(Math.abs(x - (y * z)) == 2*Number.EPSILON)
To avoid this you should be checking the order of the difference , and compare it to EPSILON
, instead of the comparing the actual value :
x = 2.3;
y = 23;
z = 0.1;
equal = (Math.abs(x - (y * z)))/ Number.EPSILON < 10;
console.log(equal)
Upvotes: 1