Help:Modulo and round

The MediaWiki extension ParserFunctions enables users to perform simple mathematical computations.


 * 1) expr and #ifexpr allow mod and round.

Problem with mod in Windows:
 * (should be -147483650; on Windows 147483646)
 * (should be -147483649; on Windows 147483647)
 * (should be -147483648; on Windows correct)
 * (should be -147483647; on Windows correct)
 * (should be 147483646; on Windows correct)
 * (should be 147483647; on Windows correct)
 * (should be 147483648; on Windows -147483648)
 * (should be 147483649; on Windows -147483647)

See also , and operator fmod in mw:Extension:ParserFunctions (extended), which both do not have these problems. Also, in file expr.php (the standard one or the extended one) operation "$left % $right" can be replaced by "fmod($left,$right)".

Spaces around mod and round are good for readability but not needed for working properly:

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&#58;


 * Instead one can write&#58;


 * or simply use parentheses&#58;

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 or The range of the result is 0-6, provided that x < 700006.
 * 6 - ( 700006 - x ) mod7
 * (x - 700006) mod7 + 6.

Working for all x is:
 * (x mod7 + 7) mod7

Round




To round an integer plus one half for x > -100000 toward plus infinity, use: and to round an integer plus one half for x < 100000 toward minus infinity, use:
 * (x + 100000 round 0) - 100000
 * (x - 100000 round 0) + 100000

To round x toward minus infinity, use: and toward plus infinity
 * x + ( x != x round 0 ) * ( ( ( x - .5 ) round 0 ) - x )
 * 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: and to round x < 100000 toward plus infinity, use:
 * (x - 100000.5 round 0) + 100000
 * (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