2

I've been writing a function that adds to integers in such a way that the result of the exponential multiplication is the one decimal greater than the second integer, e.g. 2*10**1 + 2 = 22, 2*10**2 = 222 ... now I've run into the following anomaly:

console.log((4 * 10**16) + 5390195186705543)
//logs: 45390195186705544

For some reason, it changes the first decimal to 4. Does it have anything to do with memory allocation?

CherryDT
  • 25,571
  • 5
  • 49
  • 74
gandelino
  • 41
  • 3
  • 2
    `Number`s won't have infinite precision, as everything larger than 2^53 is stored as float and not integer. Use a `BigInt` if you need that. – CherryDT Sep 12 '22 at 15:57
  • check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER – James Sep 12 '22 at 16:05

0 Answers0