Ev. Kounis - 1 year ago 59

Python Question

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`

Answer Source

```
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 negative zero as above, but then in `-0.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`

.