Typescript Number Min & Max & Safe Values

The Typescript number has its limitation regarding Max Value, Safe Value & Min Value that you can use. The Typescript uses the MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, MAX_VALUE & MIN_VALUE to represent these values.

The Javascript stores the numbers as a double-precision 64-bit number, which means that it has a total 64-bit memory allocated to it. These numbers are stored using the scientific notation in binary format.

Scientific Notation

Out of these 64 bits, one bit is used for the sign, 11 for the exponent and remaining 52 bits are used for the mantissa (significand).

Double Precision Base 64 Number format.
  • Sign bit indicates whether the number is positive or negative. The 0 stands for positive
  • Significand contains the digits of the number.
  • The exponent tells us the position of the point

Convert Decimal to Binary 64 Bit & Vice Versa

You can follow these links to find out how to

  1. convert decimal number to binary 64-bit
  2. convert binary 64-bit number to decimal

MAX_SAFE_INTEGER & MIN_SAFE_INTEGER

The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer that you can use in Typescript. It is the static property of the Number object. It has the value of 9007199254740991.

It is referred to as safe because any number more than the above number is not guaranteed to be represented exactly and correctly. This is not the limitation of the Typescript, but a limitation imposed by the double-precision 64-bit number format followed by Javascript

9007199254740991 is represented

Any number above this number will result in loss of accuracy. For Example, adding 1 & 2 to the MAX_SAFE_INTEGER results in the same value

And that is because of both converts to same in 64-bit binary

isSafeInteger

You can use the Number.isSafeInteger method to check whether the number is safe.

MAX_VALUE & MIN_VALUE

The largest number possible to represent using the number data type is 1.7976931348623157e+308 and it is represented by Number.MAX_VALUE. The lowest number is Number.MIN_VALUE.

Numbers beyond Number.MAX_VALUE cannot be represented using the double-precision 64-bit binary system. Hence any value above the MAX_VALUE is truncated to the MAX_VALUE.

While the very large value results in infinity

Numbers & Bitwise operators

The bitwise operators and shift operators operate on 32-bit integers only, so in that case, the max safe integer is 2147483647.

Summary

The Typescript Number object has static properties MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, MAX_VALUE & MIN_VALUE. These represent the Maximum/Minimum values that are supported by the number data type. The MAX_SAFE_INTEGER & MIN_SAFE_INTEGER are extremely important, as any number above these numbers are not guaranteed to be represented exactly and correctly.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top