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?

4
  • I apologize if this is a duplicate of another question. I (surprisingly) couldn't find the answer by searching. Commented May 16, 2015 at 17:49
  • Yes. for future responses it is [here][1] [1]: stackoverflow.com/questions/307179/… Commented May 16, 2015 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. Commented May 16, 2015 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. Commented May 16, 2015 at 18:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 Amended my answer to include the numbers with exact precision.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.