Help:Calculation
 The accuracy and format of numeric results varies with the server, even on a single Wikimedia page.
The MediaWiki extension ParserFunctions enables users to perform mathematical computations.
The expr function evaluates numerical expressions, and also boolean expressions involving numbers and booleans (not strings). The syntax is
 {{ #expr: expression }}
The spaces are not needed. Inside numbers no spaces are allowed.
The supported operators (roughly in order of precedence) are not, *, /, div, mod, +, , round, =, <>, !=, <=, >=, and, and or.
Contents 
[edit] Operators
Returns 0 or 1 for Booleans or true/false.
Operator  Operation  Examples  

none  none 


exp  Exponential function e^{x} 


ln  Natural logarithm 


abs  Absolute value 


trunc  Truncation 


floor  Floor function 


ceil  Ceiling function 


sin  Sine function 


cos  Cosine function 


tan  Tangent function 


asin  Arc sine function 


acos  Arc cosine function 


atan  Arc tangent function 


+  Unary + sign 


  Unary  sign (negation) 


not  Unary NOT, logical NOT 


^  Power 


*  Multiplication 


/  Division, same as div 


div  Division, same as /, not integer division 


mod  "Modulo", remainder of division after truncating both operands to an integer (PHP operator %). Caveat, div and mod are different from all programming languages. This has been fixed (but needs to be committed), see bugzilla:6068. 


+  Addition 


  Subtraction 


round  Rounds off the number on the left to the power of 1/10 given on the right (PHP function round) 


=  Equality (numerical incl. logical) 


<>  Inequality, same as != 


!=  Inequality, same as <>, logical xor 


<  Less than 


>  Greater than 


<=  Less than or equal to 


>=  Greater than or equal to 


and  Logical AND 


or  Logical OR 

The boolean operators consider 0 to be false and any other number to be true. An intermediate or final result "true" is identified with 1. Thus Template:Xpd0. Note that "and" and "or" work with #expr and #ifexpr only; for use with #if, #ifeq, and #ifexist, use 1 as thentext and 0 as elsetext, and combine results with "and" and "or" in an outer #expr or #ifexpr. Instead of {{ #expr: {{#if:{{{a}}}10}} or {{#if:{{{b}}}10}} }} we can also use {{#if:{{{a}}}{{{b}}}10}}}}. For negation, simply subtract from 1 or interchange then and elsepart.
Precedence:
(+ and  have equal precedence, * and / also, both higher than the former two).
(first +, then =, then <).
(first additions, then round)
(mod and multiplication have equal precedence, evaluation from left to right)
Parentheses can force a different precedence: Template:Xpd0
Blank spaces are good for readability but not needed for working properly, except between not and an adjacent and/div/mod/not/or/round operator, and within numbers not allowed:
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
[edit] Numbers as input
Leading zeros are allowed, as well as a trailing decimal point (for an integer) and trailing zeros in a number with a decimal point.
These equivalences are also relevant for #ifeq and #switch, see below.
In [1] we have
define( 'EXPR_NUMBER_CLASS', '0123456789.' ); ... elseif ( false !== strpos( EXPR_NUMBER_CLASS, $char ) ) { // Number if ( $expecting != 'expression' ) {throw new ExprError('unexpected_number');} // Find the rest of it $length = strspn( $expr, EXPR_NUMBER_CLASS, $p ); // Convert it to float, silently removing double decimal points $operands[] = floatval( substr( $expr, $p, $length ) ); $p += $length;$expecting = 'operator';continue;}
Thus the part of the expression representing a number is a sequence of digits and points; due to floatval a second point and any digits and points immediately after it are ignored, and do not give an error message. Group separators are not allowed: a comma is considered an unrecognised punctuation character:
Thus a number can only consist of:
 one or more digits, or
 zero or more digits, a point, and zero or more digits.
optionally followed by an power of 10 multiplier : the letter E (or e), an optional sign and digits (for the base10 exponent).
For the purpose of evaluating an expression a leading plus or minus sign is considered a unary operator instead of part of the number. The letter e or E is considered part of a number in scientific notation, or as Euler's constant, i.e. the base of natural logarithms.
Canonical form:
Accepted, although in some cases more or less odd:
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
Using Euler's constant e:
Wrong:
Due to the specifier R ("raw"), Template:Xpdoc etc., i.e. numbers without group separators, which can be used in computations.
As opposed to ParserFunctions, "Template:Ml" accepts points and commas in numbers and interprets them in a sitelanguagespecific way (depending on $separatorTransformTable in Messagesxx.php); on this site:
(on e.g. the German and the Dutch sites reversed w.r.t. the result on English sites).
However, this can be used instead of #ifeq, #ifexpr or #switch only for distinguishing a few sitelanguagespecific categories of numbers (for English: 1 and "not equal to 1", for French <=1 and >1, etc.). Also, since it is designed for linguistic distinction of singular and plural, in some languages the categories of numbers are numerically less useful.
[edit] Numbers as output
A noninteger result has a decimal point in it. Scientific notation is produced for numbers with small absolute value (for Wikimedia: less than 1E4) and for numbers with large absolute value (for Wikimedia: greater than or equal to 1E+12; however, even within Wikimedia there is no uniformity, larger numbers in nonscientific notation and smaller numbers in scientific notation have been observed as output; the format of the same number can vary from preview to view, and by refreshing the page!). Thus numbers produced include (depending on the operating system of the server):
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoc
For negative numbers simply a minus sign is prefixed:
 Long YYYYMMDDhhmmss timestamps can run into the same problem:
 Template:Xpdoc,
 Template:Xpdoocc.
 Timestamps without seconds are 12 digits, just short enough:
 Template:Xpdoc
To avoid these problems and also increase accuracy, unless the result is represented by #expr exactly and in nonscientific notation, such as a not too large integer, do not use #expr in an expression to be evaluated by #expr or #ifexpr. Typically the result of a computational template could be needed in a further computation, so at least one version of such a template needs to give the result in a form suitable for that, e.g. Template:Tim and Template:Tim; for convenience, for the final result another version can be made simply calling the other one and applying #expr to the result, e.g. Template:Tim.
Two methods for representing intermediate results are:
1. The expression resulting from processing the expressions themselves, using #expr for branching only.
For example, Template:Tim produces the power of a number in the form of an expression:
However, the expression can get quite long, for Template:Tim see Help:Calculation/example 1. Although long expressions are allowed, they give rise to a large postexpand include size and template argument size, for which limits exist.
Applying #expr directly on the linked expression gives Expression error: Unrecognised punctuation character "[", but it can also be used in a further computation, unlike the value just mentioned.
2. Writing numbers splitting off factors which are powers of 10 but written plainly.
Example:
Intermediate results are also in such a form, preventing the growth of the expression like above. Again, #expr can be applied directly, as done in Template:Tim, giving Template:Fineval, but the result can also be used in a further computation.
Compare:
This uses the same method, but with intermediate application of #expr, giving an error because of internediate results in scientific notation.
If the result of rounding a negative number is zero, the result is "0". To avoid that, an expression x can be replaced by 0 + ( x ):
The number output is suitable for many other calculation programs, also the scientific notation. In that sense output like 6E23 is more convenient than 6Template:E.
[edit] Accuracy
 The accuracy and format of numeric results varies with the operating system of the server. Some remarks in this section are specific for Wikimedia.
Accuracy of the result of #expr is less than internally used within the computation of an expression:
 Template:Xpdoc
 Template:Xpdoc
 Template:Xpdoocc
 Template:Xpdoc (conclusion: 16 or 17 digits of 1234567890.1234567890 used)
 Template:Xpdoc (result is rounded to a total of 12 digits)
 Template:Xpdoocc
 The macheps or smallest x such that 1+x != 1 appears to be near 0.5^53 + 0.5^106 or 1.1102230246251566636831481088739149080825883E16 (see below):
 Template:Xpdoc
 Template:Xpdoc
 That's the normal behaviour for 64=1+52+11 bits (52 mantissa, 11 exponent). As explained above it is unrelated to the smallest expression result greater than 1:
 Template:Xpdoc
 Template:Xpdoc below about 1 + 5E12.
[edit] Branching depending on an expression
The function #ifexpr produces one of two specified results, depending on the value of a boolean expression involving numbers and booleans (not strings). Examples:
 {{#ifexpr: {{CURRENTDOW}} = 0 or {{CURRENTDOW}} = 6  weekEND  weekDAY}} yields weekDAY because today is Tuesday and so Template:Xpd0.
 Template:Xpd2
Note that rounding errors can affect a result:
Similarly:
[edit] Comparisons
 The function #ifeq: compares numbers and strings for equality (equal if both represent the same number or both are equal strings).
 The function #switch: compares one string with multiple others, and correspondingly produces one of multiple specified results.
 Template:Xpdopepepec
 Template:Xpd3
 Template:Xpd3
 Template:Xpd3
 Note that #ifeq: (like now #expr:) accepts exponential notation on input.
 Template:Xpd3
 Template:Xpd3
 Template:Xpd3
 Template:Xpd3
Numerical comparisons don't depend on the output format, compare:
#ifeq: allows to compare strings containing equal signs:
#switch:  #ifeq: 

Template:Xpd  Template:Xpd 
Template:Xpd  Template:Xpd 
Template:Xpd  Template:Xpd 
[edit] Length of expressions
To find the absolute value of a numeric expression x without using a separate template at least doubles the length of the expression:
 x*(12*(x<0))
 x*{{#ifexpr:x>011}}
(The first is not only shorter but has also the advantage that for substitution one less "subst:" or {{{subst}}} is needed.)
Do not use
 {{#ifexpr:x>0xx}}
for long expressions as it triples the length.
Similarly do not use mod to round or conversely, because it doubles the length of the expression.
Also providing a leading zero for the result of an expression if it is less than 10 doubles its length:
 {{#ifexpr:x<100}}x
This "exponential" growth of expressions, with much repetition, is due to the lack of variables (in the computer programming sense); however, see also VariablesExtension.
Templates (subroutines) provide some of the functionality that variables offer: a template name is comparable with a variable name, while its content is comparable with the value of the variable. Alternatively, a template parameter can be assigned a value which can be used for multiple occurrences of the same parameter in the template. Thus e.g. x*(12*(x<0)) with a long expression x can be evaluated avoiding duplication of that expression, in two ways:
 put the expression x in a template (multiple calls of the same template)
 put x*(12*(x<0)) in a template with parameter x (use of a template with multiple occurrences of the same parameter)
If the number of possible results of a long expression is small, a switch allows arbitrary conversion, including the absolute value and providing a leading zero, etc., without repeating the expression.
[edit] Error messages
Examples for all known #expr: and #ifexpr: error messages. The error texts are within the tags <strong class="error">..</strong>.
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
Template:Xpd1  
{{ #expr:{{ x1021000*}} 18 }} gives 1.8E+307  
{{ #expr:{{ x1021000*}} 179 }} gives 1.79E+308  
{{ #expr:{{ x1021000*}} 180 }} gives INF (on Wikimedia "INF", but depending on the operating system of the server it may also be e.g. "1.#INF")  
{{ #expr:{{ x33(1+(}} 1 {{ x33))}} }} gives Expression error: Unclosed bracket  
{{ #expr:{{ x34(1+(}} 1 {{ x34))}} }} gives Expression error: Stack exhausted  
Template:Xpd0 (no feature, only an oddity) 
Note: Template:Linkssmall copies a given string, here parts of an expression, for the specified times (max. 120), this help page shown on other projects actually evaluate its substituted output like 102 factors "1000" times "180" to get INF (infinity).
Wikitext without error message from the parser functions, but typically an error while using or attempting to use them:
{{{#expr:2*3}}}  {{{#expr:2*3}}}  (triple braces, the whole is interpreted as parameter tag with parameter name "#expr:2*3") 
{{#expr:2*3}}}  6}  (one closing brace too many; the last of the three is interpreted as plain text, so that the rest works fine) 
{{{#expr:2*3}}  {6  (one opening brace too many; the first of the three is interpreted as plain text, so that the rest works fine) 
{{#expr:2*3}  {{#expr:2*3}  (too few braces, the whole is interpreted as plain text) 
 A crude but informative "unrecognised word" error message can be generated intentionally. Only the first identified error is shown:
 {{ #expr: 2*{{ #ifexpr: 3*4>10toolarge3*4 }} }} gives
 Expression error: Unrecognised word "toolarge",
 {{ #expr: 2*{{ #ifexpr: 3*4>10too large3*4 }} }} gives
 Expression error: Unrecognised word "too".
See also #iferror.
[edit] Minus sign
Only the Template:Mlw character or Template:Mlw character, typed directly, work as a minus sign operator in expressions.
 The HTML character references (by name or by numeric code point value) are not recognized when evaluating expressions: numerical character references are converted only when generating the final HTML document (after expansion of templates and parser functions)
 Only a handful of character references by name are substituted early by MediaWiki, all others are interpreted only by the browser.
 The other dash characters (such as the hyphen, the figure dash, Template:Mlw, Template:Mlw and others), though often similar visually, are not valid minus signs, but punctuation signs or typographical variants.
Template:Mlw, typed directly as the character '' (U+002D)  Template:Xpd0  
hyphenminus, typed as the numerical character reference   "{{#expr:12}}"

"Expression error: Unrecognised punctuation character "&""  [2] 
hyphenminus, typed as the numerical character reference   "{{#expr:12}}"

"Expression error: Unrecognised punctuation character "&""  [3] 
Template:Mlw, typed directly as the character '−' (U+2212)  Template:Xpd0  
minus sign, typed as the numerical character reference −  "{{#expr:−12}}"

"Expression error: Unrecognised punctuation character "&""  [4] 
minus sign, typed as the numerical character reference −  "{{#expr:−12}}"

"Expression error: Unrecognised punctuation character "&""  [5] 
minus sign, typed as the symbolic character reference −  "{{#expr:−12}}"

"12"  [6] 
figure dash, typed directly as the character '‒' (U+2012)  Template:Xpd0  
figure dash, typed as the numerical character reference ‒  "{{#expr:‒12}}"

"Expression error: Unrecognised punctuation character "&""  [7] 
figure dash, typed as the numerical character reference ‒  "{{#expr:‒12}}"

"Expression error: Unrecognised punctuation character "&""  [8] 
Template:Mlw, typed directly as the character '–' (U+2013)  Template:Xpd0  
en dash, typed as the numerical character reference –  "{{#expr:–12}}"

"Expression error: Unrecognised punctuation character "&""  [9] 
en dash, typed as the numerical character reference –  "{{#expr:–12}}"

"Expression error: Unrecognised punctuation character "&""  [10] 
en dash, typed as the symbolic character reference –  "{{#expr:–12}}"

"Expression error: Unrecognised punctuation character "&""  [11] 
Also many other calculation programs require a hyphen. Therefore, in order to be able to copy rendered numbers and expressions to the edit box or input them through a copy operation into other calculation programs, displayed minus signs also need to be hyphens.
[edit] Displaying numbers and numeric expressions
Guidelines such as Template:Mlww focus on number display as end product. However, a point of consideration can also be the possibility to apply the rendered output to #expr or #ifexpr, or to input it without conversion into other calculation programs. This would require the following:
 use digits, not words
 as mentioned above, use the hyphen as minus sign
 use *, <=, and >=, not ×, ≤, or ≥
 do not use thousands separators (however, some programs allow them)
 use output like 6E23 or 6e23 rather than 6Template:E
Examples:
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
 Template:Xpd0
If the number is the result of a computation by MediaWiki and unsuitable for use in a new computation due to application of a formatting function such as #formatnum or a formatting template, one can copy the wikitext and apply the additional computation before the formatting. However, when templates are used, and copying is done to another wiki, these templates have to be copied too, or substituted.
If you want to calculate with Magic words and return group seperated results you can use formatnum:
{{formatnum: {{#expr: {{NUMBEROFPAGES:R}}  {{NUMBEROFFILES:R}} }} }}
= 1,225 (instead of 1225).