Difference between revisions of "Template:Mod"

From Traveller Wiki - Science-Fiction Adventure in the Far future
Jump to navigation Jump to search
(Created page with "<includeonly>{{ {{{|safesubst:}}}#ifexpr:({{{2}}})=0|0|{{ {{{|safesubst:}}}#ifexpr:(({{{1}}})/({{{2}}}))=floor(({{{1}}})/({{{2}}}))|0|{{ {{{|safesubst:}}}#ifexpr:(({{{1}}})/({...")
 
Line 1: Line 1:
<includeonly>{{ {{{|safesubst:}}}#ifexpr:({{{2}}})=0|0|{{ {{{|safesubst:}}}#ifexpr:(({{{1}}})/({{{2}}}))=floor(({{{1}}})/({{{2}}}))|0|{{ {{{|safesubst:}}}#ifexpr:(({{{1}}})/({{{2}}}))>0and(({{{1}}})/({{{2}}}))<1|{{ {{{|safesubst:}}}#expr:{{{1}}}}}|{{ {{{|safesubst:}}}#expr:({{{1}}})-floor(({{{1}}})/({{{2}}}))*({{{2}}})}}}}}}}}</includeonly><includeonly>
+
<includeonly>{{ {{{|safesubst:}}}#ifexpr:({{{2}}})=0|0|{{ {{{|safesubst:}}}#ifexpr:(({{{1}}})/({{{2}}}))=floor(({{{1}}})/({{{2}}}))|0|{{ {{{|safesubst:}}}#ifexpr:(({{{1}}})/({{{2}}}))>0and(({{{1}}})/({{{2}}}))<1|{{ {{{|safesubst:}}}#expr:{{{1}}}}}|{{ {{{|safesubst:}}}#expr:({{{1}}})-floor(({{{1}}})/({{{2}}}))*({{{2}}})}}}}}}}}</includeonly><noinclude>
 
Implements the mathematical [[w:en:modulo|modulo]] operator. It should not be confused with the parser function [[ParserFunctions#.23expr:|mod]] (remainder of division after truncating both operands to an integer).
 
Implements the mathematical [[w:en:modulo|modulo]] operator. It should not be confused with the parser function [[ParserFunctions#.23expr:|mod]] (remainder of division after truncating both operands to an integer).
  

Revision as of 10:57, 19 May 2016

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).