0

look at this picture.(The underLine is input.)

error example

why the end of JavaScript Number with trailing zeros or a not predictable number?

I checked the document with https://www.ecma-international.org/ecma-262/5.1/#sec-9.7

But I can't find anything useful for this problem.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I think you can find an answer here: https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin#307200 – categulario Sep 23 '18 at 15:55
  • Those numbers are bigger than `Number.MAX_SAFE_INTEGER` hence why you get problems. – VLAZ Sep 23 '18 at 15:57
  • All JavaScript numbers are IEEE double precision floating point numbers. As such they have a maximum precision of 53 bits. Exceed this precision and you get gaps in the integer number line. [What every programmer should know about floating point numbers](https://floating-point-gui.de/formats/fp/)) – spender Sep 23 '18 at 16:01
  • I know why the problem is happen, and i want to know if the end of the number have regular? – blueblueskyhua Sep 23 '18 at 16:05
  • I believe confusion is just about console tries to display number as integer value even after value is actually stored as float and precision has been lost. So if you saw "1.1111111111111111e+16" you would know it has been converted into float, right? – skyboyer Sep 23 '18 at 16:05
  • yeah, if i saw "1.1111111111111111e+16" it is no problem. But my confusion is why the numbers at the end are unpredictable?Maybe there are rules? – blueblueskyhua Sep 23 '18 at 16:09

2 Answers2

1

Numbers in Javascript use Double-precision floating-point format which can represent numbers from -(2^53 - 1) and (2^53 - 1). This limits the maximum safe number (Number.MAX_SAFE_INTEGER) to 9007199254740991.

Hence any number above that will be not be represented accurately.

Roy
  • 471
  • 5
  • 15
0

so the thing is there is maximum integer which can be safely manipulated in javascript after that you are supposed to get some unexpected results based on implementation

read about that max safe integer https://www.ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer

BTW there is a new bigint type which can handle large numbers https://developers.google.com/web/updates/2018/05/bigint

bigint however is not a standard yet i think

ashish singh
  • 6,526
  • 2
  • 15
  • 35