Help:Calculation
Learn more about Help:Calculation
This is a copy of the master help page at Meta. Do not edit this copy.
Edits will be lost in the next update from the master page. See below for more information.
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 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 
Operators
Operator  Operation  Example 

none  {{ #expr: 123456789012345 }} = 1.23456789012E+14{{ #expr: 0.000001 }} = 1.0E6
 
+  Unary + sign  {{ #expr: +30 * +7 }} = 210

  Unary  sign (negation)  {{ #expr: 30 * 7 }} = 210

not  Unary NOT, logical NOT  {{ #expr: not 0 * 7 }} = 7{{ #expr: not 30 +7 }} = 7

*  Multiplication  {{ #expr: 30*7 }} = 210

/  Division, same as div  {{ #expr: 30/7 }} = 4.28571428571

div  Division, same as /, no integer division  {{ #expr: 30 div 7 }} = 4.28571428571 (should be 4){{ #expr: 5 div 2 * 2 + 5 mod 2 }} = 6 (should be 5)

mod  "Modulo", remainder of division after truncating both operands to an integer. Caveat, div and mod are different from all programming languages. This has been fixed (but needs to be committed), see bugzilla:6068.  {{ #expr: 30 mod 7 }} = 2{{ #expr: 8 mod 3 }} = 2{{ #expr: 8 mod +3 }} = 2{{ #expr: 8 mod 2.7 }} = 0 (should be 2.6){{ #expr: 8 mod 3.2 }} = 2 (should be 1.6){{ #expr: 8.9 mod 3 }} = 2 (should be 2.9)

+  Addition  {{ #expr: 30+7 }} = 37

  Subtraction  {{ #expr: 307 }} = 23

round  Rounds off the number on the left to the power of 1/10 given on the right  {{ #expr: 30/7 round 3 }} = 4.286{{ #expr: 30/7 round 0 }} = 4{{ #expr: 3456 round 2 }} = 3500

=  Equality (numerical incl. logical)  {{ #expr: 30 = 7 }} = 0

<>  Inequality, same as !=  {{ #expr: 30 <> 7 }} = 1

!=  Inequality, same as <>, logical xor  {{ #expr: 1 != 0 }} = 1

<  Less than  {{ #expr: 30 < 7 }} = 0

>  Greater than  {{ #expr: 30 > 7 }} = 1

<=  Less than or equal to  {{ #expr: 30 <= 7 }} = 0

>=  Greater than or equal to  {{ #expr: 30 >= 7 }} = 1

and  Logical AND  {{ #expr: 4<5 and 4 mod 2 }} = 0

or  Logical OR  {{ #expr: 4<5 or 4 mod 2 }} = 1

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
}} = 2.
Precedence:
 {{
#expr: 2  3 + 4 / 5 * 6
}} = 3.8
(+ and  have equal precedence, * and / also, both higher than the former two).
 {{
#expr: 2 = 5 < 3 + 4
}} = 1
(first +, then =, then <).
 {{
#expr:1.234 + 1.234 round 1 + 1
}} gives 2.47
(first additions, then round)
 {{
#expr:3 * 4 mod 10 * 10
}} gives 20
(mod and multiplication have equal precedence, evaluation from left to right)
Parentheses can force a different precedence: {{ #expr: (2 + 3) * 4
}} = 20
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  {{
#expr:7.5round0
}} gives 8  {{
#expr:0and1
}} gives 0  {{
#expr:0or not0
}} gives 1  {{
#expr:0ornot0
}} gives Expression error: Unrecognised word "ornot"  {{
#expr:123 456
}} gives Expression error: Unexpected number  {{
#expr:not not3
}} gives 1  {{
#expr:notnot3
}} gives Expression error: Unrecognised word "notnot"
 {{
#expr:2
}} gives 2  {{
#expr:+2
}} gives 2  {{
#expr:2*3
}} gives 6  {{
#expr:notnotnot0
}} gives 1  {{
#expr:2*/3
}} gives Expression error: Unexpected / operator
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.
 {{
#expr: +01.20
}} gives 1.2  {{
#expr: 12.
}} gives 12
These equivalences are also relevant for #ifeq and #switch, see below.
Scientific notation and group separators are not accepted in input for expressions:
 {{
#expr: 2E05
}} gives Expression error: Unrecognised word "e".  {{
#expr:
{{#expr:2/100000
}}}} gives Expression error: Unrecognised word "e".
 {{
#expr: 123,456
}} gives Expression error: Unrecognised punctuation character ","
Due to the specifier R ("raw"), {{ NUMBEROFARTICLES:R
}} = etc. produce numbers without group separators, which can be used in computations.
Numbers as output
A noninteger result has a decimal point in it. Scientific notation is produced for numbers with absolute value less than 1E4 and for numbers with absolute value greater than or equal to 1E+12. Thus numbers produced include:
 {{
#expr: 1234567890*10
}} = 12345678900  {{
#expr: +1234567890*100
}} = 123456789000  {{
#expr: 1234567890*1000
}} = 1.23456789E+12  {{
#expr: +1234567890*10000
}} = 1.23456789E+13  {{
#expr: 0.1234567890/100
}} = 0.00123456789  {{
#expr: +0.1234567890/1000
}} = 0.000123456789  {{
#expr: 0.1234567890/10000
}} = 1.23456789E5  {{
#expr: +0.1234567890/100000
}} = 1.23456789E6
Since scientific notation is not accepted as input, nested use of #expr may fail in cases where the same composite computation with a single #expr works.
 Thus:
 {{
#expr: 1/10000 *2
}} = 0.0002,  {{
#expr:
{{#expr: 1/10000
}}*2
}} = 0.0002,  {{
#expr: 1/100000 *2
}} = 2.0E5, but  {{
#expr:
{{#expr: 1/100000
}}*2
}} = Expression error: Unrecognised word "e".  Long YYYYMMDDhhmmss timestamps can run into the same problem:
 {{
CURRENTTIMESTAMP
}} gives 20171017141347,  {{
#expr:
{{CURRENTTIMESTAMP
}}}} gives 2.01710171413E+13.
 Timestamps without seconds are 12 digits, just short enough:
 {{
#expr:20171017141347/100
}} gives 201710171413
See m:Template:evalint (talk, backlinks, edit) for producing output of large integers in nonscientific notation, suitable as input in another computation. For producing numbers with commas as group separators, see m:Template:csn (talk, backlinks, edit).
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 )
}} = 0  {{
#expr: 1*(0.2 round 0)
}} = 0  {{
#expr: 0+(0.2 round 0)
}} = 0
Accuracy
Accuracy of the result of #expr is less than internally used within the computation of an expression:
 {{
#expr:(2/3)
}} gives 0.666666666667  {{
#expr:(2/3)*1000000666666
}} gives 0.666666666628  {{
#expr:
{{#expr:(2/3)
}}*1000000666666
}} gives 0.666667000041  {{
#expr:1234567890.12345678901234567890
}} gives 0.123456716537 (conclusion: 16 or 17 digits of 1234567890.1234567890 used)  {{
#expr:1234567890.1234567890
}} gives 1234567890.12 (result is rounded to a total of 12 digits)  {{
#expr:(
{{#expr:1234567890.1234567890
}}1234567890)
}} gives 0.119999885559
 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
}} </small> = 0  {{
#expr:1=1+0.00000000000000011102230246251566636831481088739149080825
}} </small> = 1
 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
}} = 1.00000000001  {{
#expr:1+0.000000000004999889391
}} = 1 below about 1 + 5E12.
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). Example:
 {{#ifexpr: {{CURRENTDOW}} = 0 or {{CURRENTDOW}} = 6  weekend  MoFr}} yields MoFr because today is Tuesday with {{
CURRENTDOW
}} = 2.
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.
 {{
#switch:FRUK=LondonFR=ParisNL=Amsterdam
}} gives Paris  {{
#switch:UKUKGB=LondonFR=Paris
}} gives London  {{
#switch:ZZUK=LondonFR=Parisno match
}} gives no match
 {{
#ifeq:33.010
}} gives 1  {{
#ifeq:30310
}} gives 1  {{
#ifeq:0.000034563.456E0510
}} gives 1 Note that #ifeq: unlike #expr: accepts exponential notation on input.
 {{
#ifeq:1234567890123123456789012010
}} gives 0  {{
#ifeq:12345678901231.23456789012E+1210
}} gives 0  {{
#ifeq:12345678901201.23456789012E+1210
}} gives 1  {{
#ifeq:1.234567890120E121.23456789012E+1210
}} gives 1 Numerical comparisons don't depend on the output format, compare:
 {{
#expr:1234567890123
}} gives 1.23456789012E+12  {{
#expr:1234567890120
}} gives 1.23456789012E+12
#ifeq: allows to compare strings containing equal signs:
#switch:  result  #ifeq:  result 

{{ #switch:1.01=okayfail }} </td><td> okay
 {{ #ifeq:1.01okayfail }} </td><td> okay
 
{{ #switch:a=ba=b=okayfail }} </td><td> fail
 {{ #ifeq:a=ba=bokayfail }} </td><td> okay
 
{{ #switch:a=ca=b=failokay }} </td><td> okay
 {{ #ifeq:a=ca=bfailokay }} </td><td> okay

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). Templates (subroutines) provide some of the functionality that variables offer, but fetching them from the database can be slower than evaluating even a long expression.
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
Expression  Error message 
{{ #expr:1/0 }} </td><td> Division by zero
 
{{ #expr:2* }} </td><td> Expression error: Missing operand for *
 
{{ #expr:1 2 }} </td><td> Expression error: Unexpected number
 
{{ #ifexpr:1*/2 }} </td><td> Expression error: Unexpected / operator
 
{{ #expr: 1 (2) }} </td><td> Expression error: Unexpected ( operator
 
{{ #expr: (1 }} </td><td> Expression error: Unclosed bracket
 
{{ #expr: 1) }} </td><td> Expression error: Unexpected closing bracket
 
{{ #expr:2*123,456 }} </td><td> Expression error: Unrecognised punctuation character ","
 
{{ #expr:{{{a}}} }} </td><td> Expression error: Unrecognised punctuation character "{"
 
{{ #ifexpr:3%2 }} </td><td> Expression error: Unrecognised punctuation character "%"
 
{{ #ifexpr:abc }} </td><td> Expression error: Unrecognised word "abc"
 
{{ #expr:abc.def }} </td><td> Expression error: Unrecognised word "abc"
 
{{ #expr:{{ x1021000*}} 18 }} gives 1.8E+307  
{{ #expr:{{ x1021000*}} 179 }} gives 1.79E+308  
{{ #expr:{{ x1021000*}} 180 }} gives INF  
{{ #expr:{{ x33(1+(}} 1 {{ x33))}} }} gives 34  
{{ #expr:{{ x34(1+(}} 1 {{ x34))}} }} gives Expression error: Stack exhausted  
{{ #expr:3.4.5.6 }} gives 3.4 (no feature, only an oddity)

Note: m:Template:x (talk, backlinks, edit) 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
 m:ParserFunctions
 m:Help:Modulo and round
 Help:Parser function (general considerations)
 Help:Substitution
 m:Category:Mathematical templates
 w:en:Category:Mathematical templates
edit 
Wikipediaspecific help
 This help page is new, and some links still might only on Meta.
This page is a copy of the master help page at Meta (for general help information all Wikimedia projects can use), with two Wikipediaspecific templates inserted. To update the main text, edit the master help page for all projects at m:Help:Calculation. For Wikipediaspecific issues, use Template:Ph:Calculation (the extra text at the bottom of this page) or Template:Phh:Calculation for a Wikipediaspecific lead (text appears at the top of this page). You are welcome to copy the exact wikitext from the master page at Meta and paste it into this page at any time. To view this page in other languages see the master page at Meta.