Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JavaScript support 64-bit integers?

I have the following code:

var str = "0x4000000000000000";   //4611686018427387904 decimal var val = parseInt(str); alert(val); 

I get this value: "4611686018427388000", which is 0x4000000000000060

I was wondering if JavaScript is mishandling 64-bit integers or am I doing something wrong?

like image 736
ahmd0 Avatar asked Mar 10 '12 03:03

ahmd0


People also ask

Is JavaScript 64bit?

JavaScript represents numbers using IEEE-754 double-precision (64 bit) format.

How many bits are JavaScript integers?

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#.

Does Java have 64-bit integers?

In Java, the long data type stores integer on 64 bit while the integer data type stores integer on 32bit.

Does JSON support 64-bit integers?

> JSON doesn't have 64-bit integers. The JSON "number" type has no limitations on the range. All 64-bit integers, both signed and unsigned, can be encoded as JSON numbers.


1 Answers

JavaScript represents numbers using IEEE-754 double-precision (64 bit) format. As I understand it this gives you 53 bits precision, or fifteen to sixteen decimal digits. Your number has more digits than JavaScript can cope with, so you end up with an approximation.

This isn't really "mishandling" as such, but obviously it isn't very helpful if you need full precision on large numbers. There are a few JS libraries around that can handle larger numbers, e.g., BigNumber and Int64.

like image 199
nnnnnn Avatar answered Sep 20 '22 14:09

nnnnnn