2

I got an error when trying to multiply a number with a floating point in dart. Does anyone know why this happens?

void main() {
    double x = 37.8;
    int y = 100;

    var z = x * y;
    print(z);
    // 3779.9999999999995
}

In other languages ​​(C#/C++) I would have the result: 3780.0

  • Are there some assignment going on since we got nearly the same question earlier? https://stackoverflow.com/questions/70693686/double-multiplication-in-dart-gives-unusual-result – julemand101 Jan 13 '22 at 12:47
  • C++ gives me `3779.9999999999995453` as an answer :-) – Bart Jan 13 '22 at 12:48
  • Anyhow, this is just the result of binary floating point math: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Bart Jan 13 '22 at 12:50

2 Answers2

2

You can use toStringAsFixed to control fraction digits.

void main() {
  double x = 37.8;
  int y = 100;

  var z = x * y;
  print(z.toStringAsFixed(1));
  // 3780.0
}

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
2

This is completely expected because 37.8 (or rather, the 0.8 part) cannot be precisely encoded as a binary fraction in the IEEE754 standard so instead you get a close approximation that will include an error term in the LSBs of the fraction.

If you need numbers that are lossless (e.g. if you are handling monetary calculations) then check out the decimal package.

A simpler hack if your floating point number has sufficient bits allocated to the fraction to keep the erroroneous bits out of the way is to round off the number after your calculation to the number of decimal places that you care about.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61