This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fail to get a right answer for 1.2%0.2
Type: compile error Stage: resolved
Components: Tests Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, qq100460045
Priority: normal Keywords:

Created on 2018-08-05 07:44 by qq100460045, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg323136 - (view) Author: Yiwei Guo (qq100460045) Date: 2018-08-05 07:46
the answer of 1.2%0.2 should be zero
msg323137 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-08-05 07:58
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
History
Date User Action Args
2022-04-11 14:59:04adminsetgithub: 78518
2018-08-05 07:58:54mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg323137

resolution: not a bug
stage: resolved
2018-08-05 07:46:13qq100460045setmessages: + msg323136
2018-08-05 07:44:45qq100460045create