Why 7%1.4 is not zero? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: Why 7%1.4 is not zero? (/thread-2308.html) |
Why 7%1.4 is not zero? - tinghf - Mar-06-2017 Hello guys, 7/1.4=5, the remainder is zero, simple maths. But why 7%1.4 gives the result 4.440892098500626e-16? I know it is extremely close to zero, but I want to know how Python do the maths and why it happen. Does anyone can help me? p.s. I am using Python 2.7.13 RE: Why 7%1.4 is not zero? - micseydel - Mar-06-2017 It's because the underlying math is done in binary. Consider 10 / 3, you can't represent it precisely with decimal notation. The same problem happens here, except that 1.4 can't be precisely in binary. If it's a problem in your code, the typical way to solve the problem is to have a tolerance. Check that the result is sufficiently small. 10**(-16) is very small. RE: Why 7%1.4 is not zero? - tinghf - Mar-07-2017 (Mar-06-2017, 07:14 PM)micseydel Wrote: It's because the underlying math is done in binary. Consider 10 / 3, you can't represent it precisely with decimal notation. The same problem happens here, except that 1.4 can't be precisely in binary. Thank you! Got it! |