0

This question asks about the highest number in JavaScript without losing precision. Here, I ask about the highest representable number in JavaScript. Some answers to the other question reference the answer to this question, but they do not answer that question, so I hope I am safe asking here.

I tried to find the answer, but I got lost halfway through. The highest number representable in JavaScript seems to be somewhere between 2^1023 and 2^1024. I went further (in the iojs REPL) with

var highest = Math.pow(2, 1023);
for(let i = 1022; i > someNumber; i--) {
  highest += Math.pow(2, someNumber);
}

The highest number here seems to be when someNumber is between 969 and 970. This means it is between (2^1023 + 2^1022 + ... + 2^970) and (2^1023 + 2^1022 + ... + 2^969). I'm not sure how to go further without running out of memory and/or waiting years for a loop or function to finish.

What is the highest number representable in JavaScript? Does JavaScript store all digits of this number, or just some, because whenever I see numbers of 10^21 or higher they are represented in scientific notation? Why can JavaScript represent these extremely high numbers, especially if it can "remember" all the digits? Isn't JavaScript a base 64 language?

Finally, why is this the highest representable number? I am asking because it is not an integer exponent of 2. Is it because it is a floating point number? If we took the highest floating point number, would that be related to some exponent of 2?

Community
  • 1
  • 1
trysis
  • 8,086
  • 17
  • 51
  • 80
  • I apologize if this is a duplicate of another question. I (surprisingly) couldn't find the answer by searching. – trysis May 16 '15 at 17:49
  • Yes. for future responses it is [here][1] [1]: http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin – AskarovBeknar May 16 '15 at 17:55
  • I mentioned that question in my question, but I'm asking the highest number that JavaScript can handle, while that question is asking about the highest number it can handle *without losing precision*. Please read the question before you comment. – trysis May 16 '15 at 18:00
  • I wish the "duplicate answer" flag had a "No my question is different,*I already explained how*" resolution. Because I did already explain how. – trysis May 16 '15 at 18:07

2 Answers2

3

ECMAScript uses IEEE 754 floating points to represent all numbers (integers and floating points) in memory. It uses double precision (64 bit), so the largest possible number would be the following (in binary):

(-1)^0 * (1.1111111111111111111111111111111111111111111111111111)_2 * 2^1023
     ^      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        ^^^^
 sign bit                     52 binary digits                         exponent

That is 1.9999999999999997779553950749686919152736663818359375 * 2^1023, which is exactly equal to 179769313486231570814527423734518473246981451219193357160576872808257310254570927992989173324623785766498017753719800531497718555288192667185248845624861831489179706103179456665410545164365169396987674822445002542175370097858557402467390846365155202987281348776667818932226328810501776426180817703854493120592.218308495538871112145305600. That number is also available in JavaScript as Number.MAX_VALUE.

poke
  • 369,085
  • 72
  • 557
  • 602
  • The question I linked to actually referenced Number.MAX_VALUE (though it's not an answer to that question) but I kind of wanted to know why, and also the exact number. By the way, you say that number is equal to 1.797693134862315708145274237e+308, but is that *exactly* equal, or approximately? – trysis May 16 '15 at 18:05
  • @trysis Amended my answer to include the numbers with exact precision. – poke May 16 '15 at 18:11
2

JavaScript uses IEE 754 double-precision floating point numbers, aka the binary64. This format has 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.

The highest possible number is that which is encoded using the highest possible exponents and mantissa, with a 0 sign bit. Except that the exponent value of 7ff (base 16) is used to encode Infinity and NaNs. The largest number is therefore encoded as 7fef ffff ffff ffff, and its value is (1 + (1 − 2^(−52))) × 2^1023.

Refer to the linked article for further details about the formula.

sjrd
  • 21,805
  • 2
  • 61
  • 91