I am getting an unexpected result when using mod '%' in Python.
When I type


I would expect the result to be 0 as 3.56 goes in to 356 exactly 100 times. However when I run the code it returns 3.5599999999999947.
What is the reason for returning this number and not 0?

Thanks in advance

5 Months
Discussion Span
Last Post by Gribouillis

On my computer, when I write

f = 3.56

in a python program, the following bits are stored in memory

0 10000000000 1100011110101110000101000111101011100001010001111011

This is the IEEE 754 representation of the floating point number 3.56 according to my machine's precision. The corresponding real value is not exactly 3.56, it is

Fraction(8016407336719483, 2251799813685248)

This is equal to

Fraction(356, 100) + Fraction(3, 56294995342131200)

The error is approximately 5.3290705182e-17

Also have a look at https://docs.python.org/3/tutorial/floatingpoint.html

Edited by Gribouillis

Votes + Comments
well written!
That's accurate.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.