Javascript Reference
Categories: Number

The max number precision value in javascript

@June 29, 2009, 12:35 p.m.
<script type="text/javascript">
my = 9007199254740992;
for (var i = 0; i < 10; i++) {
    document.write(my + i);
    document.write('<br />');

The results of above example are:
9007199254740992+0 = 9007199254740992
9007199254740992+1 = 9007199254740992
9007199254740992+2 = 9007199254740994
9007199254740992+3 = 9007199254740996

It is out of control but browser will not tell you there is an error which makes bug hard to find.

And if you try 123456789012345678901111111111 - 123456789012345678901111000000, you will get 0. Because both of them are 1.2345678901234568e+29 according to science count.

1) Why these happen?
No matter how big the number system is, you will reach the end even decimal of C#. So javascript was designed to be 2^53 = 9007199254740992 when it was born.

2) About bigger number
Yes, there is bigger number.js to download and use. But it is not a solution. Think about efficiency and speed.
Trying to improve your algorithm instead of making your program slower and slower will be more helpful.

Powered by Linode.