What's in a Number?
More accurately, what can be represented by a JavaScript Number object?
Recently on twitter I saw a question about adding on a Gigasecond (1 Billion seconds), and how it seems inappropriate you need to convert times and dates to milliseconds, thus making 1Gs into an even larger numbers. But 1Gs in ms (10^12 seconds) fits easily in the range of numbers available in JavaScript. This got me thinking what are the limits of what can be represented by natural numbers in JS.
What is the range of numbers in JS?
Naively one may assume that querying the biggest and smallest numbers
// largest natural number which can be represented by a Number
Number.MAX_VALUE
1.7976931348623157e+308
Number.MIN_VALUE
5e-324 // smallest non-zero number
Number.MIN_VALUE > 0
true
Number.MAX_VALUE < Infinity
true
Numbers in JavaScript are Double-precision floating-point format which has 53 bits of precision.
53 bits is not enough to represent the 10^308
stated above.
This means that part of this range must be non-contiguous, there are going to be gaps if you try counting up to this number in JavaScript.
So at what point can we no longer count any more?
Well 53 bits can represent up to:
Math.pow(2, 53) - 1;
9007199254740991 // ~~ 9×10^15
Handily this number is a constant on the Number object:
Number.MAX_SAFE_INTEGER
9007199254740991
Number.MIN_SAFE_INTEGER
-9007199254740991
Beyond this limit you start to lose precision.
Number.MAX_SAFE_INTEGER + 1
9007199254740992
Number.MAX_SAFE_INTEGER + 2
9007199254740992
Number.MAX_SAFE_INTEGER + 3
9007199254740994
What can we precisely represent with limit?
So what exactly can one count with 9 Quadrillion?
Lot’s of fun on wikipedia: https://en.wikipedia.org/wiki/Category:Orders_of_magnitude
- This is about 500× US Debt in Dollars
- Number of Synapses in the Human Brain is of this Magnitude
- Number of atoms in about 10 nanograms of material.
- Earths atmosphere is about 5×10^15 Tons
- One Quadrillion Pennies
- One quadrillion seconds is about 31M years.
- One quadrillion meters is about 6685 × The distance between the Earth and Sun. (~0.1 light years).
- One quadrillion bytes is a Petabyte.
When does JavaScript expire?
I initially thought the Date object would only be limited by the max integer.
new Date(Number.MAX_SAFE_INTEGER)
Invalid Date // Awww
Apparently it is limited to one hundred million days (10^12
) days either side of 1/1/1970 which is 100000000*24*3600*1000 = 8640000000000000ms
. About 95% of the MAX_SAFE_INTEGER
new Date(8640000000000000)
Sat Sep 13 275760 01:00:00 GMT+0100 (BST)
new Date(-8640000000000000)
Tue Apr 20 -271821 01:00:00 GMT+0100 (BST)
// That only gives us 273744 years of JavaScript left!!!
Unfortunately on a cosmological scale (Billions of years) this timeline is nothing. It doesn’t really scratch the age of the sun. (5 Billion years)
Nor does it even approach a geological timeline. Pangea broke apart 200 million years ago.
But it does encompass human history quite nicely: Modern humans evolved and started migrating from africa about 200,000 years ago.
Although I doubt early homo sapiens would have much use for milisecond levels of precision.