Help:Modulo and round
From lensowiki
The MediaWiki extension ParserFunctions enables users to perform simple mathematical computations.
#expr and #ifexpr allow mod and round.
| Operator | Operation | Example |
|---|---|---|
| mod | "Modulo", remainder of division after truncating both operands to an integer. Caveat, mod is different from all programming languages. This has been fixed (but needs to be committed), see bugzilla:6068. | Template:Evaldemo Template:Evaldemo Template:Evaldemo Template:Evaldemo (should be 2.6) Template:Evaldemo (should be 1.6) Template:Evaldemo (should be 2.9) |
| round | Rounds off the number on the left to the power of 1/10 given on the right | Template:Evaldemo Template:Evaldemo {{ #expr: 3456 round -2}} = {{#expr: 3456 round -2}}
|
Spaces around mod and round are good for readability but not needed for working properly:
- {{
#expr:7mod3{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:7mod3|||}}|{{#ifeq:-|+|{{#expr:7mod3||}}|{{#ifeq:-|+|{{#expr:7mod3|}}|{{#expr:7mod3}}}}}}}} - {{
#expr:7.5round0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:7.5round0|||}}|{{#ifeq:-|+|{{#expr:7.5round0||}}|{{#ifeq:-|+|{{#expr:7.5round0|}}|{{#expr:7.5round0}}}}}}}}
Precedence:
(first additions, then round)
(mod and multiplication have equal precedence, evaluation from left to right)
To remind the reader of the precedence, one might write:
- When using spaces where there is precedence, the layout of the expression may be confusing:
- Template:Evaldemo
- Instead one can write:
- Template:Evaldemo
- or simply use parentheses:
- Template:Evaldemo
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
- {{
#expr:2.5 round 0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2.5 round 0|||}}|{{#ifeq:-|+|{{#expr:2.5 round 0||}}|{{#ifeq:-|+|{{#expr:2.5 round 0|}}|{{#expr:2.5 round 0}}}}}}}} - {{
#expr:-2.5 round 0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2.5 round 0|||}}|{{#ifeq:-|+|{{#expr:-2.5 round 0||}}|{{#ifeq:-|+|{{#expr:-2.5 round 0|}}|{{#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
- {{#if:1|[[{{#if:|template}}:{{#if:|mod|template}}:{{#if:|foo|mod}}]]}} ( {{#if:1|[[{{#if:|template}}:{{#if:|mod|template}} talk:{{#if:|foo|mod}}|talk]]}} {{#if:1|[{{fullurl:{{#if:|template}}:{{#if:|mod|template}}:{{#if:|foo|mod}}|action=edit}} edit]}} {{#if:|[{{fullurl:{{#if:|template}}:{{#if:|mod|template}}:{{#if:|foo|mod}}|action=watch}} watch]}} {{#if:|{{#ifeq:{{#if:|mod|template}}|user|[[{{#if:|template}}:Special:Contributions/{{#titleparts:{{#if:|foo|mod}}|1}}|contribs]]}}}} {{#if:1|[{{fullurl:{{#if:|template}}:{{#if:|mod|template}}:{{#if:|foo|mod}}|action=history}} history]}} {{#if:1|[{{fullurl:{{#if:|template}}:Special:Whatlinkshere/{{#if:|mod|template}}:{{#if:|foo|mod}}|limit=1000}} links]}} {{#if:|[{{fullurl:{{#if:|template}}:Special:Log|page=%7B%7B%23if%3A%7Cmod%7Ctemplate%7D%7D%3A%7B%7B%23if%3A%7Cfoo%7Cmod%7D%7D}} logs]}} {{#if:|[[{{#if:|template}}:Special:Prefixindex/{{#if:|mod|template}}:{{#if:|foo|mod}}|localspace]]}} {{#if:|[{{fullurl:{{#if:|template}}:{{#if:|mod|template}}:{{#if:|foo|mod}}|action=delete}} delete]}} {{#if:|{{#ifeq:{{#if:|mod|template}}|user|[[{{#if:|template}}:Special:Blockip/{{#titleparts:{{#if:|foo|mod}}|1}}|block]]}}}} )
- m:Category:Mathematical templates
- w:en:Category:Mathematical templates
http://www.fayeunrauphotography.com/ kjøp cialis 520853 http://www.primetermites.com/ ジェネリックバイアグラ 5575 http://www.witch-ring.com/Generic-Viagra/ Comprando viagra sfnjy http://www.flweaver.com/ online Cialis kmt http://www.blogdemoteros.com/ tadalafil 5271