Template:Mod/doc
This is a documentation subpage for Template:Mod. It contains usage information, categories, and other content that is not part of the original template page. |
- Description:
Implements the mathematical modulo operator. It should not be confused with the parser function mod (remainder of division after truncating both operands to an integer).
The returned result is always of the same sign as the divisor or zero, and its absolute value is lower than the absolute value of the divisor. This allows this template to be fully compatible with cyclic modular arithmetic (for exampel in calendar computations).
For example if the given modulus is 10, the result will always be positive or null and lower than 10 (all dividend values in that positive range will be returned without change). This is not true with the mod and fmod operators in expressions evaluated with the #expr: ParserFunction extension of MediaWiki.
However, this template returns 0 if the divisor is null (this template should never return any division by zero error).
- Usage:
- {{Mod|dividend|divisor}}
- Examples with subtitution of expressions when saving pages:
- {{subst:Mod|12|7}} = 5.
- Examples with positive integer modulus (all results must be positive or zero):
- {{Mod|12|10}} = 2.
- {{Mod|10.1|10}} = 0.1.
- compare: {{#expr: 10.1 mod 10}} = 0.
- {{Mod|10|10}} = 0.
- {{Mod|2|10}} = 2.
- {{Mod|0|10}} = 0.
- {{Mod|-2|10}} = 8.
- {{Mod|-10|10}} = 0.
- {{Mod|-10.1|10}} = 9.9.
- {{Mod|-12|10}} = 8.
- Examples with negative integer modulus (all results must be negative or zero):
- {{Mod|12|-10}} = -8.
- compare: {{#expr: 12 mod -10}} = 2.
- compare: {{#expr: 12 fmod -10}} = 2.
- {{Mod|10.1|-10}} = 0.1.
- {{Mod|10|-10}} = 0.
- {{Mod|2|-10}} = -8.
- {{Mod|0|-10}} = 0.
- {{Mod|-2|-10}} = -2.
- {{Mod|-10|-10}} = 0.
- {{Mod|-10.1|-10}} = -0.1.
- {{Mod|-12|-10}} = -2.
- Examples with positive non-integer modulus (all results must be positive or zero):
- {{Mod|21.5|10.5}} = 0.5.
- compare: {{#expr: 21.5 mod 10.5}} = 1.
- compare: {{#expr: 21.5 fmod 10.5}} = 0.5.
- {{Mod|21.1|10.5}} = 0.1.
- {{Mod|21|10.5}} = 0.
- {{Mod|20.9|10.5}} = 10.4.
- {{Mod|11|10.5}} = 0.5.
- {{Mod|10.6|10.5}} = 0.1.
- {{Mod|10.5|10.5}} = 0.
- {{Mod|10.1|10.5}} = 10.1.
- {{Mod|10|10.5}} = 10.
- {{Mod|2|10.5}} = 2.
- {{Mod|0|10.5}} = 0.
- {{Mod|-2|10.5}} = 8.5.
- {{Mod|-10|10.5}} = 0.5.
- {{Mod|-10.1|10.5}} = 0.4.
- {{Mod|-10.5|10.5}} = 0.
- {{Mod|-10.6|10.5}} = 10.4.
- {{Mod|-11|10.5}} = 10.
- {{Mod|-20.9|10.5}} = 0.1.
- {{Mod|-21|10.5}} = 0.
- {{Mod|-21.1|10.5}} = 10.4.
- {{Mod|-21.5|10.5}} = 10.
- Examples with negative non-integer modulus (all results must be positive or zero):
- {{Mod|21.5|-10.5}} = -10.
- compare: {{#expr: 21.5 mod -10.5}} = 1.
- compare: {{#expr: 21.5 fmod -10.5}} = 0.5.
- Examples with null modulus (all results are zero by design in this template, without error):
- {{Mod|2|0}} = 0.
- compare: {{#expr: 2 mod 0}} = Expression error: Division by zero.
- compare: {{#expr: 2 fmod 0}} = Expression error: Division by zero.
- {{Mod|0|0}} = 0.
- compare: {{#expr: 0 mod 0}} = Expression error: Division by zero.
- compare: {{#expr: 0 fmod 0}} = Expression error: Division by zero.
- {{Mod|-2|0}} = 0.
- compare: {{#expr: -2 mod 0}} = Expression error: Division by zero.
- compare: {{#expr: -2 fmod 0}} = Expression error: Division by zero.
- Examples with possible overflows on servers using 32-bit integers:
{{Mod|123|(2^32-1)}}
→ 123- compare:
{{#expr: 123 mod (2^32-1)}}
→ 123 (wrong on 32-bit servers) - compare:
{{#expr: 123 fmod (2^32-1)}}
→ 123 (works on recent versions of MediaWiki)
- compare:
{{Mod|123|(2^64-1)}}
→ 123- compare:
{{#expr: 123 mod (2^64-1)}}
→ Expression error: Division by zero (wrong on 32-bit and 64-bit serverw) - compare:
{{#expr: 123 fmod (2^64-1)}}
→ 123 (works on recent versions of MediaWiki)
- compare:
- Examples with non numeric parameters (all results are expression errors by design in this template):
- {{Mod|a|b}} = Expression error: Unrecognized word "b"..
- compare: {{#expr: a mod b}} = Expression error: Unrecognized word "a"..
- compare: {{#expr: a fmod b}} = Expression error: Unrecognized word "a"..
- See also:
- {{Modint}}
- Help:Modulo and round.
- Code and documentation initially imported from w:en:Template:Mod.