Konubinix' opinionated web of thoughts

Javascript Integers and Precision


In python, I can do this.

return 10**18 + 1

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

The 1 got lost, as expected.

In javascript, the behavior in float is the same.

return 10.**18 + 1

This is as expected.

But, unfortunately, using integers

return 10**18 + 1

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.)


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

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

Notes linking here