help icon indicating copy to clipboard operation
help copied to clipboard

Giving incorrect output while manipulating large number input

Open touchnewshiva opened this issue 5 years ago • 6 comments

  • Version:12.8.1
  • Platform:Windows 10 Pro 64-bit
  • Subsystem:

What steps will reproduce the bug?

   var aa = 125029734092369566;
   var bb = 2;
   var sum = aa + bb;
   console.log(sum);

How often does it reproduce? Is there a required condition?

This issue started from 18th digit onwards.

What is the expected behavior?

125029734092369568 or Crossed the input size limit

125029734092369570

What do you see instead?

125029734092369570

Additional information

It would be nice If JavaScript handles this.

JS Bug

touchnewshiva avatar Jul 10 '20 16:07 touchnewshiva

JS uses floating-point numbers as its default number type, which means that large numbers will not be represented exactly. The documentation on Number.isSafeInteger has a few more details.

Recent versions of Node.js, including v12.x, support BigInts, i.e. arbitrary-precision integers:

> 125029734092369566n + 2n
125029734092369568n
> Number(125029734092369568n)
125029734092369570 // Imprecise, because converted to Number instead of BigInt

addaleax avatar Jul 10 '20 16:07 addaleax

JS uses floating-point numbers as its default number type, which means that large numbers will not be represented exactly. The documentation on Number.isSafeInteger has a few more details.

Recent versions of Node.js, including v12.x, support BigInts, i.e. arbitrary-precision integers:

> 125029734092369566n + 2n
125029734092369568n
> Number(125029734092369568n)
125029734092369570 // Imprecise, because converted to Number instead of BigInt

Thanks for the suggestion addaleax. with your modification its working. But it'll be awesome if Node throws warning or exception instead of incorrect answer.

touchnewshiva avatar Jul 10 '20 19:07 touchnewshiva

I'm wandering about next steps here, given that type-definitions are part of v8 , there is no role of node.js here (node.js can handle and control it's own API calls.)

PoojaDurgad avatar Dec 22 '20 05:12 PoojaDurgad

@touchnewshiva - anything pending on this issue?

PoojaDurgad avatar Feb 10 '21 10:02 PoojaDurgad

It would be good if we add some auto comments or pop ups to notify that, it's not possible

On Wed, Feb 10, 2021 at 4:20 PM Pooja D P [email protected] wrote:

@touchnewshiva https://github.com/touchnewshiva - anything pending on this issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nodejs/help/issues/2838#issuecomment-776622099, or unsubscribe https://github.com/notifications/unsubscribe-auth/APQDBRANWQVMDEFMOP3SXI3S6JQGFANCNFSM4OWXQSQA .

touchnewshiva avatar Feb 21 '21 19:02 touchnewshiva

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 12 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 12 '24 01:06 github-actions[bot]