[Tutor] Modulo operator (was no subject)

Bob Gailer bgailer at alum.rpi.edu
Tue Nov 28 19:44:49 CET 2006


Please always provide a subject line. Otherwise it is very hard to 
follow threads.

Noufal Ibrahim wrote:
> Kopalle Narasimha wrote:
>   
>> Hello, Everyone.
>>
>> I have a strange problem with floating point numbers. Please help me.
>> I tried the following at the python prompt:
>>
>> Case 1:
>>     
>>>>>  4.5/2.0         #Gives quotient
>>>>>           
>> Answer: 2.25
>>
>> Case 2:
>>     
>>>>>  4.5%2.0      #Gives Remainder
>>>>>           
Despite what the documentation says, the modulo operator does not return 
a remainder! Notice:
 >>> [x % 3 for x in range(-5,5)]
[1, 2, 0, 1, 2, 0, 1, 2, 0, 1]
For negative values of the left argument the result is NOT the 
remainder. The literature seems very confused about this, using 
"remainder" when it is NOT the case. What's worse, in some languages the 
mod operator DOES return the remainder.

[snip]

-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list