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

Calculation error #12834

Closed
supachaiofficial opened this issue May 4, 2017 · 4 comments
Closed

Calculation error #12834

supachaiofficial opened this issue May 4, 2017 · 4 comments
Labels
invalid Issues and PRs that are invalid.

Comments

@supachaiofficial
Copy link

  • Version:
    -> v4.8.1
    -> v7.8.0

  • Platform:
    -> Linux ip-172-31-17-62 4.4.0-72-generic [stream] support piping multiple streams into a single stream #93-Ubuntu SMP Fri Mar 31 14:07:41 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
    -> Darwin supachai-macbook-pro.local 16.5.0 Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64

Node Input:
console.log('print output: ',93.46+100.00+93.46)
Node Output:
print output: 286.91999999999996

Expect Correct Result:
print output: 286.92

@ChALkeR
Copy link
Member

ChALkeR commented May 4, 2017

That is the expected behaviour per the ECMAScript spec, the binary numbers defined in IEEE 754-2008 behave that way.

@ChALkeR ChALkeR closed this as completed May 4, 2017
@ChALkeR
Copy link
Member

ChALkeR commented May 4, 2017

You can verify that in browser developers console (both Firefox and Chromium).

Also you can verify that in other languages, e.g. python.

With C++:

  double x = 93.46 + 100.00 + 93.46;
  std::cout << std::setprecision(20) << x << std::endl;

will give you 286.91999999999995907.

The only difference here is the default rounding of the output in C++ — to obtain similar results in JS, you would probably want to perform the rounding manually.

@ChALkeR ChALkeR added the invalid Issues and PRs that are invalid. label May 4, 2017
@supachaiofficial
Copy link
Author

supachaiofficial commented May 4, 2017

echo ("php calculation\n93.46+100.00+93.46 = ");
echo ( 93.46+100.00+93.46 );
echo ("\n");

will show:
php calculation
93.46+100.00+93.46 = 286.92

@ChALkeR
Copy link
Member

ChALkeR commented May 4, 2017

@supachaiofficial That's rounding on the output (or, more specifically, on the default number-to-string conversion).

php > echo number_format(93.46+100.00+93.46, 15)."\n";
286.919999999999959

You could verify that it's the rounding on the output/conversion with a similar example:

php > echo 286.91999999999996 . "\n";
286.92

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.
Projects
None yet
Development

No branches or pull requests

2 participants