Help:Mod, round, floor, ceil, trunc
The MediaWiki extension ParserFunctions enables users to perform simple mathematical computations; #expr and #ifexpr allow various discontinuous functions.
Uniform w.r.t. 0, non-strict monotonic:[1]
- ceil
- floor
Symmetric w.r.t. 0, on each side of 0 non-strict monotonic:
- trunc
- round
Otherwise related to 0, on each side of 0 periodic:
- mod
See also Help:Mod, round, floor, ceil, trunc/table.
Trunc[edit]
Trunc converts a float to a number of integer type by truncation of the fraction. For 2^63 <= x <= 2^64 we get x - 2^64, for larger x we get 0; for x < -2^63 we get -2^63.
For example:
{{#expr:trunc(-2*2^63-2^12)}}
→ -4096{{#expr:trunc(-2*2^63+2^12)}}
→ 4096{{#expr:trunc(-1*2^63-2^12)}}
→ 9223372036854771712{{#expr:trunc(-1*2^63+2^12)}}
→ -9223372036854771712{{#expr:trunc(0*2^63-2^12)}}
→ -4096{{#expr:trunc(0*2^63+2^12)}}
→ 4096{{#expr:trunc(1*2^63-2^12)}}
→ 9223372036854771712{{#expr:trunc(1*2^63+2^12)}}
→ -9223372036854771712{{#expr:trunc(2*2^63-2^12)}}
→ -4096{{#expr:trunc(2*2^63+2^12)}}
→ 4096{{#expr:trunc(2^64+1024)}}
→ 0{{#expr:trunc(3*2^63-2^12)}}
→ 9223372036854771712{{#expr:trunc(3*2^63+2^12)}}
→ -9223372036854771712
Conversion to a number of integer type is useful because the result, and subsequent results of type integer, are displayed exactly, instead of being rounded to 14 digits.
x = trunc x if and only if x is of type integer or a float with a value that a number of type integer also can have.
Mod[edit]
mod (modulo, PHP operator %)[2]: if a is nonnegative, a mod b is the remainder by division after truncating both operands to an integer (more accurately: apply the trunc function), while (-a) mod b = - ( a mod b) and a mod (-b) = a mod b.[3]
{{#expr: 30 mod 7}}
gives 2 [1]{{#expr: -8 mod -3}}
gives -2 [2]{{#expr: -8 mod +3}}
gives -2 [3]{{#expr: 8 mod 2.7}}
gives 0 [4] (elsewhere 2.6){{#expr: 8 mod 3.2}}
gives 2 [5] (elsewhere 1.6){{#expr: 8.9 mod 3}}
gives 2 [6] (elsewhere 2.9)
To get a positive mod even for a negative number, use "(p mod q + q) mod q" instead of "p mod q".
Rounding to a float before applying mod can be avoided by constructing a type-integer form of the number:
{{#expr:(1e16+1)mod3}}
→ 1{{#expr:(trunc1e16+trunc1)mod3}}
→ 2
For large integers there used to be errors, Template:Modint was used as an alternative.
Details[edit]
p mod 0 gives an error message (produced by MediaWiki, not php operator %):
{{#expr:-27mod0}}
→ Expression error: Division by zero
Otherwise php operator % is applied. This involves applying the trunc function to the arguments first. The result of p % 0 is the empty string; this applies for 0 < |q| < 1 and q >= 2^64:
{{formatnum:{{#expr:-123 mod .9}}}}
→ Expression error: Division by zero{{formatnum:{{#expr:-123 mod -.9}}}}
→ Expression error: Division by zero{{formatnum:{{#expr:-123 mod (2^64)}}}}
→ Expression error: Division by zero{{formatnum:{{#expr:-123 mod 1e20}}}}
→ −123- Compare:
{{formatnum:{{#expr:-123 mod (2^64-2048)}}}}
→ −123
Round[edit]
See Help:Round.
Floor and ceil[edit]
When the functions floor and ceil are applied to an integer-type number, this number is first rounded to float (in the general way, not specifically downward for floor or upward for ceil) before applying the mathematical function:
{{numf|floor (trunc1e17+trunc1)}}
→ 100,000,000,000,000,000{{numf|ceil (trunc1e17+trunc1)}}
→ 100,000,000,000,000,000
Use Template:Floor and Template:Ceil to get in such a case the integer-type expression for the exact result:
{{numf|{{floor|trunc1e17+trunc1}}}}
→ 100,000,000,000,000,000{{numf|{{ceil|trunc1e17+trunc1}}}}
→ 100,000,000,000,000,000
To check whether the internal result of an expression x is mathematically an integer one can simply test "(x) = floor (x)", or similarly with ceil (not with trunc because for large floats we would get false negatives, and not with round0, because for odd numbers between 2^52 and 2^53 we would get false negatives).
Safety margins[edit]
To round an integer x down to a multiple of 7 we can do one of the following:
- 7*((x-3)/7 round 0)
- 7*floor((x+.5)/7)
- 7*ceil((x-6.5)/7)
and to round x to the nearest multiple of 7 we can do one of the following:
- 7*((x/7) round 0)
- 7*floor((x+3.5)/7)
- 7*ceil((x-3.5)/7)
In these cases the position with respect to 0 (the sign of x) is not important, even for round, because the value to be rounded is never halfway between integers. All methods provide the same generous safety margin of .5 in x, or 1/14 after division by 7, for rounding errors.
Note that for arbitrary real x the difference between the two problems corresponds to a shift in x of 3.5, while here, with integers x, the shift is 3.
Precedence[edit]
{{#expr:1.234 + 1.234 round 1 + 1}}
gives 2.47 [7]
(first additions, then round)
{{#expr:3 * 4 mod 10 * 10}}
gives 20 [8]
(mod and multiplication have equal precedence, evaluation from left to right)
- When using spaces where there is precedence, the layout of the expression may be confusing:
{{#expr:23+45 mod 10}}
gives 28 [9]- Instead one can write:
{{#expr:23 + 45 mod10}}
gives 28 [10]- or use parentheses:
{{#expr:23 + (45 mod 10)}}
gives 28 [11]
See also[edit]
- ↑ Estimation of a discrete monotone distribution
- ↑ https://www.w3schools.com/php/php_operators.asp
- ↑ mod with non-integer arguments is different from all programming languages, see bugzilla:6068 (marked as won't fix).
Links to other help pages
- Help contents
- Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
- Versions of this help page (for other languages see further)
- What links here on Meta or from Meta · Wikipedia · MediaWiki
- Reading
- Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
- Tracking changes
- Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
- Logging in and preferences
- Logging in · Preferences
- Editing
- Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
- Referencing
- Links · URL · · Footnotes
- Style and formatting
- Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
- Fixing mistakes
- Show preview · Reverting edits
- Advanced functioning
- Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
- Others
- Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors