0

The parseInt function made my number loose precision: the last two digits changed from 18 to 20:

console.log(parseInt('76561198236425518', 10));

76561198236425520

Why did that happen and how to fix it?

Aracthor
  • 5,757
  • 6
  • 31
  • 59

1 Answers1

0

Numbers are stored as floating point with a 53-bit manttissa. This limits the precision you can have to less than what you have in that number of yours, hence it has to round to the nearest floating point number it can represent.

The actual number of bits needed to represent a number N can be calculated as about log2N or, if you're working on a calculator that can't calculate logarithms to base two, logxN/logx2.

The value of log276561198236425518, roughly 56.1, shows that it requires about 56 bits, which is why it's not exact near the end.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953