I tried to do this calculation with Node.js.
let x = 841251657
console.log((x*x)-x)
I got: 707704349563994000
printed.
When I use julia I get 707704350405245649
for the same calculation.
And I got 7.0770435e+17
for google calculator.
Apparently, something going wrong here.
What can I do to get the correct answer?
The maximum safe integer is defined as Number.MAX_SAFE_INTEGER
, which is (2^53) -1, which is the largest integer you can represent with double-precision floating point.
> console.log(Number.MAX_SAFE_INTEGER)
9007199254740991
The number you're calculating is significantly larger than that, so precision is being lost. You can check this with Number.isSafeInteger((x*x)-x)
.
For numbers this big, you should be using a BigInt library.