Open main menu

lensowiki β

Changes

Help:Modulo and round

3,550 bytes added, 02:42, 24 June 2006
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",&#160;remainder&#160;of&#160;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|&#61;}}<br />{{evaldemo|#expr: -8 mod -3|&#61;}}<br />{{evaldemo|#expr: -8 mod +3|&#61;}}<br />{{evaldemo|#expr: 8 mod 2.7|&#61;}} (should be 2.6)<br />{{evaldemo|#expr: 8 mod 3.2|&#61;}} (should be 1.6)<br />{{evaldemo|#expr: 8.9 mod 3|&#61;}} (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|&#61;}}<br />{{evaldemo|#expr: 30/7 round 0|&#61;}}<br />{&#123;<small>&#160;</small><code>#expr:&#160;3456&#160;round&#160;-2</code>&#125;}&#160;&#61;&#160;{{#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&#58;
:{{evaldemo|#expr:23+45 mod 10}}
;Instead one can write&#58;
:{{evaldemo|#expr:23 + 45 mod10}}
;or simply use parentheses&#58;
:{{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}}
Anonymous user