0

I needed to make a program that iterates over values from x1 to x2 in dx increments. But I can't figure out how it works

a = 1

while a < 2:
    print(a)
    a += 0.1

I got this result.

1
1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008

but wanted to get this result.

1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Nikolay G
  • 11
  • 4
  • Does this answer your question? [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Mohammad Sep 04 '21 at 14:50
  • also https://stackoverflow.com/questions/55517241/how-to-correctly-deal-with-floating-point-arithmetic-in-python – buran Sep 04 '21 at 14:51
  • also https://docs.python.org/3/tutorial/floatingpoint.html – buran Sep 04 '21 at 14:53

0 Answers0