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

Wrong result for a simple expression #3

Closed
tomlux opened this issue Jan 2, 2020 · 3 comments
Closed

Wrong result for a simple expression #3

tomlux opened this issue Jan 2, 2020 · 3 comments

Comments

@tomlux
Copy link

tomlux commented Jan 2, 2020

Hy,
I'm making this expression:
3.93 + 2.87 + 2.23 + 2.51 + 2.05 + 2+2.65 + 2.66 + 2.6 + 2.6 + 2.6 + 2.6 + 1.9 + 2.6 + 2.6

The result shoud be: 38.4

But I'm getting when running with the MathExpression as result: 38.400000000000006

Here the simple code:
MathExpression expression = new MathExpression("3.93+2.87+2.23+2.51+2.05+2+2.65+2.66+2.6+2.6+2.6+2.6+1.9+2.6+2.6"); System.out.println("expression.value = " + expression.evaluate().getValue());

Is this a bug in your library or what is here wrong?

Thanks,
Tom

@vatbub
Copy link
Owner

vatbub commented Jan 2, 2020

Hey Tom, the library saves number literals as doubles, the calculations are hence carried out using double precision. To me, this looks like usual rounding errors which occur when doing floating point calculations. I will have a closer look at it later today.

@vatbub
Copy link
Owner

vatbub commented Jan 2, 2020

I just tried your example and modified it a little:

MathExpression expression = new MathExpression("3.93+2.87+2.23+2.51+2.05+2+2.65+2.66+2.6+2.6+2.6+2.6+1.9+2.6+2.6");
double expected = 3.93 + 2.87 + 2.23 + 2.51 + 2.05 + 2 + 2.65 + 2.66 + 2.6 + 2.6 + 2.6 + 2.6 + 1.9 + 2.6 + 2.6;
Number result = expression.evaluate();
System.out.println("expression.value = " + result.getValue());
System.out.println("expected result: = " + expected);

I got the following console output:

expression.value = 38.400000000000006
expected result: = 38.400000000000006

This shows again that the issue is rounding related to floating point rounding errors. If you wish to read more about why this happens, you can read this overview on StackExchange or this paper if you wish to get more in-depth information.

Let me know if you need any other help with the library :)

@vatbub vatbub closed this as completed Jan 2, 2020
@vatbub
Copy link
Owner

vatbub commented Jan 2, 2020

Apart from that, you may want to pull and run mvn install again as I just fixed issues #1 and #2 .
Cheers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants