Changes
omitting spaces doesn't improve readability
{{h:h}}[[Category:Editor handbook]]
The MediaWiki extension [[m:ParserFunctions|ParserFunctions]] enables users to perform simple mathematical [[m:Help:Calculation|computations]].
<nowiki>#</nowiki>expr and #ifexpr allow mod and round.
{| cellpadding="6px" border=1 style="border:1px solid #C0C0C0; border-collapse:collapse;"
! ''Operator''
! ''Operation''
! ''Example''
|-
! [[w:en:Modulo operation|mod]]
|| "Modulo", remainder of division after truncating both operands to an integer.<br /><br />Caveat, '''mod''' is different from all programming languages. This has been fixed (but needs to be committed), see [[bugzilla:6068]].
||{{evaldemo|#expr: 30 mod 7|=}}<br />{{evaldemo|#expr: -8 mod -3|=}}<br />{{evaldemo|#expr: -8 mod +3|=}}<br />{{evaldemo|#expr: 8 mod 2.7|=}} (should be 2.6)<br />{{evaldemo|#expr: 8 mod 3.2|=}} (should be 1.6)<br />{{evaldemo|#expr: 8.9 mod 3|=}} (should be 2.9)
|-
! round
|| Rounds off the number on the left to the power of 1/10 given on the right
||{{evaldemo|#expr: 30/7 round 3|=}}<br />{{evaldemo|#expr: 30/7 round 0|=}}<br />{{<small> </small><code>#expr: 3456 round -2</code>}} = {{#expr: 3456 round -2}}
|}
Spaces around mod and round are good for readability but not needed for working properly:
*{{evd|#expr:7mod3}}
*{{evd|#expr:7.5round0}}
Precedence:
*{{evaldemo|#expr:1.234 + 1.234 round 1 + 1 }}
(first additions, then round)
*{{evaldemo|#expr:3 * 4 mod 10 * 10 }}
(mod and multiplication have equal precedence, evaluation from left to right)
To remind the reader of the precedence, one might write:
*{{evaldemo|#expr:1.234+1.234 round 1+1 }}
*{{evaldemo|#expr:2 + 3*4mod10*10 + 5}}
;When using spaces where there is precedence, the layout of the expression may be confusing:
:{{evaldemo|#expr:23+45 mod 10}}
;Instead one can write:
:{{evaldemo|#expr:23 + 45 mod10}}
;or simply use parentheses:
:{{evaldemo|#expr:23 + (45 mod 10)}}
==Mod==
To get a positive mod even for a negative number, use e.g. (700000 + x) mod7 instead of x mod7. The range of the result is now 0-6, provided that x > -700000.
Alternatively, use
*6 - ( 700006 - x ) mod7
or
*(x - 700006) mod7 + 6.
The range of the result is 0-6, provided that x < 700006.
Working for all x is:
*(x mod7 + 7) mod7
==Round==
*{{evd|#expr:2.5 round 0}}
*{{evd|#expr:-2.5 round 0}}
To round an integer plus one half for x > -100000 toward plus infinity, use:
*(x + 100000 round 0) - 100000
and to round an integer plus one half for x < 100000 toward minus infinity, use:
*(x - 100000 round 0) + 100000
To round x toward minus infinity, use:
*x + ( x != x round 0 ) * ( ( ( x - .5 ) round 0 ) - x )
and toward plus infinity
*x + ( x != x round 0 ) * ( ( ( x + .5 ) round 0 ) - x )
If x is a long expression this multiplies the length by 5! Under conditions for x there are alternatives:
To round x > -100000 toward minus infinity, use:
*(x - 100000.5 round 0) + 100000
and to round x < 100000 toward plus infinity, use:
*(x + 100000.5 round 0) - 100000
If x is a multiple of 1/n with n<1000 we can round toward minus infinity with:
*x - .499 round 0
For arbitrary n > 1 we can choose instead of -.499 any number between -.5 and -.5 + 1/n.
To find the largest multiple of 7 not larger than x (i.e. to round toward minus infinity to a multiple of 7) we can do:
*((x-3)/7 round 0) * 7
==See also==
*{{tim|mod}}
*[[m:Category:Mathematical templates]]
*[[w:en:Category:Mathematical templates]]
{{h:f|enname=Modulo and round}}
The MediaWiki extension [[m:ParserFunctions|ParserFunctions]] enables users to perform simple mathematical [[m:Help:Calculation|computations]].
<nowiki>#</nowiki>expr and #ifexpr allow mod and round.
{| cellpadding="6px" border=1 style="border:1px solid #C0C0C0; border-collapse:collapse;"
! ''Operator''
! ''Operation''
! ''Example''
|-
! [[w:en:Modulo operation|mod]]
|| "Modulo", remainder of division after truncating both operands to an integer.<br /><br />Caveat, '''mod''' is different from all programming languages. This has been fixed (but needs to be committed), see [[bugzilla:6068]].
||{{evaldemo|#expr: 30 mod 7|=}}<br />{{evaldemo|#expr: -8 mod -3|=}}<br />{{evaldemo|#expr: -8 mod +3|=}}<br />{{evaldemo|#expr: 8 mod 2.7|=}} (should be 2.6)<br />{{evaldemo|#expr: 8 mod 3.2|=}} (should be 1.6)<br />{{evaldemo|#expr: 8.9 mod 3|=}} (should be 2.9)
|-
! round
|| Rounds off the number on the left to the power of 1/10 given on the right
||{{evaldemo|#expr: 30/7 round 3|=}}<br />{{evaldemo|#expr: 30/7 round 0|=}}<br />{{<small> </small><code>#expr: 3456 round -2</code>}} = {{#expr: 3456 round -2}}
|}
Spaces around mod and round are good for readability but not needed for working properly:
*{{evd|#expr:7mod3}}
*{{evd|#expr:7.5round0}}
Precedence:
*{{evaldemo|#expr:1.234 + 1.234 round 1 + 1 }}
(first additions, then round)
*{{evaldemo|#expr:3 * 4 mod 10 * 10 }}
(mod and multiplication have equal precedence, evaluation from left to right)
To remind the reader of the precedence, one might write:
*{{evaldemo|#expr:1.234+1.234 round 1+1 }}
*{{evaldemo|#expr:2 + 3*4mod10*10 + 5}}
;When using spaces where there is precedence, the layout of the expression may be confusing:
:{{evaldemo|#expr:23+45 mod 10}}
;Instead one can write:
:{{evaldemo|#expr:23 + 45 mod10}}
;or simply use parentheses:
:{{evaldemo|#expr:23 + (45 mod 10)}}
==Mod==
To get a positive mod even for a negative number, use e.g. (700000 + x) mod7 instead of x mod7. The range of the result is now 0-6, provided that x > -700000.
Alternatively, use
*6 - ( 700006 - x ) mod7
or
*(x - 700006) mod7 + 6.
The range of the result is 0-6, provided that x < 700006.
Working for all x is:
*(x mod7 + 7) mod7
==Round==
*{{evd|#expr:2.5 round 0}}
*{{evd|#expr:-2.5 round 0}}
To round an integer plus one half for x > -100000 toward plus infinity, use:
*(x + 100000 round 0) - 100000
and to round an integer plus one half for x < 100000 toward minus infinity, use:
*(x - 100000 round 0) + 100000
To round x toward minus infinity, use:
*x + ( x != x round 0 ) * ( ( ( x - .5 ) round 0 ) - x )
and toward plus infinity
*x + ( x != x round 0 ) * ( ( ( x + .5 ) round 0 ) - x )
If x is a long expression this multiplies the length by 5! Under conditions for x there are alternatives:
To round x > -100000 toward minus infinity, use:
*(x - 100000.5 round 0) + 100000
and to round x < 100000 toward plus infinity, use:
*(x + 100000.5 round 0) - 100000
If x is a multiple of 1/n with n<1000 we can round toward minus infinity with:
*x - .499 round 0
For arbitrary n > 1 we can choose instead of -.499 any number between -.5 and -.5 + 1/n.
To find the largest multiple of 7 not larger than x (i.e. to round toward minus infinity to a multiple of 7) we can do:
*((x-3)/7 round 0) * 7
==See also==
*{{tim|mod}}
*[[m:Category:Mathematical templates]]
*[[w:en:Category:Mathematical templates]]
{{h:f|enname=Modulo and round}}