lostbits
lostbits

Reputation: 1054

What is the integer format in memory?

I just started reading Javascript (Professional JavaScript for Web Developers, 3rd Edition) and the text variously says that integers are:

[1] Stored as IEEE-754 format (pg. 35).
[2] Have a -0 value (pg. 36).
[3] Stored as a 32-bit two's complement number (pg. 50).
[4] Stored as a IEEE-754 64-bits number (pg. 49).

These are inconsistent definitions. What is the format of an integer in Javascipt?

thanks

Upvotes: 0

Views: 130

Answers (2)

Ixrec
Ixrec

Reputation: 976

IEEE-754 floats do have a -0 value, so there's no inconsistency between 1, 2 and 4. Only 3 appears to be inconsistent.

Javascript numbers are always stored as IEEE-754 64-bit floating point numbers. In some expressions (usually with bitwise operators) they may be temporarily converted to 32-bit integers and then converted back to 64-bit floats, but they're always stored as floats. I assume this conversion is what #3 is actually referring to.

A JIT compiler for Javascript may use actual integers in the compiled code as an optimization (especially if asm.js is involved), but during interpretation it's still all floats.

Upvotes: 1

user3715654
user3715654

Reputation: 41

Here is the specification:

http://ecma262-5.com/ELS5_HTML.htm#Section_8.5

According to the specification, javascript doesn't have integers, only floating point numbers.

Upvotes: 2

Related Questions