The beginning of infinity in JavaScript

Infinity begins relatively early in JavaScript:

> Math.pow(2, 1024)
Infinity
> Math.pow(2, 1023)
8.98846567431158e+307

What is going on here?


All numbers in JavaScript are floating point numbers and (roughly) encoded internally [1] as


1.f × 2p

This is the most common way of representing floating point numbers. The left-hand side of the multiplication comprises the digits, the right-hand side moves the dot to the correct position (to the left if it is negative, to the right if it is positive).


The mantissa is a binary 1, followed by a binary dot, followed by a 52 bit fraction f. The 11 bit exponent p has to be in the range


−1023 < p < 1024

Thus, the exponent of Math.pow(2, 1024) is out of range and Infinity is an error result.


Consult [1] if you want to know more about how numbers are encoded in JavaScript.

Reference




  1. How numbers are encoded in JavaScript


Comments

Popular posts from this blog

Steve Lopez and the Importance of Newspapers

A Treasure Hunt Without The Treasure

Drop a ping-pong ball in the clown’s mouth