Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You may also be interested in learning about Number.EPSILON (the floating point epsilon) which is required when dealing with number equality...

For example, this is an incorrect way to compare numbers:

    > 0.1 + 0.2 === 0.3
    false
This is the proper way to compare numbers:

    a = 0.1 + 0.2
    b = 0.3
    > Math.abs(a - b) < Number.EPSILON
    true
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.



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


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 :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: