Reputation: 15204
Can anybody please explain why the following happens?
print(-1 * (605 % 11)) #-> 0
print(-1 * (0.5*1210 % 11)) #-> -0.0
print(-1 * (0.5*1210) % 11) #-> 0.0
Especially the -0.0
is baffling..
Upvotes: 1
Views: 342
Reputation: 280564
print(-1 * (605 % 11)) #-> 0
Integer arithmetic. No surprises here.
print(-1 * (0.5*1210 % 11)) #-> -0.0
Here's where you might get surprised. 0.5*1210 % 11
evaluates to floating-point zero, and then -1 * 0.0
results in negative zero, which is a thing in floating-point. It exists because it makes edge cases of some numeric algorithms easier to implement.
print(-1 * (0.5*1210) % 11) #-> 0.0
Here's where someone more familiar with floating-point than with Python might get surprised. -1 * (0.5*1210)
evaluates to -605.0
, but then in -605.0 % 11
, Python defines the %
operation as returning a result of the same sign as the denominator, rather than the numerator, so this returns 0.0
instead of -0.0
.
Upvotes: 6