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

Rounding Off Issue #51

Open
amitmishra7 opened this issue Apr 10, 2021 · 1 comment
Open

Rounding Off Issue #51

amitmishra7 opened this issue Apr 10, 2021 · 1 comment
Labels
duplicate Indicates similar issues or pull requests question Indicates that an issue or pull request needs more information

Comments

@amitmishra7
Copy link

amitmishra7 commented Apr 10, 2021

Hi, I tried to make a simple calculator using this library. But I am facing an issue with a very simple expression. I tried to multiple 2.3 with 3 and the answer received was 6.8999999999999995. Rather it should have been simply 6.9.
I have used the below code

Expression exp = p.parse('2.3 * 3');
double val = exp.evaluate(EvaluationType.REAL, null);

The value of double returned here is 6.8999999999999995 instead of 6.9.

Another example is with the expression 2.1 * 3 which returned 6.300000000000001 instead of 6.3.

Same is the case with 52.2 % 2 which returned 0.20000000000000284 instead of 0.2

Can any one help me here?

@fkleon
Copy link
Owner

fkleon commented Apr 10, 2021

Hi @amitmishra7

I believe this is a similar issue as #27 and happens due to this library's use of 64 bit double-precision floating point numbers.
This is an issue that you will encounter in Dart and many other languages if you're not using big decimal representations.

I suggest to solve this you could round the results to the intended precision before presenting to the user/processing further.
This can be achieved by using Dart's toStringAsFixed method or writing your own rounding function.

For example:

void main() {
  double result = 2.3 * 3;
  print(result); // prints 6.8999999999999995
  String resultRounded = result.toStringAsFixed(5);
  print(resultRounded); // prints 6.90000
  double resultRoundedAsDouble = double.parse(resultRounded);
  print(resultRoundedAsDouble); // prints 6.9
}

@fkleon fkleon added question Indicates that an issue or pull request needs more information duplicate Indicates similar issues or pull requests labels Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Indicates similar issues or pull requests question Indicates that an issue or pull request needs more information
Projects
None yet
Development

No branches or pull requests

2 participants