Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why? console.log(parseFloat(1024) + parseFloat(300.86)); = 1324.8600000000001 #3542

Closed
iceopen opened this issue Oct 27, 2015 · 3 comments
Closed
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.

Comments

@iceopen
Copy link

iceopen commented Oct 27, 2015

console.log(parseFloat(1024) + parseFloat(300.86));
console.log(parseFloat(100) + parseFloat(100.86));
console.log(parseFloat(1024) + parseFloat(200.78));
console.log(parseFloat(400) + parseFloat(200.86));
var tol = parseFloat(1024) + parseFloat(300.86);
console.log(tol);

1324.8600000000001
200.86
1224.78
600.86
1324.8600000000001

@mscdex mscdex added the question Issues that look for answers. label Oct 27, 2015
@mscdex
Copy link
Contributor

mscdex commented Oct 27, 2015

Welcome to the world of floating point math. There are lots of articles that describe how floating point in general works. You might start with the answers from this stackoverflow question.

@iceopen
Copy link
Author

iceopen commented Oct 27, 2015

I tested the pull 3 programming languages:
Java:1324.8600000000001
Node:1324.8600000000001
PHP:1324.86

@bnoordhuis
Copy link
Member

Closing, not a bug. Like @mscdex said, that's how floating point values work; 1324.86 is a value that cannot be exactly represented in a binary IEEE-754 double.

PHP has the same issue by the way, it's just that the way it prints floating point numbers by default doesn't show it:

$ php -r 'printf("%.16f\n", 1024.0 + 300.86);'
1324.8600000000001273

One language that does get it right is perl6:

$ perl6 -e 'printf "%.16f\n", 1024.0 + 300.86'
1324.8600000000000000

That's because it represents the number as a nominator/denominator rational:

$ perl6 -e 'print "{(1024.0 + 300.86).nude}\n"'
66243 50

The more you know, right?

@bnoordhuis bnoordhuis added the invalid Issues and PRs that are invalid. label Oct 27, 2015
ryzokuken added a commit to ryzokuken/node that referenced this issue Mar 8, 2018
Rename the test appropriately alongside mentioning the subsystem
Also, make a few basic changes to make sure the test conforms
to the standard test structure

Refs: nodejs#19105
Refs: https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md#test-structure
BridgeAR pushed a commit to BridgeAR/node that referenced this issue Mar 11, 2018
Rename the tests appropriately alongside mentioning the subsystem.
Also, make a few basic changes to make sure the test conforms
to the standard test structure.

This renames:
- test-regress-nodejsGH-1531
- test-regress-nodejsGH-2245
- test-regress-nodejsGH-3238
- test-regress-nodejsGH-3542
- test-regress-nodejsGH-3739
- test-regress-nodejsGH-4256

PR-URL: nodejs#19212
Refs: nodejs#19105
Refs: https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md#test-structure
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this issue Mar 17, 2018
Rename the tests appropriately alongside mentioning the subsystem.
Also, make a few basic changes to make sure the test conforms
to the standard test structure.

This renames:
- test-regress-GH-1531
- test-regress-GH-2245
- test-regress-GH-3238
- test-regress-GH-3542
- test-regress-GH-3739
- test-regress-GH-4256

PR-URL: #19212
Refs: #19105
Refs: https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md#test-structure
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue Mar 20, 2018
Rename the tests appropriately alongside mentioning the subsystem.
Also, make a few basic changes to make sure the test conforms
to the standard test structure.

This renames:
- test-regress-GH-1531
- test-regress-GH-2245
- test-regress-GH-3238
- test-regress-GH-3542
- test-regress-GH-3739
- test-regress-GH-4256

PR-URL: #19212
Refs: #19105
Refs: https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md#test-structure
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
MayaLekova pushed a commit to MayaLekova/node that referenced this issue May 8, 2018
Rename the tests appropriately alongside mentioning the subsystem.
Also, make a few basic changes to make sure the test conforms
to the standard test structure.

This renames:
- test-regress-nodejsGH-1531
- test-regress-nodejsGH-2245
- test-regress-nodejsGH-3238
- test-regress-nodejsGH-3542
- test-regress-nodejsGH-3739
- test-regress-nodejsGH-4256

PR-URL: nodejs#19212
Refs: nodejs#19105
Refs: https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md#test-structure
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
BethGriggs pushed a commit that referenced this issue Oct 2, 2018
Rename the tests appropriately alongside mentioning the subsystem.
Also, make a few basic changes to make sure the test conforms
to the standard test structure.

This renames:
- test-regress-GH-1531
- test-regress-GH-2245
- test-regress-GH-3238
- test-regress-GH-3542
- test-regress-GH-3739
- test-regress-GH-4256

PR-URL: #19212
Refs: #19105
Refs: https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md#test-structure
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

3 participants