0

When I run the following code:

for x in np.arange(0, 1, 0.1):
    print(x)

I get the following output?

0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9

Why python does not display the numbers as expected? Is there any way to make python display the numbers as expected (i.e., 0.1, 0.2, 0.3, etc.)?

ASE
  • 1,702
  • 2
  • 21
  • 29
  • 3
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) –  Nov 02 '20 at 23:19
  • I changed the question. Please see the modified question. – ASE Nov 02 '20 at 23:25
  • 1
    Because the fourth element printed out is `0.1 * 3` and you're expecting `3.0 / 10`. These are not equal. Floating point will be filled with surprises until you understand why they are not equal. – Frank Yellin Nov 02 '20 at 23:26
  • [How to format a floating number to fixed width in Python](https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python) –  Nov 02 '20 at 23:27

0 Answers0