Help:Calculation
 The accuracy and format of numeric results may vary by server. Currently the results produced by Wikimedia servers seem to be uniform.
The #expr
and #ifexpr
functions of MediaWiki's ParserFunctions extension evaluate mathematical and Boolean expressions—that is, expressions involving numbers and Boolean values (true and false). It does not work with arbitrary strings, as do most of the other parser functions.
The syntax of the #expr
function is:
{{ #expr: expression }}
Spaces are not needed, except between two adjacent words (for operators and constants). Inside numbers, no grouping separators (spaces, commas, apostrophes) are allowed; the only decimal separator supported is the period (dot, full stop); and currently the only supported digits are the ASCII decimal digits 0–9.
In expressions embedded within templates, be careful when using magic words for date and time elements, such as {{CURRENTHOUR}}
as their return value may be formatted differently on translated pages, which will break expressions (instead, use the parser function #time
with the appropriate xn
flags before each formatting element). Similar errors will occur with some other magic words that return quantities (make sure you include the appropriate raw formatting flag).
General[edit]
Technically, an expression is a string representing a tree structure with type/value pairs as nodes, with binary operators in infix notation, unary operators in prefix notation, and end nodes represented by numbers and constants. Letters in operators and names of constants are caseinsensitive.
The ParserFunctions extension determines which operators and constants, and what numbers, are supported. The e in scientific notation (e.g., "2e3" for 2000) and the sign of a number are treated as operators, while the supported literal numbers are unsigned numbers in ordinary decimal format. The extension also determines the precedence of the operators and the error messages, and it converts all literal numbers to floatingpoint values.
For the rest of the implementation, the extension uses PHP functions and operators, so any type conversions and pecularities of certain operators are the properties of those PHP functions and operators themselves. Also, the format of results is entirely determined by PHP.
The only data types used are the PHP data types float (double precision floatingpoint format) and integer (64bit integer). The range for type integer is from −2^{63} = −9,223,372,036,854,775,808 through 2^{63} − 1 = 9,223,372,036,854,775,807. Type float allows fractions and very large numbers, but only in the range ±2^{53} = ±9,007,199,254,740,992 can all integer values be exactly represented in type float (see Help:Calculation accuracy).
Dynamic typing is applied. The end nodes are all of type float (as mentioned, numbers are converted to float; this applies even for numbers with an integer value and format). The data type of the result of an operation depends on the operator, and for some operators on the type(s) of the argument(s), and in some cases on their value(s). If according to these rules the result is of type float, any argument of type integer is converted to float before the operation, and the result is also rounded to float:
{{numfh(trunc2^trunc62+trunc512)2^62}}
→ 0{{numfh(trunc2^trunc62+trunc512)+(trunc2^trunc62+trunc1535)}}
→ 9,223,372,036,854,776,000 (ca. 9.2e18)1.0000000000000_{hex}*2^63
{{numfh2^63+2047}}
→ 9,223,372,036,854,778,000 (ca. 9.2e18)1.0000000000001_{hex}*2^63
Apart from that, a numerical value outside the range of type integer is converted to float, except in the case of trunc
(and mod
, which involves applying trunc
to the arguments first).
There are 31 operators (excluding two synonyms), two constants, and the unsigned numbers in ordinary decimal notation.
Operators, numbers, and constants[edit]
Since literal numbers are of type float, trunc
is sometimes used in the examples to construct an integertype argument, to demonstrate the result of an operator for this case.
Operator  Args  Operation  PHP  Data type  Prec  Examples  

(number)  0  unsigned number in ordinary decimal notation (unary plus and minus and e are treated as operators, see elsewhere in this table)  floatval  float  n.a. 
 
+ 
1  unary + sign

(nothing)  same as argument  n.a. 
 
 
1  unary  sign (negation)

  same as argument  10 
 
pi 
0  constant π  pi()  float  n.a. 
 
e as subexpression 
0  constant e  exp(1)  float  n.a. 
 
e between subexpressions 
2  *10^  * pow (10,..)  float unless the factor on the left is of type integer and the exponent is nonnegative and of type integer  10 
Wrong:
 
exp 
1  exponential function e^{x}  exp  float  9 
Compare:
 
ln 
1  natural logarithm  log  float  9 
Hence, the common logarithm of e.g. 2:
 
abs 
1  absolute value  abs  same as argument, but never negative  9 
 
sqrt 
1  square root  sqrt  float  9 
Negative arguments are not permitted:
 
trunc 
1  truncation  (int), i.e. typecasting to integer  integer  9 
 
floor 
1  floor function  floor  float  9 
 
ceil 
1  ceiling function  ceil  float  9 
 
sin 
1  sine  sin  float  9 
With an angle in degrees, e.g. 30°:
 
cos 
1  cosine  cos  float  9 
 
tan 
1  tangent  tan  float  9 
 
asin 
1  arcsine  asin  float  9 
 
acos 
1  arccosine  acos  float  9 
 
atan 
1  arctangent  atan  float  9 
 
not 
1  negation, logical NOT  !  integer (1 or 0)  9 
 
^ 
2  exponentiation (power)  pow  float unless the base is of type integer and the exponent is nonnegative and of type integer  8 
 
* 
2  multiplication  *  integer if both arguments are integer, otherwise float  7 
 
/ (also written div) 
2  division (div is not integer division^{[1]})  /  float, unless both arguments are integer and the mathematical result is an integer  7 
 
mod 
2  modulo operation, remainder of division after truncating both operands to an integer.^{[1]}  %  integer  7 
May give unexpected results for some values of the second argument (see this section):
 
fmod 
2  modulo operation, floating point. Returns first argument after subtracting an integer multiple of the second argument.  fmod  float  7 
 
+ 
2  addition  +  integer if both arguments are integer, otherwise float  6 
 
 
2  subtraction    integer if both arguments are integer, otherwise float  6 
 
round 
2  rounds off the number on the left to a multiple of 1/10 raised to a power, with the exponent equal to the truncated value of the number given on the right  round  float  5 
 
= 
2  equality (numerical incl. logical, not for strings)  ==  integer (1 or 0)  4 
wrong:
 
<> (also written !=) 
2  inequality, logical xor; not for strings (negation of =)  !=  integer (1 or 0)  4 
 
< 
2  less than (not for ordering of strings)  <  integer (1 or 0)  4 
wrong:
 
> 
2  greater than (same as <, with arguments reversed)  >  integer (1 or 0)  4 
 
<= 
2  less than or equal to (same as >=, with arguments reversed)  <=  integer (1 or 0)  4 
 
>= 
2  greater than or equal to (negation of <)  >=  integer (1 or 0)  4 
 
and 
2  logical AND  &&  integer (1 or 0)  3 
 
or 
2  logical OR    integer (1 or 0)  2 

The logical operators and
, or
, and not
interpret an input value of 0 as false and any other number as true, and return 0 for a false result and 1 for true. These output values also apply to the relation operators; thus {{#expr: (2 < 3) + 1}}
gives 2 [1].
To use and
, or
, and not
in #if
, #ifeq
, or #ifexist
contexts, one can use 1 as thentext (true case) and 0 as elsetext (false case), then combine the results with the logical operators provided by #expr
or #ifexpr
. Note that negation can also be achieved by subtracting the result from 1 (inside of #expr
or #ifexpr
) or by simply switching the then and elsetext(s) of #if
#ifeq
#ifexists
or #ifexpr
. Also, note that the construct {{#expr: {{#if:{{{a}}}10}} or {{#if:{{{b}}}10}} }}
is equivalent to the simpler {{#if:{{{a}}}{{{b}}}10}}}}
.
Precedence is indicated in the "Prec" column above, a higher number means that the operator is applied earlier. Examples (">" refers to going before, "~" means application from left to right):
e
>floor
,not
, etc.:{{#expr:floor1.5e1}}
→ 15,{{#expr:not0e1}}
→ 1floor
>^
:{{#expr:floor1.5^2}}
→ 1^
>*
:{{#expr:2*3^2}}
→ 18*
~/
~mod
:{{#expr:12/3*2}}
→ 8,{{#expr:111/3mod10}}
→ 7,{{#expr:358mod10*2}}
→ 16,*
>+
,
:{{#expr:2+3*4}}
→ 14,{{#expr:23*4}}
→ 10+
~
:{{#expr:62+3}}
→ 7,{{#expr:2+3}}
→ 1+
,
>round
:{{#expr:1.234round21}}
→ 1.2round
>=
etc.:{{#expr:1.23=1.234round2}}
→ 1=
etc. >and
:{{#expr:1 and 2=1}}
→ 0and
>or
:{{#expr:1 or 1 and 0}}
→ 1
In the case of equal precedence number, evaluation is from left to right:
{{#expr:12/2*3}}
→ 18{{#expr:3^3^3}}
→ 19683
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 words (including "e
"), and not allowed within numbers:
{{#expr:7mod3}}
gives 1 [2]{{#expr:7.5round0}}
gives 8 [3]{{#expr:0and1}}
gives 0 [4]{{#expr:0or not0}}
gives 1 [5]{{#expr:0ornot0}}
gives Expression error: Unrecognized word "ornot". [6]{{#expr:123 456}}
gives Expression error: Unexpected number. [7]{{#expr:not not3}}
gives 1 [8]{{#expr:notnot3}}
gives Expression error: Unrecognized word "notnot". [9]
{{#expr:2}}
gives 2 [10]{{#expr:+2}}
gives 2 [11]{{#expr:2*3}}
gives 6 [12]{{#expr:notnotnot0}}
gives 1 [13]{{#expr:2*/3}}
gives Expression error: Unexpected / operator. [14]{{#expr:sinln1.1}}
gives Expression error: Unrecognized word "sinln". [15]{{#expr:sin ln1.1}}
gives 0.095165945236752 [16]
For scientific notation e
is treated as an operator. An e
between subexpressions works just like *10^
, except that together with the unary minus it has the highest precedence (e.g., before a separate ^
), and that the implicit 10 is of type integer, not float. An e
as subexpression (i.e., with each side either nothing or an operator) is Euler's constant. An e
with nothing or an operator on one side and a subexpression on the other gives an error message.
Positional and named parameters[edit]
Positional parameters can be used in calculations inside a template.
For example, in Template:To USD/data/2018, the {{{1}}} positional parameter refers to the amount of local currency to convert to US dollars, and the second positional parameter specifies the country whose currency is being used. When Chinese currency is specified (CHN), the effective template code is:
{{#expr: ({{{1}}} / 6.62) round {{#ifeq: {{{round}}}  yes  0  2 }} }}
Here {{{round}}} is an optional named parameter. If it is equal to "yes", then the converted value is displayed to the nearest US dollar. The default is rounding to the nearest penny.
Here is an example template call specifying the amount 25 Chinese Yuan:
{{To USD/data/201825CHN}}
This produces the result: 3.78
Transitivity[edit]
For comparing a number of type float with one of type integer, the integer is converted to float. Therefore the operators =
, <=
and >=
are not necessarily transitive with mixed types:
{{#expr:trunc1e16=1e16}}
→ 1{{#expr:1e16=trunc1e16+trunc1}}
→ 1{{#expr:trunc1e16=trunc1e16+trunc1}}
→ 0
{{#expr:trunc1e16>=1e16}}
→ 1{{#expr:1e16>=trunc1e16+trunc1}}
→ 1{{#expr:trunc1e16>=trunc1e16+trunc1}}
→ 0
Similarly, if a >= b
and b = c
, that does not necessarily imply a >= c
:
{{#expr:trunc1e16>=1e16}}
→ 1{{#expr:1e16=trunc1e16+trunc1}}
→ 1{{#expr:trunc1e16>=trunc1e16+trunc1}}
→ 0
However, <
and >
are properly transitive.
Monotonicity[edit]
When dividing numbers of type integer, a small change in the dividend can change the type of the result. Therefore, if the absolute value of the result is greater than 2^53, it is not always a monotonic function of the dividend:
{{numf(trunc1e18trunc2)/trunc3}}
→ 333,333,333,333,333,300{{numf(trunc1e18trunc1)/trunc3}}
→ 333,333,333,333,333,300{{numftrunc1e18/trunc3}}
→ 333,333,333,333,333,300
Template:Divint rectifies this.
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 apply also for #ifeq
and #switch
, see below.
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 (following an optional leading sign):
 one or more digits; or
 zero or more digits, a decimal point, and zero or more digits.
Examples:
"{{#expr:123}}"
gives "123" [19]"{{#expr:123.456}}"
gives "123.456" [20]"{{#expr:.456}}"
gives "0.456" [21]"{{#expr:0}}"
gives "0" [22]
Also accepted:
"{{#expr: 123.}}"
gives "123" [23]"{{#expr:000123.4560}}"
gives "123.456" [24]"{{#expr:.}}"
gives "0" [25]
With ignored part:
"{{#expr:123.456.789}}"
gives "123.456" [26]
But wrong:
"{{#expr: 123,456}}"
gives "Expression error: Unrecognized punctuation character ","." [27]"{{#expr: 123 456}}"
gives "Expression error: Unexpected number." [28]
Combinations with the operator e
:
Float:
"{{#expr:2.3e5}}"
gives "2.3E5" [29]{{#expr:2e18}}
→ 2.0E+18
Integer type:
{{#expr:(trunc123456789012345)e trunc4}}
→ 1234567890123450000
Compare:
{{#expr:123456789012345e4}}
→ 1.2345678901235E+18{{#expr:trunc123456789012345e4}}
→ 1234567890123450112{{#expr:(trunc123456789012345)e4}}
→ 1.2345678901235E+18
"{{#expr: e5}}"
gives "Expression error: Unexpected number." [30]"{{#expr: e}}"
gives "2.718281828459" [31]"{{#expr: E}}"
gives "2.718281828459" [32]
Commas can be removed as follows (obviously this is most useful when the value is provided by a template call, parser function, or some other magic word):
{{formatnum:1,2,,34.567,8R}}
→ 1234.5678
Input of a number of type integer is not possible. A float can be converted to type integer with the function trunc
. An integer with a value that is not a float value can be constructed, e.g. with Template:Trunc, where the number is given in two pieces:
{{numf{{trunc1234567890123456789}}}}
→ 1,234,567,890,123,456,800
The smallest positive float can be written:
{{#expr:.5e323}}
→ 4.9406564584125E324
but we cannot use that output as input:
{{#expr:4.9406564584125E324}}
→ 0
All digits are used to determine the float to which a number is rounded, as demonstrated in a borderline case:
{{hex0.00000000000000011102230246251566636831481088739149080825}}
→1.0000000000000_{hex}*2^53
{{hex0.00000000000000011102230246251566636831481088739149080826}}
→1.0000000000001_{hex}*2^53
{{numf2^53}}
→ .00000000000000011102230246251565{{numf2^53+2^105}}
→ .00000000000000011102230246251568
Similarly:
{{numfh4398046511104.00048828125}}
→ 4,398,046,511,104 (ca. 4.4e12)1.0000000000000_{hex}*2^42
{{numfh4398046511104.00048828125000000000000000000000000000000000000000000000001}}
→ 4,398,046,511,104.001 (ca. 4.4e12)1.0000000000001_{hex}*2^42
 2^42+2^10 = 4,398,046,511,104.000,976,562,5
Thus we see that the value halfway the two consecutive floats is rounded down in this case, while the other decimal fractions between the two floats are rounded to the nearest of the two.
Function plural[edit]
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]
The MediaWiki software simply passes on the literal result of the PHP computation, except that logical values are changed to 1 and 0. Therefore the format can depend on the server.
A number of type integer is displayed without rounding and in ordinary decimal notation:
{{#expr:trunc(2^52)}}
→ 4503599627370496{{#expr:trunc(2^52)}}
→ 4503599627370496{{#expr:trunc1100000}}
→ 1100000{{#expr:trunc1200000}}
→ 1200000{{#expr:trunc1300000}}
→ 1300000{{#expr:trunc4100000}}
→ 4100000
while a number of type double is rounded to 14 significant digits, while inconsistently displaying some numbers in scientific format. This is reportedly a bug in the Zend Engine which has been fixed [35], but on Wikimedia apparently not yet:
{{#expr:2^52}}
→ 4.5035996273705E+15{{#expr:(2^52)}}
→ 4.5035996273705E+15{{#expr:1100000}}
→ 1100000{{#expr:1200000}}
→ 1200000{{#expr:1300000}}
→ 1300000{{#expr:4100000}}
→ 4100000{{#expr:1/7}}
→ 0.14285714285714
Note: Internally, the expression may be computed with more digits (typically 18 significant digits, for example on Wikimedia servers which are using IEEE 64bit double in the implementation of PHP used by MediaWiki, but possibly more depending on the hardware architecture supported by PHP, which may have been itself compiled to use "long double" with an extended precision using 80 to 128bit binary formats), so the formatted value returned by #expr will not exhibit some small differences.
If MediaWiki is installed on a server whose PHP engine was compiled for an architecture using different binary storage formats for its C/C++ datatype "double" (possibly with less precision than the IEEE 64bit format), and possibly optimized for speed (the compiled C code or its linkedin mathematical libraries may then not use "strict" IEEE rounding modes for every floating point operation, but may keep some precision for intermediate results by not rounding them at each step, or could also compute results faster using internal values with less precision), you will get different results in PHP: MediaWiki will not be able to use the same precision (or the same range of magnitudes), and the results of calculations may vary between servers. When installing MediaWiki on a PHP server, make sure you look at PHP configuration options.
For example if the architecture supports fast floating points only with 32bit format, you'll get only 7 or 8 significant digits, and the 14 digits displayed by MediaWiki may exceed what the server can really compute. As well you may get unexpected "infinite" values or zero where another server could have returned accurate values.
For some representable round numbers, notably some multiples of 100,000, scientific notation is produced, which, if reused in an expression, is not even exactly equal to the original number:
{{numfh4.1e6}}
→ 4,099,999.9999999995 (ca. 4.1e6)1.f47cfffffffff_{hex}*2^21
Thus we may want to either compare two results of #expr
(for equality up to 14 digits) or compare two expressions, such as 4100000 and 4000000+100000 (for exact equality); depending on context and intention, the negative result of the comparison of the result of #expr
with the exact number may be confusing.
The function formatnum
adds commas (on the left of the decimal point only), but does not convert from or to scientific format:
{{formatnum:1234567.890123}}
→ 1,234,567.890123{{formatnum:1234567.890123E16}}
→ 12,345,678,901,230,000,000,000
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}.
Template:Num displays a number with high accuracy (such that in the case of float the specific internal value is reconstructed when using the output as input), with the variant Template:Numf showing thousands separators:
{{numftrunc3^trunc39}}
→ 4,052,555,153,018,976,000{{numftrunc3^trunc40}}
→ 12,157,665,459,056,929,000{{numf1/7}}
→ .14285714285714285
Negative zero[edit]
Although the literal "0
" (the unary minus applied to 0) gives 0, some operations give the float value "−0" (preserving commutativity of +
and *
):
Generating −0 with *
, /
, ceil
, round
:
{{#expr: 1*0}}
→ 0{{#expr: (1e200)*1e200}}
→ 0{{#expr: 1/1e333}}
→ 0{{#expr: 0/1}}
→ 0{{#expr: (1e200)/1e200}}
→ 0{{#expr: (1e200)/1e200}}
→ 0{{#expr: ceil(.1)}}
→ 0{{#expr: .2round0}}
→ 0
Passing −0 on with unary +
, binary +
and 
, *
(and hence operator e
), /
, floor
, ceil
:
{{#expr: +(1*0)}}
→ 0{{#expr: (1*0)+(1*0)}}
→ 0{{#expr: (1*0)0}}
→ 0{{#expr: 1*(1*0)}}
→ 0{{#expr: (1*0)e0}}
→ 0
{{#expr: (1*0)/1}}
→ 0{{#expr: floor(1*0)}}
→ 0{{#expr: ceil(1*0)}}
→ 0
However:
{{#expr: 0}}
→ 0{{#expr: 10^401}}
→ 0
Since 2011 the function #ifexpr
takes both 0 and the float −0 as false:
{{#ifexpr:010}}
→ 0{{#ifexpr:1*010}}
→ 0
Also, as argument of a logical operator −0 is taken as false:
{{#expr:not(1*0)}}
→ 1{{#expr:(1*0)and1}}
→ 0{{#expr:(1*0)or0}}
→ 0
If an expression of type float can have the value −0, then an operation that will remove the minus sign from a possible −0 but not affect any other result is the addition of 0. If an expression may be of type integer then one can add trunc0
.
{{#expr:0+(1*0)}}
→ 0
Type conversion[edit]
A float can be converted to type integer by operator trunc
(however, note that for 2^63 <= x <= 2^64, we get x − 2^64, and for larger x we get 0; for x < −2^63 we get −2^63).
An expression of type integer can be converted to float by adding 0. Note that for integers greater than 2^53, this involves rounding.
Limitations and workarounds[edit]
The operator trunc gives the correct mathematical result of rounding toward 0 to an integer for integertype numbers and for floats x inside the integer range: −2^63 <= x < 2^63. To also get the correct mathematical result for floats outside this range is simple, because these floats all have an integer value, so they can be left unchanged. Template:Trunc does this.
The operator mod
gives strange errors for some fairly large values of the second argument:
{{#expr:123mod(2^641)}}
→ Expression error: Division by zero
Template:Modint works correctly for a larger range.
The operator round
with second argument 0 gives wrong results for odd numbers between 2^52 and 2^53, even though the exact results are representable as float. Also, the operator rounds integertype numbers with an absolute value between 2^53 and 2^63 to float. Template:Round0 always gives the expression for the exact result, for subsequent use in an expression, or for display (with the accuracy of this depending on the accuracy of the display function or template only).
The operator floor
rounds integertype numbers with an absolute value between 2^53 and 2^63 to float, and not necessarily downward. Similarly the operator ceil
rounds these numbers not necessarily upward. Template:Floor and Template:Ceil always give the expressions for the exact results, for subsequent use in an expression, or for display (with the accuracy of this, and the direction of rounding, depending on the display function or template only).
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 Thursday and so
"{{CURRENTDOW}}"
is "4" [36]. {{#expr:2^10=1024}}
→ 1
Note that rounding errors can affect a comparison, even if they are not visible in the displayed values: the internal values are compared. This applies even to large integers:
{{#expr:1024e201e23}}
→ 2.4E+21{{#expr:1024e201e23=2.4e21}}
→ 0
Instead one may want to allow a relatively small difference that could be present due to rounding errors:
{{#expr:abs(1024e201e232.4e21)<1e8}}
→ 1
Again, for comparing a number of type float with one of type integer, the integer is converted to float. In this case the type is determined by the format of the number, e.g. 2 is an integer, but 2.0 and 2e0 are floats; also 12345678901234567890 is a float, because it is too large for an integer.
Again, equality is not transitive with mixed types:
{{#ifeq:1234567890123456712345678901234568.010}}
→ 1{{#ifeq:12345678901234568.01234567890123456810}}
→ 1{{#ifeq:123456789012345671234567890123456810}}
→ 0
{{#ifeq:1234567890123456712345678901234567e010}}
→ 1{{#ifeq:12345678901234567e01234567890123456810}}
→ 1{{#ifeq:123456789012345671234567890123456810}}
→ 0
{{numtrunc(2^62)trunc1+trunc(2^62)}}
→ 9223372036854775807{{#ifeq:9223372036854775700922337203685477590010}}
→ 0{{#ifeq:9223372036854775900922337203685477580010}}
→ 0{{#ifeq:9223372036854775700922337203685477580010}}
→ 0
Comparisons[edit]
The functions #ifeq
and #switch
compare numbers and strings for equality using PHP operator ==
, the same as the equality operator mentioned above, but now applied directly to the expanded wikitext of the arguments. For comparison as numbers no expressions (not even constants) are allowed, but in this case the unary plus and minus and the e
of scientific notation are taken as part of the number, instead of as operators. Without e
and decimal point the type is integer, otherwise it is float. As mentioned above, when an integer is compared with a float, the integer is converted to float first.
{{#ifeq:33.010}}
→ 1{{#ifeq:30310}}
→ 1{{#ifeq:0.000034563.456E0510}}
→ 1{{#ifeq:1e23.1e2410}}
→ 1 although rounding both numbers to float gives different internal numbers:{{#expr:1e23.1e24}}
→ 16777216{{#expr:1e23=.1e24}}
→ 0{{#ifexpr:1e23=.1e2410}}
→ 0
{{#ifeq:9034567890123456789903456789012345678810}}
→ 0 (two numbers of type integer, therefore only true if exactly equal); compare:{{#ifeq:9034567890123456700.0903456789012345680010}}
→ 1 (due to the decimal point in one number, both are rounded to float before the comparison, so the comparison is cruder)
Length of expressions[edit]
Long expressions are allowed, see Template:Long expression demo. (But see also Help:Expansion#Efficiency.)
Error messages[edit]
The following examples show all known #expr
and #ifexpr
error messages. The error strings are all within <strong class="error">
elements.
 Division by zero
"{{#expr:1/0}}"
→ "Expression error: Division by zero" [37]
 Missing operand
 Unexpected number
"{{#expr:1 2}}"
→ "Expression error: Unexpected number." [42]
 Unexpected word
"{{#expr:1 a}}"
→ "Expression error: Unrecognized word "a"." [43]
 Unexpected operator
 Unexpected bracket
 Unrecognized punctuation
 Unrecognized word
 Result is not a number
"{{#expr:sqrt1}}"
→ "In sqrt: Result is not a number." [55]
 Invalid argument
 Stack exhausted
"{{#expr: {{x34(1+(}} 1 {{x34))}} }}"
→ "Expression error: Stack exhausted."
Note that this last example uses Template:x, which generates multiple copies of a given string. However:
"{{#expr: {{x33(1+(}} 1 {{x33))}} }}"
→ "34"
Large numbers and infinity
{{#expr: {{x1021000*}} 18 }}
→ 1.8E+307{{#expr: {{x1021000*}} 179 }}
→ 1.79E+308{{#expr: {{x1021000*}} 180 }}
→ INF
On Wikimedia wikis this last example gives "INF", but depending on the operating system of the server it may also give, e.g., "1.#INF".
INF also appears when an intermediate result is out of range:
{{#expr:1e309/1e308}}
→ INF{{#expr:1e200*1e200*1e300}}
→ INF
but:
{{#expr:1e200*(1e200*1e300)}}
→ 1.0E+100
Notice that an infinite result is not considered an error:
{{#iferror: {{#expr:1e200*1e200*1e300}}  error}}
→ INF
Miscellaneous malformed expressions or markup
These are not errors, per se, but are probably not what was intended.
"#expr:3.4.5.6"
→ "3.4""{{{#expr:2*3}}}"
→ "{{{#expr:2*3}}}" (triple braces are interpreted as a reference to an undefined parameter named "#expr:2*3")"{{#expr:2*3}}}"
→ "6}" (extra closing braces are interpreted as plain text)"{{{#expr:2*3}}"
→ "{6" (one extra opening brace is interpreted as plain text)"{{#expr:2*3}"
→ "{{#expr:2*3}" (too few opening or closing braces results in everything being interpreted as plain text)
Checking for a number[edit]
Check whether a string is a valid numeric expression:
{{#if:{{#ifexpr:3}}01}}
gives 1{{#if:{{#ifexpr:32}}01}}
gives 1{{#if:{{#ifexpr:3 2}}01}}
gives 0
Find the value represented by a string if it is a valid numeric expression, otherwise just return the string:
{{#iferror:{{#expr:3}}3}}
gives 3{{#iferror:{{#expr:32}}32}}
gives 1{{#iferror:{{#expr:3 2}}3 2}}
gives 3 2
Check whether a string is a number:
{{#ifeq:3{{#expr:3}}10}}
gives 1{{#ifeq:3{{#expr:3}}10}}
gives 1{{#ifeq:3.5{{#expr:3.5}}10}}
gives 1{{#ifeq:03{{#expr:03}}10}}
gives 1{{#ifeq:32{{#expr:32}}10}}
gives 0{{#ifeq:3 2{{#expr:3 2}}10}}
gives 0
Minus sign[edit]
Only the hyphenminus character or minus sign 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, en dash, em dash and others), though often similar visually, are not valid minus signs, but punctuation signs or typographical variants.
hyphenminus, typed directly as the character '' (U+002D)  "{{#expr:12}}" 
"12"  [58] 
hyphenminus, typed as the numerical character reference   "{{#expr:12}}"

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

"Expression error: Unrecognized punctuation character "&"."  [60] 
minus sign, typed directly as the character '−' (U+2212)  "{{#expr:−12}}" 
"12"  [61] 
minus sign, typed as the numerical character reference −  "{{#expr:−12}}"

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

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

"12"  [64] 
figure dash, typed directly as the character '‒' (U+2012)  "{{#expr:‒12}}" 
"Expression error: Unrecognized punctuation character "‒"."  [65] 
figure dash, typed as the numerical character reference ‒  "{{#expr:‒12}}"

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

"Expression error: Unrecognized punctuation character "&"."  [67] 
en dash, typed directly as the character '–' (U+2013)  "{{#expr:–12}}" 
"Expression error: Unrecognized punctuation character "–"."  [68] 
en dash, typed as the numerical character reference –  "{{#expr:–12}}"

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

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

"Expression error: Unrecognized punctuation character "&"."  [71] 
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 w:Wikipedia: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"." [72]{{#expr:{{{1}}}*4,187}} [[w:kJkJ]] ({{{1}} kcal)}}
gives Expression error: Unrecognized punctuation character "{". kJ ({Template:1 kcal)}}"{{#expr:2<3}}"
gives "1" [73]"{{#expr:2≤3}}"
gives "Expression error: Unrecognized punctuation character "≤"." [74]"{{#expr:2<=3}}"
gives "1" [75]"{{#expr:2>3}}"
gives "0" [76]"{{#expr:2≥3}}"
gives "Expression error: Unrecognized punctuation character "≥"." [77]"{{#expr:2>=3}}"
gives "0" [78]"{{#expr:2*3}}"
gives "6" [79]"{{#expr:2×3}}"
gives "Expression error: Unrecognized punctuation character "×"." [80]"{{#expr:2,300}}"
gives "Expression error: Unrecognized punctuation character ","." [81]"{{#expr:6E23}}"
gives "6.0E+23" [82]
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 separated results you can use formatnum:
{{formatnum: {{#expr: {{NUMBEROFPAGES:R}}  {{NUMBEROFFILES:R}} }} }}
= 12,135,569 (instead of 12135569).
See also[edit]
 Help:Mod, round, floor, ceil, trunc
 Help:Calculation accuracy
 Help:Comparison between ParserFunctions syntax and TeX syntax
 Category:Mathematical templates and positional variables
 mw:Extension:MathStatFunctions
 mw:Extension:Foxway
 http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ParserFunctions/Expr.php
 w:Positional parameters, as {{{1}}}.
 Meta:Sandbox to try the calculations.
 ↑ ^{a} ^{b}
div
andmod
are different from all programming languages, see phab:T8068
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 · User style
 Editing
 Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
 Referencing
 Links · URL · Piped links · Interwiki linking · 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