Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are 40-bit integers represented exactly in JavaScript?

I'm using a server-side calculation which needs to generate (with * and + operations) and compare 40-bit integers. I'm aware that at that point the V8 engine stores the numbers as Double rather than int. Can I rely on these numbers to be generated and compared correctly?

My intuition says yes - doubles shouldn't have trouble with that - but I'm not sure how to check or where to find information on this.

like image 262
configurator Avatar asked Nov 15 '12 00:11

configurator


People also ask

How many bits is integer in JS?

Description. The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991, or ~9 quadrillion). Double precision floating point format only has 52 bits to represent the mantissa, so it can only safely represent integers between -(253 – 1) and 253 – 1.

How many bytes is a number in JavaScript?

Number type. The Number type is a double-precision 64-bit binary format IEEE 754 value.

What is the largest number that can be stored in JavaScript?

JavaScript Number MAX_VALUE MAX_VALUE returns the largest number possible in JavaScript. Number. MAX_VALUE has the value of 1.7976931348623157e+308.

How many bits is BigInt in JavaScript?

The BigInt. asUintN() method can be useful to stay in the range of 64-bit arithmetic.


1 Answers

Yes.

A JavaScript Number, which is a 64-bit IEEE 754 floating point value, can store integers from -253 to 253 without loss of precision, since doubles can store up to 53 bits of mantissa (52 explictly).

References:

  • ECMA-262: 4.3.19 Number value

  • Double-precision floating point numbers (Wikipedia)

like image 197
NullUserException Avatar answered Sep 28 '22 01:09

NullUserException