Help:Calculation
 The accuracy and format of numeric results varies with the server; even on a single page, while for any rendering there is consistency within the page, there is no consistency among renderings.
The MediaWiki extension ParserFunctions enables users to perform simple 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 shown in the syntax above are not required, but spaces within numbers are not allowed.
The supported operators (roughly in order of precedence) are not, *, /, div, mod, +, , round, =, <>, !=, <=, >=, and, and or.
Contents
Operators[edit]
Operator  Operation  Examples  

none  none 
Wrong:


exp  e to the specified power 
Compare:


ln  Natural logarithm 
Hence, the base ten logarithm of e.g. 2:


abs 


trunc 


floor 


ceil 


sin 
With an angle in degrees, e.g. 30°:


cos 


tan 


asin 


acos 


atan 


+  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 "{{#expr: (2 < 3) + 1}}"
gives "2" [90]. 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:
"{{#expr: 2  3 + 4 / 5 * 6}}"
gives "3.8" [91]
(+ and  have equal precedence, * and / also, both higher than the former two).
"{{#expr: 2 = 5 < 3 + 4}}"
gives "1" [92]
(first +, then =, then <).
"{{#expr:1.234 + 1.234 round 1 + 1}}"
gives "2.47" [93]
(first additions, then round)
"{{#expr:3 * 4 mod 10 * 10}}"
gives "20" [94]
(mod and multiplication have equal precedence, evaluation from left to right)
Parentheses can force a different precedence: "{{#expr: (2 + 3) * 4}}"
gives "20" [95]
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:
"{{#expr:7mod3}}"
gives "1" [96]"{{#expr:7.5round0}}"
gives "8" [97]"{{#expr:0and1}}"
gives "0" [98]"{{#expr:0or not0}}"
gives "1" [99]"{{#expr:0ornot0}}"
gives "Expression error: Unrecognized word "ornot"." [100]"{{#expr:123 456}}"
gives "Expression error: Unexpected number." [101]"{{#expr:not not3}}"
gives "1" [102]"{{#expr:notnot3}}"
gives "Expression error: Unrecognized word "notnot"." [103]
"{{#expr:2}}"
gives "2" [104]"{{#expr:+2}}"
gives "2" [105]"{{#expr:2*3}}"
gives "6" [106]"{{#expr:notnotnot0}}"
gives "1" [107]"{{#expr:2*/3}}"
gives "Expression error: Unexpected / operator." [108]"{{#expr:sinln1.1}}"
gives "Expression error: Unrecognized word "sinln"." [109]"{{#expr:sin ln1.1}}"
gives "0.095165945236752" [110]
Numbers as input[edit]
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 [113] 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. (For the purpose of evaluating an expression a plus or minus sign is considered a unary operator instead of part of the number.)
Canonical form:
"{{#expr: 123}}"
gives "123" [114]"{{#expr: 123.456}}"
gives "123.456" [115]"{{#expr: 0.456}}"
gives "0.456" [116]"{{#expr: 0}}"
gives "0" [117]"{{#expr: 2.0E5}}"
gives "2.0E5" [118]
Accepted, although in some cases more or less odd:
"{{#expr: .456}}"
gives "0.456" [119]"{{#expr: 123.}}"
gives "123" [120]"{{#expr: 000123}}"
gives "123" [121]"{{#expr: 123.456.789}}"
gives "123.456" [122]"{{#expr: .}}"
gives "0" [123]"{{#expr: 2e5}}"
gives "2.0E5" [124]"{{#expr: 2E5}}"
gives "2.0E5" [125]"{{#expr: E}}"
gives "2.718281828459" [126]"{{#expr: e}}"
gives "2.718281828459" [127]"{{#expr: E5}}"
gives "2.281718171541" [128]"{{#expr: e5}}"
gives "2.281718171541" [129]"{{#expr: 1e5}}"
gives "1.0E5" [130]
Wrong:
"{{#expr: 123,456}}"
gives "Expression error: Unrecognized punctuation character ","." [131]"{{#expr: 123 456}}"
gives "Expression error: Unexpected number." [132]
Due to the specifier R ("raw"), "{{NUMBEROFARTICLES:R}}"
gives "6724" [133] etc., i.e. numbers without group separators, which can be used in computations.
As opposed to ParserFunctions, "plural" 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.
Numbers as output[edit]
A noninteger result has a decimal point in it. Scientific notation is produced for numbers with small absolute value ( less than 1E4) and for numbers with large absolute value ( greater than or equal to 1E+12; however, even within MediaWiki 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):
"{{#expr: .000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123456789012345}}"
gives "1.2345678901234E106" [136]"{{#expr: .00000123456789012345}}"
gives "1.2345678901234E6" [137]"{{#expr: .0000123456789012345}}"
gives "1.2345678901235E5" [138]"{{#expr: .000123456789012345}}"
gives "0.00012345678901234" [139]"{{#expr: 123456789012.345}}"
gives "123456789012.35" [140]"{{#expr: 1234567890123.45}}"
gives "1234567890123.4" [141]"{{#expr: 12345678901234.5}}"
gives "12345678901234" [142]"{{#expr: 123456789012345}}"
gives "1.2345678901234E+14" [143]"{{#expr: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456}}"
gives "1.2345678901235E+105" [144]"{{#expr: 1.234e4}}"
gives "12340" [145]"{{#expr: 1.234e4}}"
gives "0.0001234" [146]
For negative numbers simply a minus sign is prefixed:
"{{#expr: 123456789012345}}"
gives "1.2345678901234E+14" [147]
 Long YYYYMMDDhhmmss timestamps can run into the same problem:
"{{CURRENTTIMESTAMP}}"
gives "20171212135925" [148],"{{#expr:{{CURRENTTIMESTAMP}}}}"
gives "20171212135925" [149]. Timestamps without seconds are 12 digits, just short enough:
"{{#expr:20171212135925/100}}"
gives "201712121359.25" [150]
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. m:Template:roote (backlinks, edit) and m:Template:rooteval (backlinks, edit); for convenience, for the final result another version can be made simply calling the other one and applying #expr to the result, e.g. m:Template:rootfineval (backlinks, edit).
Two methods for representing intermediate results are:
1. The expression resulting from processing the expressions themselves, using #expr for branching only.
For example, m:Template:powe (backlinks, edit) produces the power of a number in the form of an expression:
"{{powe31.1812}}"
gives "Template:Powe" [151]
However, the expression can get quite long, for m:Template:roote (backlinks, edit) 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 a linked expression gives Expression error: Unrecognized 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:
"{{rooteval.0000000000001}}"
giving "Template:Rooteval" [152]
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 m:Template:rootfineval (backlinks, edit), giving Template:Fineval, but the result can also be used in a further computation.
Compare:
"{{#expr:(.0000000000001)^(1/2)}}"
gives "3.1622776601684E7" [153]
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 ):
"{{#expr: ( 0.2 round 0 )}}"
gives "0" [154]"{{#expr: 1*(0.2 round 0)}}"
gives "0" [155]"{{#expr: 0+(0.2 round 0)}}"
gives "0" [156]
The number output is suitable for many other calculation programs, also the scientific notation. In that sense output like 6E23 is more convenient than 6×10^{23}.
Accuracy[edit]
 The accuracy and format of numeric results varies with the operating system of the server.
Accuracy of the result of #expr is less than internally used within the computation of an expression:
"{{#expr:(2/3)}}"
gives "0.66666666666667" [157]"{{#expr:(2/3)*1000000666666}}"
gives "0.66666666662786" [158]"{{#expr:{{#expr:(2/3)}} *1000000666666}}"
gives "0.66666667000391" [159]"{{#expr:1234567890.12345678901234567890}}"
gives "0.12345671653748" [160] (conclusion: 16 or 17 digits of 1234567890.1234567890 used)"{{#expr:1234567890.1234567890}}"
gives "1234567890.1235" [161] (result is rounded to a total of 12 digits)"{{#expr:({{#expr:1234567890.1234567890}} 1234567890)}}"
gives "0.12350010871887" [162]
 The macheps or smallest x such that 1+x != 1 appears to be near 0.5^53 + 0.5^106 or 1.1102230246251566636831481088739149080825883E16 (see above):
"{{#expr:1=1+0.00000000000000011102230246251566636831481088739149080826}}"
gives "0" [163]"{{#expr:1=1+0.00000000000000011102230246251566636831481088739149080825}}"
gives "1" [164]
 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:
"{{#expr:1+0.000000000004999889392}}"
gives "1.000000000005" [165]"{{#expr:1+0.000000000004999889391}}"
gives "1.000000000005" [166] below about 1 + 5E12.
Branching depending on an expression[edit]
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
"{{CURRENTDOW}}"
is "2" [167].
"{{#ifexpr:2^10=102410}}"
gives "1" [168]
Note that rounding errors can affect a result:
"{{#expr:sin pi}}"
gives "6.9829626776863E15" [169]"{{#ifexpr:sin pi=010}}"
gives "0" [170]"{{#ifexpr:abs sin pi<1e1510}}"
gives "0" [171]
Similarly:
Comparisons[edit]
 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.
"{{#switch:FRUK=LondonFR=ParisNL=Amsterdam}}
gives "Paris" [174]
"{{#ifeq:33.010}}"
gives "1" [175]"{{#ifeq:30310}}"
gives "1" [176]"{{#ifeq:0.000034563.456E0510}}"
gives "1" [177] Note that #ifeq: unlike #expr: accepts exponential notation on input.
"{{#ifeq:1234567890123123456789012010}}"
gives "0" [178]"{{#ifeq:12345678901231.23456789012E+1210}}"
gives "0" [179]"{{#ifeq:12345678901201.23456789012E+1210}}"
gives "1" [180]"{{#ifeq:1.234567890120E121.23456789012E+1210}}"
gives "1" [181] Numerical comparisons don't depend on the output format, compare:
"{{#expr:1234567890123}}"
gives "1234567890123" [182]"{{#expr:1234567890120}}"
gives "1234567890120" [183]
#ifeq: allows to compare strings containing equal signs:
#switch:  result  #ifeq:  result 

"{{#switch:1.01=okayfail}}" gives "fail" [184] 
"{{#ifeq:1.01okayfail}}" gives "okay" [185] 

"{{#switch:a=ba=b=okayfail}}" gives "fail" [186] 
"{{#ifeq:a=ba=bokayfail}}" gives "okay" [187] 

"{{#switch:a=ca=b=failokay}}" gives "okay" [188] 
"{{#ifeq:a=ca=bfailokay}}" gives "okay" [189] 
Length of expressions[edit]
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)
For the preexpand include size maximum the first method does not help, but the second does.
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.
Error messages[edit]
Examples for all known #expr: and #ifexpr: error messages. The error texts are within the tags <strong class="error">..</strong>. Note that with the old preprocessor the "=" in "class=error" disturbs the numbering of unnamed parameters, if one of them produces an error message: {{t1{{#expr:1/0}}}} gives {{[[Template:Division by zero.Division by zero.]]}} .
Expression  Error message 
"{{#expr:1/0{{{2}}}}}" gives "Division by zero." [190] 

"{{#expr:2*{{{2}}}}}" gives "Expression error: Missing operand for *." [191] 

"{{#expr:1 2{{{2}}}}}" gives "Expression error: Unexpected number." [192] 

"{{#ifexpr:1*/2{{{2}}}}}" gives "Expression error: Unexpected / operator." [193] 

"{{#expr: 1 (2){{{2}}}}}" gives "Expression error: Unexpected ( operator." [194] 

"{{#expr: (1{{{2}}}}}" gives "Expression error: Unclosed bracket." [195] 

"{{#expr: 1){{{2}}}}}" gives "Expression error: Unexpected closing bracket." [196] 

"{{#expr:2*123,456{{{2}}}}}" gives "Expression error: Unrecognized punctuation character ","." [197] 

"{{#expr:{{{a}}}{{{2}}}}}" gives "Expression error: Unrecognized punctuation character "{"." [198] 

"{{#ifexpr:3%2{{{2}}}}}" gives "Expression error: Unrecognized punctuation character "%"." [199] 

"{{#ifexpr:abc{{{2}}}}}" gives "Expression error: Unrecognized word "abc"." [200] 

"{{#expr:abc.def{{{2}}}}}" gives "Expression error: Unrecognized word "abc"." [201] 

{{ #expr:{{ x1021000*}} 18 }} gives 1.8E+307  
{{ #expr:{{ x1021000*}} 179 }} gives 1.79E+308  
{{ #expr:{{ x1021000*}} 180 }} gives INF ( "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.  
"{{#expr:3.4.5.6}}" gives "3.4" [202] (no feature, only an oddity) 
Note: template:x ( talk edit history links ) 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: Unrecognized word "toolarge".,
 {{ #expr: 2*{{ #ifexpr: 3*4>10too large3*4 }} }} gives
 Expression error: Unrecognized word "too"..
See also #iferror.
Minus sign[edit]
Only the ASCII hyphen (hyphenminus) and minus signs work as minus signs; dashes are not permitted. The output, however, will always be in the form of a hyphenminus:
hyphenminus  "{{#expr:12}}" 
"12"  [203] 
Unicode hyphen (U+2010, ‐)  "{{#expr:‐12}}" 
"Expression error: Unrecognized punctuation character "�"."  
minus sign produced by −  "{{#expr:−12}}" 
"12"  
−  "{{#expr:−12}}" 
"12"  
em dash produced by —  "{{#expr:—12}}" 
"Expression error: Unrecognized punctuation character "�"."  
—  "{{#expr:—12}}" 
"Expression error: Unrecognized punctuation character "&"."  
en dash produced by –  "{{#expr:–12}}" 
"Expression error: Unrecognized punctuation character "�"."  
–  "{{#expr:–12}}" 
"Expression error: Unrecognized punctuation character "&"." 
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.
Displaying numbers and numeric expressions[edit]
Guidelines such as WP:Manual of Style (dates and numbers)#Numbers 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 6×10^{23}
Examples:
"{{#expr:three}}"
gives "Expression error: Unrecognized word "three"." [204]"{{#expr:2<3}}"
gives "1" [205]"{{#expr:2≤3}}"
gives "Expression error: Unrecognized punctuation character "�"." [206]"{{#expr:2<=3}}"
gives "1" [207]"{{#expr:2>3}}"
gives "0" [208]"{{#expr:2≥3}}"
gives "Expression error: Unrecognized punctuation character "�"." [209]"{{#expr:2>=3}}"
gives "0" [210]"{{#expr:2*3}}"
gives "6" [211]"{{#expr:2×3}}"
gives "Expression error: Unrecognized punctuation character "�"." [212]"{{#expr:2,300}}"
gives "Expression error: Unrecognized punctuation character ","." [213]"{{#expr:6E23}}"
gives "6.0E+23" [214]
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.
See also[edit]
 mw:Help:Extension:ParserFunctions
 Help:Parser function (general considerations)
 Help:Substitution
 m:Category:Mathematical templates
fr:Aide:Fonctions parseur hsb:Pomoc:Předłohowe programowanje it:Aiuto:Funzioni parser nl:Help:Parserfuncties pl:Pomoc:Funkcje parsera sl:Pomoč:ParserFunctions vi:Trợ giúp:Tính toán