1

I have not been able to find a straight answer on this. I've check the spec but don't see anything that defines the precision.

Number.MAX_VALUE
    1.7976931348623157e+308
a = 9007199254740992
a == a-1
    false
a+1
    9007199254740992
a+2
    9007199254740994
a*a
    8.112963841460668e+31   
a*a == ((a*a)-1)
    true
a*a == ((a*a)*a)
    false
ralphinator80
  • 641
  • 5
  • 17
  • What level of precision and with which operators? Technically I'd say that `0` is the greatest value you'd be able to use precisely, given that it'll work accurately with every operator. – zzzzBov Aug 09 '12 at 21:54

1 Answers1

2
Math.pow( 2, 53 )

is the largest integer. After that you start to lose precision for integers.

You can find it here:

Significand precision: 53 bits (52 explicitly stored)

As for "decimals", you can never rely on getting accurate results regardless of how small or big they are.

0.1 + 0.2 === 0.30000000000000004

Btw, if you are looking a work-around for the above:

function round(num) {
     return Math.round( num * 1e9 ) / 1e9;
}

round(0.1 + 0.2) === 0.3
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Some old versions of IE may not follow the spec. In fact they may predate it (tasty!) http://stackoverflow.com/questions/5401844/number-precision-in-javascript – John Watts Aug 09 '12 at 22:08
  • @JohnWatts It's a shame nobody asked what the result was for `===`, what IE version he was using, or if he was even telling the truth.. ([=== would have been worth a shot](http://stackoverflow.com/questions/4850978/ie-bug-window-top-false)). Anyways, can't reproduce in IE7 and can't go any lower right now. – Esailija Aug 09 '12 at 22:13