Konubinix' opinionated web of thoughts

Javascript Integers and Precision

Fleeting

In python, I can do this.

return 10**18 + 1
1000000000000000001

Because integer are not supposed to lose precision. Using floats, on the other end, because big numbers EAT small numbers, I get.

return 10.**18 + 1
1e+18

The 1 got lost, as expected.

In javascript, the behavior in float is the same.

return 10.**18 + 1
1000000000000000000

This is as expected.

But, unfortunately, using integers

return 10**18 + 1
1000000000000000000

It also got lost.

Actually, this is explained in

A number literal like 37 in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript also has a BigInt type, but it’s not designed to replace Number for everyday uses. 37 is still a number, not a BigInt.)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

Therefore, one would need to do the following to manipulate those integers.

return BigInt(10**18) + BigInt(1)
1000000000000000001n

Notes linking here