Help:Modulo and round
From Eugene Neighbors
{{#if: Help|{{#if:|Other languages:}} Template:H-langs:
{{#if: Help||}}|{{#if:|Other languages:}} Template:H-langs: {{#if: Help||}}}}
The MediaWiki extension ParserFunctions enables users to perform simple mathematical computations.
#expr and #ifexpr allow mod and round.
| Operator | Operation | Example |
|---|---|---|
| mod | "Modulo" (PHP operator %): if a is nonnegative, a mod b is the remainder by division after truncating both operands to an integer, while (-a) mod b = - ( a mod b). Caveats:
| {{ #expr: 30 mod 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 mod 7}}{{ #expr: -8 mod -3}} {{#ifeq: =|=|=|gives}} {{#expr: -8 mod -3}}{{ #expr: -8 mod +3}} {{#ifeq: =|=|=|gives}} {{#expr: -8 mod +3}}{{ #expr: 8 mod 2.7}} {{#ifeq: =|=|=|gives}} {{#expr: 8 mod 2.7}} (should be 2.6){{ #expr: 8 mod 3.2}} {{#ifeq: =|=|=|gives}} {{#expr: 8 mod 3.2}} (should be 1.6){{ #expr: 8.9 mod 3}} {{#ifeq: =|=|=|gives}} {{#expr: 8.9 mod 3}} (should be 2.9)
|
| round | (PHP function round): rounds the number on the left to the nearest multiple of 1/10 raised to the power given on the right; if two are equally near, rounding is away from zero. | {{ #expr: 30/7 round 3}} {{#ifeq: =|=|=|gives}} {{#expr: 30/7 round 3}}{{ #expr: 30/7 round 0}} {{#ifeq: =|=|=|gives}} {{#expr: 30/7 round 0}}{{ #expr: 3456 round -2}} = {{#expr: 3456 round -2}}
|
Problem with mod in Windows:
- {{
#expr:-2147483650 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2147483650 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:-2147483650 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:-2147483650 mod 1000000000|}}|{{#expr:-2147483650 mod 1000000000}}}}}}}} (should be -147483650; on Windows 147483646) - {{
#expr:-2147483649 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2147483649 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:-2147483649 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:-2147483649 mod 1000000000|}}|{{#expr:-2147483649 mod 1000000000}}}}}}}} (should be -147483649; on Windows 147483647) - {{
#expr:-2147483648 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2147483648 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:-2147483648 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:-2147483648 mod 1000000000|}}|{{#expr:-2147483648 mod 1000000000}}}}}}}} (should be -147483648; on Windows correct) - {{
#expr:-2147483647 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2147483647 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:-2147483647 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:-2147483647 mod 1000000000|}}|{{#expr:-2147483647 mod 1000000000}}}}}}}} (should be -147483647; on Windows correct) - {{
#expr:2147483646 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2147483646 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:2147483646 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:2147483646 mod 1000000000|}}|{{#expr:2147483646 mod 1000000000}}}}}}}} (should be 147483646; on Windows correct) - {{
#expr:2147483647 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2147483647 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:2147483647 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:2147483647 mod 1000000000|}}|{{#expr:2147483647 mod 1000000000}}}}}}}} (should be 147483647; on Windows correct) - {{
#expr:2147483648 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2147483648 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:2147483648 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:2147483648 mod 1000000000|}}|{{#expr:2147483648 mod 1000000000}}}}}}}} (should be 147483648; on Windows -147483648) - {{
#expr:2147483649 mod 1000000000{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2147483649 mod 1000000000|||}}|{{#ifeq:-|+|{{#expr:2147483649 mod 1000000000||}}|{{#ifeq:-|+|{{#expr:2147483649 mod 1000000000|}}|{{#expr:2147483649 mod 1000000000}}}}}}}} (should be 147483649; on Windows -147483647)
See also Template:Links-core, 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:
- {{
#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:
- {{
#expr:1.234 + 1.234 round 1 + 1}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:1.234 + 1.234 round 1 + 1 }}
(first additions, then round)
- {{
#expr:3 * 4 mod 10 * 10}} {{#ifeq: +|-|{{{2}}}|gives}} {{#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:
- {{
#expr:1.234+1.234 round 1+1}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:1.234+1.234 round 1+1 }} - {{
#expr:2 + 3*4mod10*10 + 5}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:2 + 3*4mod10*10 + 5}}
- When using spaces where there is precedence, the layout of the expression may be confusing:
- {{
#expr:23+45 mod 10}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:23+45 mod 10}} - Instead one can write:
- {{
#expr:23 + 45 mod10}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:23 + 45 mod10}} - or simply use parentheses:
- {{
#expr:23 + (45 mod 10)}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:23 + (45 mod 10)}}
Contents |
[edit] 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
[edit] 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:1.5 round 0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:1.5 round 0|||}}|{{#ifeq:-|+|{{#expr:1.5 round 0||}}|{{#ifeq:-|+|{{#expr:1.5 round 0|}}|{{#expr:1.5 round 0}}}}}}}} - {{
#expr:.5 round 0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:.5 round 0|||}}|{{#ifeq:-|+|{{#expr:.5 round 0||}}|{{#ifeq:-|+|{{#expr:.5 round 0|}}|{{#expr:.5 round 0}}}}}}}} - {{
#expr:-.5 round 0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-.5 round 0|||}}|{{#ifeq:-|+|{{#expr:-.5 round 0||}}|{{#ifeq:-|+|{{#expr:-.5 round 0|}}|{{#expr:-.5 round 0}}}}}}}} - {{
#expr:-1.5 round 0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-1.5 round 0|||}}|{{#ifeq:-|+|{{#expr:-1.5 round 0||}}|{{#ifeq:-|+|{{#expr:-1.5 round 0|}}|{{#expr:-1.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}}}}}}}}
- {{
#expr:2.25 round 1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2.25 round 1|||}}|{{#ifeq:-|+|{{#expr:2.25 round 1||}}|{{#ifeq:-|+|{{#expr:2.25 round 1|}}|{{#expr:2.25 round 1}}}}}}}} - {{
#expr:2.15 round 1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2.15 round 1|||}}|{{#ifeq:-|+|{{#expr:2.15 round 1||}}|{{#ifeq:-|+|{{#expr:2.15 round 1|}}|{{#expr:2.15 round 1}}}}}}}} - {{
#expr:2.05 round 1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2.05 round 1|||}}|{{#ifeq:-|+|{{#expr:2.05 round 1||}}|{{#ifeq:-|+|{{#expr:2.05 round 1|}}|{{#expr:2.05 round 1}}}}}}}} - {{
#expr:-2.05 round 1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2.05 round 1|||}}|{{#ifeq:-|+|{{#expr:-2.05 round 1||}}|{{#ifeq:-|+|{{#expr:-2.05 round 1|}}|{{#expr:-2.05 round 1}}}}}}}} - {{
#expr:-2.15 round 1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2.15 round 1|||}}|{{#ifeq:-|+|{{#expr:-2.15 round 1||}}|{{#ifeq:-|+|{{#expr:-2.15 round 1|}}|{{#expr:-2.15 round 1}}}}}}}} - {{
#expr:-2.25 round 1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-2.25 round 1|||}}|{{#ifeq:-|+|{{#expr:-2.25 round 1||}}|{{#ifeq:-|+|{{#expr:-2.25 round 1|}}|{{#expr:-2.25 round 1}}}}}}}}
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
[edit] See also
{{#if: Help|{{#if: Help|Template:Ph:Modulo and round}}
[edit] Links to other help pages
- Help contents (all help pages)
- Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s:
- Versions of this help page (for other languages see below)
- Meta | Wikinews | {{#if:|{{#if:Wikipedia|Wikipedia|w:Help:Modulo_and_round#}}|{{#if:Wikipedia|Wikipedia|w:Help:Modulo_and_round}}}} | Wikiquote | Wiktionary
- Links to this page from Meta (int) Meta (ext) Wikipedia MediaWiki
- Reading
- Go | Search | Stop words | URL | Namespace | Page name | Section
- Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
- Logging in and preferences
- Logging in | Preferences | User style
- Editing
- Advanced editing | Editing FAQ | Editing toolbar | Export | Import
- Editing tips and tricks | Editing shortcuts
- Tracking changes
- Recent changes (enhanced) | Related changes | Watching pages | Diff
- Page history | Edit summary | User contributions | Minor edit | Patrolled edit
- Style & formatting
- Wikitext examples | Wikitext reference | Reference card
- HTML in wikitext | HTML elements
- List | Table | Sorting
- Special input and output
- Inputbox | Special characters | Displaying a formula | Images and other uploaded files | EasyTimeline
- Advanced functioning
- Template | Advanced templates | Parser function | ParserFunctions | Parameter default | Variable | Magic word | System message | Substitution
- Array | Calculation
- Page management
- Starting a new page | Renaming (moving) a page | Maintenance | Merging and moving pages | Protecting pages
- Resolving disputes | Deleting a page
- Special pages
- Talk page | Testing | Sandbox
- Lists of resources
- Lists of Categories | Copyrights | Infobox
- Redirect | Reference Desk | Shortcuts | Stub types
Other languages: Template:H-langs:Modulo and round |{{#if: Help|Template:Ph:Modulo and round}}
[edit] Links to other help pages
- Help contents (all help pages)
- Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s:
- Versions of this help page (for other languages see below)
- Meta | Wikinews | {{#if:|{{#if:Wikipedia|Wikipedia|w:Help:Modulo_and_round#}}|{{#if:Wikipedia|Wikipedia|w:Help:Modulo_and_round}}}} | Wikiquote | Wiktionary
- Links to this page from Meta (int) Meta (ext) Wikipedia MediaWiki
- Reading
- Go | Search | Stop words | URL | Namespace | Page name | Section
- Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
- Logging in and preferences
- Logging in | Preferences | User style
- Editing
- Advanced editing | Editing FAQ | Editing toolbar | Export | Import
- Editing tips and tricks | Editing shortcuts
- Tracking changes
- Recent changes (enhanced) | Related changes | Watching pages | Diff
- Page history | Edit summary | User contributions | Minor edit | Patrolled edit
- Style & formatting
- Wikitext examples | Wikitext reference | Reference card
- HTML in wikitext | HTML elements
- List | Table | Sorting
- Special input and output
- Inputbox | Special characters | Displaying a formula | Images and other uploaded files | EasyTimeline
- Advanced functioning
- Template | Advanced templates | Parser function | ParserFunctions | Parameter default | Variable | Magic word | System message | Substitution
- Array | Calculation
- Page management
- Starting a new page | Renaming (moving) a page | Maintenance | Merging and moving pages | Protecting pages
- Resolving disputes | Deleting a page
- Special pages
- Talk page | Testing | Sandbox
- Lists of resources
- Lists of Categories | Copyrights | Infobox
- Redirect | Reference Desk | Shortcuts | Stub types
Other languages: Template:H-langs:Modulo and round }}
