Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why max digits with decimal in JavaScript are only 16

I came across this issue a while back when I was testing some HTML forms. The Maximum number of digits in JavaScript Number with a decimal point is only 16

I have tried the following

var x = 12345678912345.6789

x is 12345678912345.68 - 16 Digits only

var x = 123456789123.6789

x is 123456789123.6789 - 16 Digits only

new Number(12345678912345.6789)

12345678912345.68 - 16 Digits only

new Number(123456789123.6789)

123456789123.6789 - 16 Digits only

if you count the total digits, they are 16. If you increase digits before decimal point, the digits after decimal point get rounded

Similarly

new Number(.12345678912367890)

is 0.1234567891236789 - 16 Digits only (notice trailing 0 is missing)

Which makes me deduce that I can only have 16 Digits in a number with Decimal in it. If I try to add more digits, the number starts to round

I also observed that when I serialize a number with decimal into JSoN in ASP.NET MVC, it also converts the number to Max 16 Digits and rounds the rest

My Question: why?

like image 921
U.P Avatar asked Jan 02 '23 06:01

U.P


2 Answers

According to JS/ECMAScript specification, the Number type uses double-precision floating point which has 64-bit format (binary64), consists of a sign bit (determines positive or negative value), 11 exponent bits and 52 fraction bits (each digit represents 4-bits, hence 64-bit has 16 digits):

The Number type representing the double-precision 64-bit format IEEE 754-2008 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic.

The maximum positive number which can be represented properly using double precision is 9007199254740992, which can be achieved by using Math.pow(2, 53). If the number range is between Math.pow(2, 53) and Math.pow(2, 54) (or between Math.pow(2, -53) and Math.pow(2, -54)), only even numbers can be represented properly because the exponent bits will affect LSB (least-significant bit) on the fraction bits.

Let's review the large number part:

var x = 12345678912345.6789

var x = new Number(12345678912345.6789)

This number contains more than 52 fractional bits (72 bits in total), hence the rounding used to keep the fractional bits to 52.

Also with this decimal number:

var x = new Number(.12345678912367890)

This number contains 68 fractional bits, hence the last zero is chopped off to keep 64-bit length.

Usually numeric representation larger than 9007199254740992 or smaller than 1.1102230246251565E-16 are stored as literal strings instead of Number. If you need to compute very large numbers, there are certain external libraries available to perform arbitrary precision arithmetic.

Further reading:

ECMAScript: Number Encoding

ECMAScript: Working with large integers

like image 169
Tetsuya Yamamoto Avatar answered Jan 03 '23 19:01

Tetsuya Yamamoto


In javascript You can't represent most decimal fractions exactly with binary floating point types (which is what ECMAScript uses to represent floating point value).

This is why javascript uses 16 numbers after the decimal point. for example -

Try to run:

var x =  3.14159265358979323846;
print(x.toFixed(20));

and see the following is being cast to float.

If you want to cast more then 16 points after the decimal point you can either:

  • Use literal string to represent your number
  • Use external libraries like math.js or BigInteger.js.
like image 29
Barr J Avatar answered Jan 03 '23 19:01

Barr J