The blog of Ada Rose Cannon

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

5e-324 // smallest non-zero number

Number.MIN_VALUE > 0

Number.MAX_VALUE < Infinity

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:



Beyond this limit you start to lose precision.




What can we precisely represent with limit?

So what exactly can one count with 9 Quadrillion?

Lot’s of fun on wikipedia:

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.