1

I have tried JavaScript Number method and it returns unexpected results.

If i pass 192000000000000005 value into Number() method. It returns 192000000000000000, without 5.

If I Pass 19200000000000005 value, it returns 19200000000000004, which is unexpected.

What is that meaning of those results? You can find the below screenshot too.

Chrome Console:

enter image description here

Vivek Athalye
  • 2,974
  • 2
  • 23
  • 32
Shanmugaraja_K
  • 1,914
  • 3
  • 13
  • 25
  • 1
    see `Number.MAX_SAFE_INTEGER` - that number is 21 times larger than the maximum "safe" integer representable by floating point – Jaromanda X Feb 26 '18 at 04:07
  • 1
    JavaScript numbers are ***always*** 64-bit floating bit numbers. – Elliott Frisch Feb 26 '18 at 04:07
  • If I pass 19 digit number it returns as 18 digit number? – Shanmugaraja_K Feb 26 '18 at 04:08
  • The numbers you're trying to make are too big to be represented exactly. – Pointy Feb 26 '18 at 04:11
  • @Raja - 192,000,000,000,000,005 is **18** digits ... what 19 digit number are you "passing" ? – Jaromanda X Feb 26 '18 at 04:12
  • @JaromandaX I need to pass this 192000000000000005, and if i pass this to Number() I didn't lose the last integer 5. – Shanmugaraja_K Feb 26 '18 at 04:14
  • `I didn't lose the last integer 5` - actually you **did** lose it ... yes, but it's **the same number of digits** ... you just get a number that is 5 less than the number you "passed in", i.e. the **5** becomes a **0** - due to the reasons explained above - count the digits ... you'll see you are wrong - and still, no 19 digit number – Jaromanda X Feb 26 '18 at 04:15
  • 2
    Let me save you the hassle of asking more about this ... `192000000000000005` can not be represented by a `Number` in javascript, due to the limitations of 64bit floating point representation ... the Maximum integer that is safe (i.e. all integers up to that integer can be represented without gaps) is Number.MAX_SAFE_INTEGER === 9,007,199,254,740,991 - you can see that's 16 digits - so 18 digits is not safe, and 19 (whatever, you've never tried) won't work either - note 9007199254740991 is `2**53 - 1` - 64bit floating point has a 53 bit mantissa – Jaromanda X Feb 26 '18 at 04:20
  • @JaromandaX well written comment. Please put it as an answer because it indeed answers the question `What is that meaning of those results?` :) – Vivek Athalye Feb 26 '18 at 04:25
  • @JaromandaX is there any MSDN or Mozilla docs link that explains the same? – Shanmugaraja_K Feb 27 '18 at 05:37
  • I wouldn't trust MSDN - probably is on Mozilla – Jaromanda X Feb 27 '18 at 07:15

1 Answers1

2

192000000000000005 can not be represented by a Number in javascript, due to the limitations of 64bit floating point representation

The Maximum integer that is safe (i.e. all integers up to that integer can be represented without gaps) is Number.MAX_SAFE_INTEGER === 9,007,199,254,740,991

you can see that's 16 digits - so 18 digits is not safe, and 19 (whatever, you've never tried) won't work either -

Note, the actual definition of MAX_SAFE_INTEGER is

the largest integer n, where n and n + 1 are both exactly representable as a Number value

console.log(9007199254740991) //=> 9007199254740991  
console.log(9007199254740992) //=> 9007199254740992  
console.log(9007199254740993) //=> 9007199254740992  

So, while 9007199254740992 is representable as a Number, because 9007199254740993 also has the same value when stored in a 64bit float, it is not "safe"

note 9007199254740991 is 2**53 - 1 - which makes sense when you know that 64bit floating point has a 53 bit mantissa

Mozilla Developer Network Documentation

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87