7

I have picked two random double numbers:

double a = 7918.52;
double b = 5000.00;

I would expect to get 2918.52 from a - b.
Well, it gives me a result of 2918.5200000000004, which seems odd.

print(a - b); // -> 2918.5200000000004

But if I change double a to 7918.54, I will get the expected result of 2918.54.

Can someone explain to me why some double values result in unexpected rounding issues and others do not?

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
Zoka
  • 393
  • 3
  • 14
  • 3
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – jamesdlin Jun 16 '19 at 15:56

3 Answers3

6

The reason for this is floating-point arithmetic and the fact that Dart uses the IEEE 754 standard as far as I am concerned.

This happens for all languages that use floating-point arithmetic. You can read through similar questions regarding other programming languages.

General question about floating-point arithmetic in modern programming languages.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
0

creativecreatorormaybenot is right, this is issue comes from the Dart language itself.

The best solution I came up with was to manually round it to the precision you expect :

double substract(double a, double b, int precision) {
  return (a - b).precision(precision);
}

double precision(int fractionDigits) {
  num mod = pow(10.0, fractionDigits);
  return ((this * mod).round().toDouble() / mod);
}
Jack'
  • 1,722
  • 1
  • 19
  • 27
0

A simple solution would be:

  • depending on how many decimals you expect (lets assume 2, you can make it more generic...)
  • ((a * 100).toInt() - (b * 100)).toInt() / 100.
Kowboj
  • 351
  • 2
  • 14