JavaScript Nodejs failed to subtract number

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?

Answer:1

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.

Answer:2

In many places I'm setting modified by, created by etc: const test = this.state.isValueEdited ? { modifiedById: getLoggedInUser().userId, } : { // TODO }; How could I check if getLoggedInUser has ...

In many places I'm setting modified by, created by etc: const test = this.state.isValueEdited ? { modifiedById: getLoggedInUser().userId, } : { // TODO }; How could I check if getLoggedInUser has ...

  1. conditional operator react native
  2. conditional operator react js
  3. conditional operator react
  4. conditional (ternary) operator react
  5. conditional operator in react render
  6. conditional rendering ternary operator react

I have an Array with Objects, there some of them has same "Subarrays" but with different labels. I need to summarize those based on equality. Comparing two different arrays is easy so far, but I am ...

I have an Array with Objects, there some of them has same "Subarrays" but with different labels. I need to summarize those based on equality. Comparing two different arrays is easy so far, but I am ...

I have an array with multiple nested arrays. Each nested array has three objects. I am trying to delete the second one but at the moment, I am getting a null value in its place. All I want is the ...

I have an array with multiple nested arrays. Each nested array has three objects. I am trying to delete the second one but at the moment, I am getting a null value in its place. All I want is the ...

Going through an online test, i found this weird expression where "1"- -"1" = 2 i understand -"1" will be converted to number, but the other "1"- is getting converted too?

Going through an online test, i found this weird expression where "1"- -"1" = 2 i understand -"1" will be converted to number, but the other "1"- is getting converted too?

  1. need some explanation
  2. we need some explanation
  3. he need some milk explanation
  4. some things don't need an explanation
  5. need some explanation traduction