Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this number get increased by one? [duplicate]

console.log(10209761399365907);

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

This is happening only for this specific number. For instance with 10155071933693662 I get the correct value (10155071933693662).

Is there something I need to know about that specific number? The only workaround I figured out is to pass the value as a string.

like image 242
Ziba Leah Avatar asked Nov 29 '16 13:11

Ziba Leah


1 Answers

Your numeric value is too large to be represented exactly as a 64-bit floating point number. The value you get is an approximation. JavaScript natively only has 64-bit floating point numbers; values that do fit in a long in C or C++ may be too large to be represented exactly because some of the 64 bits are used for an exponent in floating point representation. (The tradeoff is that with a floating point it's possible to work with approximations for values with much larger or smaller numeric magnitude than is possible with simple integer representation.)

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

console.log("10209761399365907");

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

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

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

There's a constant (in newer JavaScript environments) called Number.MAX_SAFE_INTEGER. That tells you how big a number you can represent such that the value will be represented exactly; that is, that the 64-bit floating point mantissa will contain explicit bits for the entire value.

Larger even integers can be represented exactly, depending on how many zero bits are at the low end of their binary representation. Thus, 1152921504606846800 can be exactly represented because it's got 7 bits of all zeros at the low end of the binary representation. However, 1152921504606846801 comes out as 1152921504606846800 because that low 1 bit has nowhere to go. (Unrepresented bits are assumed to be zero.)

Addendum: Here's the (almost) original version of the question as posed, to explain the possibly confusing example code above:


I'm facing a problem with Javascript that is quite weird. I have some ids coming from my server and when I press a button the parameter is passed to a method to perform a 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)" />

That became:

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

But when I click the button... the number the method receives the parameter INCREASED by ONE!

Alert from CODE

And this is happening ONLY for one specific ID! For instance, with 10155071933693662 I get the correct value (10155071933693662)

Is there something I need to know about that specific number? All this is really strange and the only workaround I figured out is to pass the value as a string!

like image 163
Pointy Avatar answered Nov 13 '22 04:11

Pointy