2

The ECMAScript Number type has exactly 18437736874454810627 values.

Why did they come to this number (18437736874454810627)? In other words, because computers are usually based on binary system, then almost all the numbers (like bit, byte, int16, int32, etc) are based on 2. But I can't see how we can get from 18437736874454810627 to 2.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
user1039304
  • 365
  • 5
  • 10

5 Answers5

6

Because a floating point number used by JavaScript has a fixed number of bits, used as explained in the ECMA standard (page 41 of the PDF, or page 29 as written in the footer).

The Number type has exactly 18437736874454810627 (that is, 2^64 - 2^53 + 3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 (that is, 2^53 - 2) distinct "Not-a-Number" values of the IEEE Standard are represented in ECMAScript as a single special NaN value.

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • i don't understand why is 2^64 - 2^53 + 3 and not 2^64 – user1039304 Nov 12 '11 at 15:23
  • The details of the standard are quite complicated, but from the sentence I've pasted you read *except that the 9007199254740990 (that is, 2^53 - 2) distinct "Not-a-Number" values of the IEEE Standard are represented in ECMAScript as a single special NaN value.* – stivlo Nov 12 '11 at 15:26
  • 2
    @user1039304 - A double precision IEEE 754 number uses 1 bit for the sign, 11 bits for the exponent and 52 bits for the significand. That's a total of 64 bits. You can read more about it [here](http://en.wikipedia.org/wiki/Floating_point). Some bits are used to represent +/-0, +/- infinity and NaN. – jfriend00 Nov 12 '11 at 15:29
2

According to my understanding to the specification ECMA Section 8.5 - The Number Type, there are 3 kinds of representations to the Javascript Number, namely:

  • NaN
  • infinity
  • finite numbers

Let's read again a few excerpts from the specification:

the 9007199254740990 (that is, 253−2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value. [...] all NaN values are indistinguishable from each other.

Note that NaN is actually 1 single special value although multiple values are used to represent NaNs.

There are two other special values, called positive Infinity and negative Infinity.

This is simple and obvious that we have 2 infinity values.

The other 18437736874454810624 (that is, 264−253) values are called the finite numbers.

And there are 18437736874454810624 finite numbers.


So, adding up all the numbers: 1 + 2 + 18437736874454810624 = 18437736874454810627

cychoi
  • 2,890
  • 3
  • 22
  • 31
1

JavaScript uses IEEE-754 numbers which only offer 53 bits of precision. See the "Double Precision" table in that article. Also see this StackOverflow question.

Community
  • 1
  • 1
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
0

Here's my understanding:

  1. JavaScript is using 64-bit floating-point format defined by the IEEE 754 standard.

  2. According to this standard

    • A. There are 264 different kinds of values that can be presented as a numeric value according to this standard.
    • B. 253 of them are used to represent "NaN" & "+∞" & "-∞" (see Exponent encoding in wiki page)

So, Number type will be:

  • A - B + NaN + +Infinity + -Infinity
0

In javascript, 9007199254740990 of those values are NaN .(The IEEE Standard for 64 bit Binary Floating-Point Arithmetic defines a value for all but one NaN)

kennebec
  • 102,654
  • 32
  • 106
  • 127