ymutlu
ymutlu

Reputation: 6715

js missing integer

Javascript can't use 10100401822940525, its like 'conn' folder in windows. Why js would fail with this number?

You can check example,

http://jsfiddle.net/ymutlu/yCpWh/1/

Upvotes: 2

Views: 236

Answers (3)

Paul
Paul

Reputation: 141839

In IEEE floating point type numbers, the larger the number gets, the bigger the gap between numbers. It makes sense when you look at how the number is stored.

Consider that there are are the same amount of possible values with a negative exponent as non-negative. That means that there are the same number of possible Numbers between 0 and 1 as there are between 1 and Infinity. Obviously the Numbers between 1 and Infinity as much further spaced out. This spacing is actually distributed so the further away you get from 0 the bigger the gap between possible stored numbers (There is no real max number, it's just eventually you get to the point where no number is distinguishible from Infinity).

This actually works very well, because in most cases you need less precision when dealing with larger numbers. The difference between 10100401822940524 and 10100401822940526 is much less significant than the difference between 1 and 3 relative to the size of the number. It's much more important to be able to represent the number 2 than the number 10100401822940525 and you are limited to 64 bits.

Upvotes: 4

Kendall Frey
Kendall Frey

Reputation: 44316

This is an instance of floating-point rounding. Numbers in Javascript are stored (AFAIK) as a double-precision floating-point number. Floating-point numbers have limited precision, so when the numbers get too big, they can no longer be stored as an exact integer, and they need to be rounded.

Here is another example, with greater error. http://jsfiddle.net/NhZ75/

If you are wondering why the math works, it's because most computers do floating-point math in a higher precision than they store it, so the rounding errors usually only happen when you store the number in a variable.

Upvotes: 1

Michi Werner
Michi Werner

Reputation: 336

I found this: http://gskinner.com/blog/archives/2011/03/the-case-of-the-disappearing-number.html

Don't know if he is right, but it sounds reasonable. ;)

Upvotes: 1

Related Questions