[issue34337] Fail to get a right answer for 1.2%0.2

Mark Dickinson report at bugs.python.org
Sun Aug 5 03:58:54 EDT 2018


Mark Dickinson <dickinsm at gmail.com> added the comment:

This isn't a bug: in short, you're evaluating a discontinuous function very close to a discontinuity; the errors inherent in floating-point arithmetic put you the "wrong" side of the discontinuity.

Or more simply, with binary floating-point, What You See Is Not What You Get. When you type 1.2, the value actually stored is the closest values that's exactly representable as a float, which turns out to be 1.1999999999999999555910790149937383830547332763671875. Similarly, the value actually stored for 0.2 is 0.200000000000000011102230246251565404236316680908203125. And 1.1999999999999999555910790149937383830547332763671875 % 0.200000000000000011102230246251565404236316680908203125 is 0.199999999999999900079927783735911361873149871826171875.

Some references:

https://docs.python.org/3/tutorial/floatingpoint.html
https://docs.python.org/3/reference/expressions.html#id17

----------
nosy: +mark.dickinson
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34337>
_______________________________________


More information about the Python-bugs-list mailing list