Hankrecords
Hankrecords

Reputation: 365

Javascript - What is the maximum number in scientific notation restricted by?

In JavaScript, I was curious to find out what was the maximum possible number representable in scientific notation without getting "Infinity" as a result, so I wrote a little program and found out it's this one:

17976931348623158079372897140530341507993413271003782693617377898044496829276475094664901797758720709633028641669288791094655554785194040263065748867150582068190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779

which can be abbreviated to 1.7976931348623157e+308.

My question is, what makes this specific number the maximum possible in JavaScript? Is it hardware-dependent (maybe maximum one on 64 bit?) or language-specific? Why exactly is 308 the maximum usable power of 10?

And also, how different is it in other languages?

Upvotes: 3

Views: 2673

Answers (2)

Shadow God
Shadow God

Reputation: 1

=2^1023.99999999999994315658113919198513 dunno why, just tested a bit.

Upvotes: -1

Hevar
Hevar

Reputation: 1529

Short answer:
Double precision float. Due to how the double data-type is defined.

Long answer:

All floating point numbers (double is a double-precision float) are written as a product of two values, the mantissa and the exponent. In principle, this works similar to how numbers are written in scientific notation: for the number 1.34 * 10^24, the mantissa is 1.34 and the exponent is 24.

https://en.wikipedia.org/wiki/Double-precision_floating-point_format

Number.MAX_VALUE

The value of Number.MAX_VALUE is the largest positive finite value of the Number type, which is approximately 1.7976931348623157e+308.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: fafalselse, [[Configurable]]: false }.

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

What differs for floats (and doubles) is that you split the total bytes that hold the number into two parts, one for the mantissa and one for the exponent. double-precision binary floating-point format: binary64

That gives you an exponent of 10 bits, and one sign bit for the exponent, so that would give you a number from -1023 to +1024.

However, the base of the exponent is not 10, but 2. The way the floating point number exponent is stored uses 8 bits (for floats) or 11 bits (for doubles), meaning you get exponent values of -127 to +128 (float) or -1023 to +1024 (double).

And 2^1024 gives us a value of 1.797693134862315907729305190789 * 10^308, which is the largest exponent of a double precision float.

Upvotes: 6

Related Questions