help
help copied to clipboard
Giving incorrect output while manipulating large number input
- 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
125029734092369570What do you see instead?
125029734092369570Additional information
It would be nice If JavaScript handles this.
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
JS uses floating-point numbers as its default number type, which means that large numbers will not be represented exactly. The documentation on
Number.isSafeIntegerhas 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.
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.)
@touchnewshiva - anything pending on this issue?
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 .
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.
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.