a negative dividend splits the conventions
- Dividend
- -17
- Divisor
- 5
- Raise the dividend to this power first
- 0
Result3
floor(-3.4) is -4 while trunc is -3, which is the whole disagreement
Open this exampleThe modulo of two integers under both conventions, because they disagree on negative numbers and the disagreement causes real bugs. Minus seventeen mod five is three in Python and minus two in C, and both are defensible.
Also called: mod calculator, remainder calculator.
-17 mod 5 is 3 by the mathematical convention, and -2 by the one most programming languages use. The two differ because the division rounds differently: flooring gives 3, which takes the sign of the divisor and is what Python returns, while truncating gives -2, which takes the sign of the dividend and is what C, Java, JavaScript and Go return. To get the mathematical result in those languages, use ((a % n) + n) % n. Enter a power to see modular exponentiation, which is computed by repeated squaring rather than by raising and then reducing.
| Dividend | Floored, as in Python | Truncated, as in C |
|---|---|---|
| -32 | 3 | -2 |
| -27 | 3 | -2 |
| -22 | 3 | -2 |
| -17 | 3 | -2 |
| -12 | 3 | -2 |
| -7 | 3 | -2 |
| -2 | 3 | -2 |
Modulo is the remainder after division, and everything hinges on how the division rounds. Flooring the quotient toward negative infinity gives a result that always takes the sign of the divisor, which is the mathematical convention and what Python does. Truncating the quotient toward zero gives a result taking the sign of the dividend, which is what C, Java, JavaScript and Go do. For positive inputs they agree exactly, which is why the difference goes unnoticed until a negative index or a wrapped timestamp appears. Modular exponentiation is computed by repeated squaring rather than by raising and then reducing, since the intermediate value would overflow long before the answer.
flooring the quotient makes the result take the sign of the divisor, which is what the mathematical convention wants and what most languages do not doEach of these is asserted on every build. If a change to the engine ever moved one of these answers, the build would fail before the page could print it.
Result3
floor(-3.4) is -4 while trunc is -3, which is the whole disagreement
Open this exampleResult2
boundary: the conventions only diverge on negatives
Open this exampleFormula version 1.0.0 · definition 1.0.0 · United States · Report a problem with this calculator