-1

When trying to run the following snippet:

void main() {
      double step = 0.1;
      double currentAmount = 1;
            while (currentAmount < 2) {
              currentAmount = currentAmount + step;
              print (currentAmount);
            }
    }

The output is as follows:

1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008
2.000000000000001

Can anybody tell me why the decimals are appearing as such and is this the normal behavior?

Dakroub_M
  • 122
  • 6

1 Answers1

0

That's because you are losing precision have a look at this package, it should solve your problem

Sami Kanafani
  • 14,244
  • 6
  • 45
  • 41