Epsilon is not the smallest quantity that can be represented. Its the smallest amount that can be added to a number between 1 and 2. If its added to 2 the result is 2. We need to add 2 * Number.EPSILON to 2 to add anything. 4 * E to 4, 8 * E to 8, 16 * E to 16 etc.
That kind of equality test should be:
abs( a-b ) < Number.EPSILON * max( abs(a), abs(b) )
In practice I judge the scale of the quantization noise that may accrue and compare the difference to it.It may also be possible to kind of caste precision away by adding a trick value. eg.
a + t == b + t
If a and b are positive, that is somewhat similar to: abs( a-b ) < Number.EPSILON * t
Re:Adding BigInts to V8
by partycoder on Wednesday May 02, 2018 @11:03PM
(#16983058)
Thanks for pointing it out.
A more popular definition is "the smallest number that yields a result different to 1 when added to 1".
Look out - I just now tested those two extra ideas which I added for equality testing and found they dont work.
This version of adding a rounding value seems to though:
( a-b + rounder ) == rounder
My apologies, be careful out there :)
For example, this is an incorrect way to compare numbers:
This is the proper way to compare numbers: This means the difference is smaller than the smallest quantity that can be represented in floating point number, and every JS number is a floating point number.Not knowing about this can cause many issues if you deal with values representing currency.