Why is this number increasing by one?

console.log(10209761399365907); 

Why does this code output a number more by one (10209761399365908 instead of 10209761399365907)?

This only happens for this particular number. For example, with 10155071933693662, I get the correct value (10155071933693662).

Is there anything I need to know about this particular number? The only workaround I found out was to pass the value as a string.

+8
javascript numbers
source share
1 answer

Your numeric value is too large to be represented exactly as a 64-bit floating point number. The value you get is an estimate. JavaScript initially has only 64-bit floating point numbers; values ​​that fit into long in C or C ++ may be too large to be exactly representable, because some of the 64 bits are used for the exponent in the floating point representation. (The trade-off is that with a floating point, you can work with approximations for values ​​with a much larger or smaller numerical value than is possible with a simple integer representation.)

If it is not important to manipulate the value as a number, make it a string:

 console.log("10209761399365907"); 

or, as I suggested for the question, since it originally appeared:

 <input type="button" value="Invita" onclick="Invite('@us.FacebookID')" /> 

where this "FacebookID" was the big number in question.

There, the constant (in new JavaScript environments) is called Number.MAX_SAFE_INTEGER . This indicates how large the figure is that you can represent in such a way that the value is accurately represented; that is, the 64-bit floating point mantissa will contain explicit bits for the whole value.

Larger even numbers can be represented exactly, depending on how many zero bits are on the lower end of their binary representation. Thus, 1152921504606846800 can be accurately represented because it received 7 bits of all zeros at the lower end of the binary representation. However, 1152921504606846801 is output as 1152921504606846800 , because this low-order bit 1 nowhere to go. (Unrepresented bits are considered zero.)

Addendum: Here is the (almost) original version of the question posed to explain a possible confusing code example above:


I have a problem with Javascript, which is rather strange. I have some identifiers coming from my server, and when I click the button, the parameter is passed to the method to execute the request.

This is the code:

 <script type="text/javascript"> function Invite(id) { alert(id); } </script> 

On the other hand, I have:

(CSHTML syntax)

  <input type="button" value="Invita" onclick="Invite(@us.FacebookID)" /> 

It has become:

 <input type="button" value="Invita" onclick="Invite(10209761399365907)"> 

But when I press the button ... the method number receives the parameter ZOOMED ONE!

Alert from CODE

And this happens ONLY for one specific identifier! For example, with 10155071933693662 I get the correct value (10155071933693662)

Is there anything I need to know about this particular number? All this is really strange, and the only workaround I found out was to pass the value as a string!

+18
source share

All Articles