# 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:

• 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

## Trunc

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)}}` → -9223372036854775808
• `{{#expr:trunc(-2*2^63+2^12)}}` → -9223372036854775808
• `{{#expr:trunc(-1*2^63-2^12)}}` → -9223372036854775808
• `{{#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)}}` → 0
• `{{#expr:trunc(2^64+1024)}}` → 0
• `{{#expr:trunc(3*2^63-2^12)}}` → 0
• `{{#expr:trunc(3*2^63+2^12)}}` → 0

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

mod (modulo, PHP operator %): 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.[1]

`{{#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

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}}}}`Expression error: Division by zero
• Compare:
• `{{formatnum:{{#expr:-123 mod (2^64-2048)}}}}` → -123

### Former errors

Errors due to 32 bits servers do not occur anymore on Wikimedia wikis, since their servers are all 64 bits now. As a result we get the following correct results:

• `{{numf|123mod((trunc2)^trunc01-trunc1)}}` → 0
• `{{numf|123mod((trunc2)^trunc02-trunc1)}}` → 0
• `{{numf|123mod((trunc2)^trunc03-trunc1)}}` → 4
• `{{numf|123mod((trunc2)^trunc04-trunc1)}}` → 3
• `{{numf|123mod((trunc2)^trunc05-trunc1)}}` → 30
• `{{numf|123mod((trunc2)^trunc06-trunc1)}}` → 60
• `{{numf|123mod((trunc2)^trunc07-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc08-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc09-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc10-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc11-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc12-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc13-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc14-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc15-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc16-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc17-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc18-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc19-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc20-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc21-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc22-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc23-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc24-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc25-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc26-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc27-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc28-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc29-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc30-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc31-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc32-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc33-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc34-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc35-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc36-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc37-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc38-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc39-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc40-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc41-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc42-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc43-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc44-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc45-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc46-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc47-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc48-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc49-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc50-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc51-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc52-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc53-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc54-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc55-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc56-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc57-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc58-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc59-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc60-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc61-trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc62-trunc1)}}` → 123
• `{{numf|123mod(trunc(2^63-1024)+trunc1023)}}` → 123
• `{{numf|123mod((trunc2)^trunc00+trunc1)}}` → 1
• `{{numf|123mod((trunc2)^trunc01+trunc1)}}` → 0
• `{{numf|123mod((trunc2)^trunc02+trunc1)}}` → 3
• `{{numf|123mod((trunc2)^trunc03+trunc1)}}` → 6
• `{{numf|123mod((trunc2)^trunc04+trunc1)}}` → 4
• `{{numf|123mod((trunc2)^trunc05+trunc1)}}` → 24
• `{{numf|123mod((trunc2)^trunc06+trunc1)}}` → 58
• `{{numf|123mod((trunc2)^trunc07+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc08+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc09+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc10+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc11+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc12+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc13+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc14+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc15+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc16+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc17+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc18+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc19+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc20+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc21+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc22+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc23+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc24+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc25+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc26+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc27+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc28+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc29+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc30+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc31+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc32+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc33+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc34+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc35+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc36+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc37+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc38+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc39+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc40+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc41+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc42+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc43+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc44+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc45+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc46+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc47+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc48+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc49+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc50+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc51+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc52+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc53+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc54+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc55+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc56+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc57+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc58+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc59+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc60+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc61+trunc1)}}` → 123
• `{{numf|123mod((trunc2)^trunc62+trunc1)}}` → 123

See Help:Round.

## Floor and ceil

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,001
• `{{numf|{{ceil|trunc1e17+trunc1}}}}` → 100,000,000,000,000,001

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

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

• `{{#expr:1.234 + 1.234 round 1 + 1}}` gives 2.47 [7]

• `{{#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]
`{{#expr:23 + 45 mod10}}` gives 28 [10]
or use parentheses:
`{{#expr:23 + (45 mod 10)}}` gives 28 [11]

1. 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: · mw: · b: · s: · mw:Manual · Google
Versions of this help page (for other languages see below)
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary
What links here on Meta or from Meta · Wikipedia · MediaWiki
Go · Search · Stop words · 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 · User style
Editing
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Referencing