It is said on Wikipedia that the representable numbers between 2⁵³ ~ 2⁵⁴ are all even integers.
If we let N = Number.MAX_SAFE_INTEGER (which is 2⁵³ - 1 = 9007199254740991), and calculate the following numbers: N+2, N+2.0000000000000001, N+2.000000000000001, I would expect that they are all the same, but I was wrong:
N + 2 === 9007199254740992
N + 2.0000000000000001 === 9007199254740992
N + 2.000000000000001 === 9007199254740994
Does anyone know why?
The first number, has a sub-decimal point component requiring 2^-56 exponent :
<<< '2.0000000000000001' gawk -v PREC=2000000 -nMbe '{
printf("\n\t0x%.16A\n\n",($0)%1) }'
0x 7.34ACA5F6226F0ADAp-56
the final 1 at the tail will be lost even during parsing, which means you're only asking it to handle
[ 4^3^3/2 - 1 ] (ps:: this is my favorite
way to express 2^53-1)
+ 2
which I'm guessing double-precision floating point logic defaults to not rounding it up. But conversely, the 2nd example you've provided has a sub-decimal component that is
0x 4.80EBE7B9D58566C8 p-52
i.e.
0.00000000000000 10000000000
0.000000000000000 2220446049 (~2^-52)
Combining both, what the floating point system sees is
0x 2.000000000000 0 p+0 <~~~ 1st one
0x 2.000000000000 5 p+0 <~~~ 2nd one
/
This 5 made all the difference
now the mantissa has something to use to round things up.