Unexpected result - Printable Version +- Python Forum ( https://python-forum.io)+-- Forum: Python Coding ( https://python-forum.io/Forum-Python-Coding)+--- Forum: General Coding Help ( https://python-forum.io/Forum-General-Coding-Help)+--- Thread: Unexpected result ( /Thread-Unexpected-result) |

Unexpected result - eftimios - Dec-02-2018
I'm teaching myself python and used the following tutorial: https://www.pythoncentral.io/pythons-range-function-explained/ >>> def frange(start, stop, step): ... i = start ... while i < stop: ... yield i ... i += step ... >>> for f in frange(0.5,3.0,0.1): ... print(f)Instead of increments in 0.1 steps I get: I can't work out why some of the increments have values that aren't exactly .1 from the previous.Is there some secret floa behaviour? Thanks. RE: Unexpected result - Gribouillis - Dec-02-2018
The python float 0.1 is not exactly equal to the same mathematical number because computers store numbers in binary form and the real number 0.1 cannot be represented exactly in base 2 on 64 bits. This is not a limitation of python, it is a limitation of the widely adopted IEEE754 floating point format used by computers. The true value of 0.1 in your program is >>> (0.1).as_integer_ratio() (3602879701896397, 36028797018963968)You can read this documentation page for more. |