-1

I'm new to python and I want you someone to explain me this:

print(9.2%4) must give me the remainder when 4 is divided by 9.2 and the output of the code is 1.1999999999999993 which is clearly not true

But this code: print(9.2/4) gives me output 2.3

Someone please explain this to me

  • 3
    "_9.2%4 must give me the remainder when 4 is divided by 9.2_" It is the other way around - the remainder is given when 9.2 is divided by 4. I can type `9.2%4` into my Chrome address bar and it does show 1.2 as the answer which is just a rounded off version of the answer you got. – takendarkk Oct 13 '21 at 18:57
  • 1
    9. module 4 is 1.2,. You're getting 1.1999999999999993 because of floating point innacuracy. – kubatucka Oct 13 '21 at 18:58
  • 1
    also in python floor division is `//`, so `9.2 // 4 == 2`, `/` is "true division" that will return regular 9.2 divided by 4 – 0dminnimda Oct 13 '21 at 18:59

1 Answers1

0

Think of it this way.

9.2 % 4 == (8 + 1.2) % 4

You can expand the right side of the above statement as follows:

((8 % 4) + (1.2 %4) )

The above can then be simplified as follow:

(0 + 1.2) == 1.2 == 1.999999999

pyzer
  • 122
  • 7