Help:Calculation

From The Scuba Wiki

Jump to: navigation, search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-
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

Operators

Returns 0 or 1 for Booleans or true/false.

Operator Operation Examples
none none
"{{#expr:123456789012345}}" Template:!Template:! "1.2345678901234E+14" [1]
"{{#expr:1.23456789012345}}" Template:!Template:! "1.2345678901235" [2]
"{{#expr:.000001}}" Template:!Template:! "1E-06" [3]
"{{#expr:.00000123456789012345}}" Template:!Template:! "1.2345678901234E-06" [4]
"{{#expr:.123e4}}" Template:!Template:! "1230" [5]
"{{#expr:123E-2}}" Template:!Template:! "1.23" [6]
"{{#expr:e}}" Template:!Template:! "2.718281828459" [7]
"{{#expr:pi}}" Template:!Template:! "3.1415926535898" [8]

Wrong:

"{{#expr:e4}}" Template:!Template:! "Expression error: Unexpected number" [9]
exp Exponential function ex
"{{#expr:exp1.2}}" Template:!Template:! "3.3201169227365" [10]

Compare:

"{{#expr:e^1.2}}" Template:!Template:! "3.3201169227365" [11]
ln Natural logarithm
"{{#expr:ln2}}" Template:!Template:! "0.69314718055995" [12]

Hence, the common logarithm of e.g. 2:

"{{#expr:ln2/ln10}}" Template:!Template:! "0.30102999566398" [13]
abs Absolute value
"{{#expr:abs-1.2}}" Template:!Template:! "1.2" [14]
trunc Truncation
"{{#expr:trunc1.2}}" Template:!Template:! "1" [15]
"{{#expr:trunc1.8}}" Template:!Template:! "1" [16]
"{{#expr:trunc-1.2}}" Template:!Template:! "-1" [17]
floor Floor function
"{{#expr:floor1.2}}" Template:!Template:! "1" [18]
"{{#expr:floor-1.2}}" Template:!Template:! "-2" [19]
ceil Ceiling function
"{{#expr:ceil1.2}}" Template:!Template:! "2" [20]
"{{#expr:ceil-1.2}}" Template:!Template:! "-1" [21]
sin Sine function
"{{#expr:sin.1}}" Template:!Template:! "0.099833416646828" [22]

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

"{{#expr:sin(30*pi/180)}}" Template:!Template:! "0.5" [23]
cos Cosine function
"{{#expr:cos.1}}" Template:!Template:! "0.99500416527803" [24]
tan Tangent function
"{{#expr:tan.1}}" Template:!Template:! "0.10033467208545" [25]
asin Arc sine function
"{{#expr:asin.1}}" Template:!Template:! "0.10016742116156" [26]
acos Arc cosine function
"{{#expr:acos.1}}" Template:!Template:! "1.4706289056333" [27]
atan Arc tangent function
"{{#expr:atan.1}}" Template:!Template:! "0.099668652491162" [28]
+ Unary + sign
"{{#expr:+12}}" Template:!Template:! "12" [29]
"{{#expr:++12}}" Template:!Template:! "12" [30]
"{{#expr:+-12}}" Template:!Template:! "-12" [31]
- Unary - sign (negation)
"{{#expr:-12}}" Template:!Template:! "-12" [32]
"{{#expr:-+12}}" Template:!Template:! "-12" [33]
"{{#expr:--12}}" Template:!Template:! "12" [34]
not Unary NOT, logical NOT
"{{#expr:not 0}}" Template:!Template:! "1" [35]
"{{#expr:not 1}}" Template:!Template:! "0" [36]
"{{#expr:not 2}}" Template:!Template:! "0" [37]
^ Power
"{{#expr:2^7}}" Template:!Template:! "128" [38]
"{{#expr:2^-7}}" Template:!Template:! "0.0078125" [39]
"{{#expr:(-2)^7}}" Template:!Template:! "-128" [40]
"{{#expr:(-2)^8}}" Template:!Template:! "256" [41]
"{{#expr:2^1.2}}" Template:!Template:! "2.2973967099941" [42]
"{{#expr:2^.5}}" Template:!Template:! "1.4142135623731" [43]
"{{#expr:(-2)^1.2}}" Template:!Template:! "NAN" [44]
"{{#expr:(-2)^.5}}" Template:!Template:! "NAN" [45]
* Multiplication
"{{#expr:30*7}}" Template:!Template:! "210" [46]
"{{#expr:30*-7}}" Template:!Template:! "-210" [47]
/ Division, same as div
"{{#expr:30/7}}" Template:!Template:! "4.2857142857143" [48]
"{{#expr:30/-7}}" Template:!Template:! "-4.2857142857143" [49]
div Division, same as /,
not integer division
"{{#expr:30 div 7}}" Template:!Template:! "4.2857142857143" [50] (should be 4)
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.
"{{#expr:30 mod 7}}" Template:!Template:! "2" [51]
"{{#expr:30 mod -7}}" Template:!Template:! "2" [52]
"{{#expr:-30 mod 7}}" Template:!Template:! "-2" [53]
"{{#expr:-30 mod -7}}" Template:!Template:! "-2" [54]
"{{#expr:-8 mod -3}}" Template:!Template:! "-2" [55]
"{{#expr:-8 mod 2.9}}" Template:!Template:! "0" [56] (should be 2.6)
"{{#expr:-8 mod 3.2}}" Template:!Template:! "-2" [57] (should be 1.6)
"{{#expr:8.9 mod 3}}" Template:!Template:! "2" [58] (should be 2.9)
+ Addition
"{{#expr:30 + 7}}" Template:!Template:! "37" [59]
- Subtraction
"{{#expr:30 - 7}}" Template:!Template:! "23" [60]
round Rounds off the number on the left to the power of 1/10 given on the right (PHP function round)
"{{#expr:4.2857 round 3}}" Template:!Template:! "4.286" [61]
"{{#expr:4.2857 round 0}}" Template:!Template:! "4" [62]
"{{#expr:42857 round -2}}" Template:!Template:! "42900" [63]
"{{#expr:4.5 round 0}}" Template:!Template:! "5" [64]
"{{#expr:-4.5 round 0}}" Template:!Template:! "-5" [65]
= Equality (numerical incl. logical)
"{{#expr:3Template:=03.0}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [66]
<> Inequality, same as !=
"{{#expr:3<>03.0}}" Template:!Template:! "0" [67]
"{{#expr:3<>4}}" Template:!Template:! "1" [68]
 != Inequality, same as <>, logical xor
"{{#expr:3!Template:=03.0}}" Template:!Template:! "Expression error: Unrecognised punctuation character "!"" [69]
"{{#expr:3!Template:=4}}" Template:!Template:! "Expression error: Unrecognised punctuation character "!"" [70]
< Less than
"{{#expr:3<2}}" Template:!Template:! "0" [71]
"{{#expr:3<3}}" Template:!Template:! "0" [72]
"{{#expr:3<4}}" Template:!Template:! "1" [73]
> Greater than
"{{#expr:3>2}}" Template:!Template:! "1" [74]
"{{#expr:3>3}}" Template:!Template:! "0" [75]
"{{#expr:3>4}}" Template:!Template:! "0" [76]
<= Less than or equal to
"{{#expr:3<Template:=2}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [77]
"{{#expr:3<Template:=3}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [78]
"{{#expr:3<Template:=4}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [79]
>= Greater than or equal to
"{{#expr:3>Template:=2}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [80]
"{{#expr:3>Template:=3}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [81]
"{{#expr:3>Template:=4}}" Template:!Template:! "Expression error: Unrecognised punctuation character "["" [82]
and Logical AND
"{{#expr:3 and 4}}" Template:!Template:! "1" [83]
"{{#expr:0 and 3}}" Template:!Template:! "0" [84]
"{{#expr:-3 and 0}}" Template:!Template:! "0" [85]
"{{#expr:0 and 0}}" Template:!Template:! "0" [86]
or Logical OR
"{{#expr:3 or 4}}" Template:!Template:! "1" [87]
"{{#expr:0 or 3}}" Template:!Template:! "1" [88]
"{{#expr:-3 or 0}}" Template:!Template:! "1" [89]
"{{#expr:0 or 0}}" Template:!Template:! "0" [90]

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" [91]. Note that "and" and "or" work with #expr and #ifexpr only; for use with #if, #ifeq, and #ifexist, use 1 as then-text and 0 as else-text, and combine results with "and" and "or" in an outer #expr or #ifexpr. Instead of {{ #expr: {{#if:{{{a}}}|1|0}} or {{#if:{{{b}}}|1|0}} }} we can also use {{#if:{{{a}}}{{{b}}}|1|0}}}}. For negation, simply subtract from 1 or interchange then- and else-part.

Precedence:

  • "{{#expr: 2 - 3 + 4 / 5 * 6}}" gives "3.8" [92]

(+ and - have equal precedence, * and / also, both higher than the former two).

  • "{{#expr: 2 = 5 < 3 + 4}}" gives "1" [93]

(first +, then =, then <).

  • "{{#expr:1.234 + 1.234 round 1 + 1}}" gives "2.47" [94]

(first additions, then round)

  • "{{#expr:3 * 4 mod 10 * 10}}" gives "20" [95]

(mod and multiplication have equal precedence, evaluation from left to right)

Parentheses can force a different precedence: "{{#expr: (2 + 3) * 4}}" gives "20" [96]

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" [97]
  • "{{#expr:7.5round0}}" gives "8" [98]
  • "{{#expr:0and1}}" gives "0" [99]
  • "{{#expr:0or not0}}" gives "1" [100]
  • "{{#expr:0ornot0}}" gives "Expression error: Unrecognised word "ornot"" [101]
  • "{{#expr:123 456}}" gives "Expression error: Unexpected number" [102]
  • "{{#expr:not not3}}" gives "1" [103]
  • "{{#expr:notnot3}}" gives "Expression error: Unrecognised word "notnot"" [104]
  • "{{#expr:---2}}" gives "-2" [105]
  • "{{#expr:-+-2}}" gives "2" [106]
  • "{{#expr:2*-3}}" gives "-6" [107]
  • "{{#expr:-not-not-not0}}" gives "-1" [108]
  • "{{#expr:2*/3}}" gives "Expression error: Unexpected / operator" [109]
  • "{{#expr:sinln1.1}}" gives "Expression error: Unrecognised word "sinln"" [110]
  • "{{#expr:sin ln1.1}}" gives "0.095165945236752" [111]

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" [112]
  • "{{#expr: 12.}}" gives "12" [113]

These equivalences are also relevant for #ifeq and #switch, see below.

In [114] 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 base-10 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 was initially parsed only as if it was a binary operator, but now when it true only when it follows a number (otherwise this is Euler's constant, i.e. the base of natural logarithms).

Canonical form:

  • "{{#expr: 123}}" gives "123" [115]
  • "{{#expr: 123.456}}" gives "123.456" [116]
  • "{{#expr: 0.456}}" gives "0.456" [117]
  • "{{#expr: 0}}" gives "0" [118]
  • "{{#expr: 2.0E-5}}" gives "2E-05" [119]

Accepted, although in some cases more or less odd:

  • "{{#expr: .456}}" gives "0.456" [120]
  • "{{#expr: 123.}}" gives "123" [121]
  • "{{#expr: 000123}}" gives "123" [122]
  • "{{#expr: 123.456.789}}" gives "123.456" [123]
  • "{{#expr: .}}" gives "0" [124]
  • "{{#expr: 2e-5}}" gives "2E-05" [125]
  • "{{#expr: 2E-5}}" gives "2E-05" [126]
  • "{{#expr: 1e-5}}" gives "1E-05" [127]
  • "{{#expr: 1 e -5}}" gives "1E-05" [128]

Using Euler's constant e:

  • "{{#expr: E}}" gives "2.718281828459" [129]
  • "{{#expr: e}}" gives "2.718281828459" [130]
  • "{{#expr: E-5}}" gives "-2.281718171541" [131]
  • "{{#expr: e-5}}" gives "-2.281718171541" [132]

Wrong:

  • "{{#expr: 123,456}}" gives "Expression error: Unrecognised punctuation character ","" [133]
  • "{{#expr: 123 456}}" gives "Expression error: Unexpected number" [134]

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, "pluralhttp://thescubawiki.com/Help:Magic_words#Language-dependent_word_conversions" accepts points and commas in numbers and interprets them in a site-language-specific 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 site-language-specific 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

A non-integer result has a decimal point in it. Scientific notation is produced for numbers with small absolute value (for Wikimedia: less than 1E-4) 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 non-scientific 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):

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 non-scientific 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:roote ( talk edit history links ) and template:rooteval ( talk edit history links ); 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:rootfineval ( talk edit history links ).

Two methods for representing intermediate results are:

1. The expression resulting from processing the expressions themselves, using #expr for branching only.

For example, template:powe ( talk edit history links ) produces the power of a number in the form of an expression:

  • "{{powe|31.18|12}}" gives "(31.18*31.18*31.18*31.18*31.18*31.18*31.18*31.18*31.18*31.18*31.18*31.18)" [137]

However, the expression can get quite long, for template:roote ( talk edit history links ) see Help:Calculation/example 1. Although long expressions are allowed, they give rise to a large post-expand include size and template argument size, for which limits exist.

Applying #expr directly on the linked expression gives 0.0052083333397318, 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|Template:1x}}" giving "(Expression error: Unexpected < operator / 10000000000 / 10000000000 / 10000000000)" [138]

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:rootfineval ( talk edit history links ), giving 1E-13, 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 ):

  • "{{#expr: ( -0.2 round 0 )}}" gives "-0" [140]
  • "{{#expr: 1*(-0.2 round 0)}}" gives "-0" [141]
  • "{{#expr: 0+(-0.2 round 0)}}" gives "0" [142]

The number output is suitable for many other calculation programs, also the scientific notation. In that sense output like 6E23 is more convenient than 6e23×1023.

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:

The macheps or smallest x such that 1+x != 1 appears to be near 0.5^53 + 0.5^106 or 1.1102230246251566636831481088739149080825883E-16 (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 + 5E-12.

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 Monday and so "{{CURRENTDOW}}" is "1" [143].
  • "{{#ifexpr:2^10=1024|1|0}}" gives "1" [144]

Note that rounding errors can affect a result:

  • "{{#expr:sin pi}}" gives "1.2246063538224E-16" [145]
  • "{{#ifexpr:sin pi=0|1|0}}" gives "0" [146]
  • "{{#ifexpr:abs sin pi<1e-15|1|0}}" gives "1" [147]

Similarly:

  • "{{#expr:sin pi=0}}" gives "0" [148]
  • "{{#expr:abs sin pi<1e-15}}" gives "1" [149]

Comparisons

  1. The function #ifeq: compares numbers and strings for equality (equal if both represent the same number or both are equal strings).
  2. The function #switch: compares one string with multiple others, and correspondingly produces one of multiple specified results.
  • Template:Xpdopepepec
  • "{{#ifeq:3|3.0|1|0}}" gives "1" [150]
  • "{{#ifeq:3|03|1|0}}" gives "1" [151]
  • "{{#ifeq:0.00003456|3.456E-05|1|0}}" gives "1" [152]
    Note that #ifeq: (like now #expr:) accepts exponential notation on input.
  • "{{#ifeq:1234567890123|1234567890120|1|0}}" gives "0" [153]
  • "{{#ifeq:1234567890123|1.23456789012E+12|1|0}}" gives "0" [154]
  • "{{#ifeq:1234567890120|1.23456789012E+12|1|0}}" gives "1" [155]
  • "{{#ifeq:1.234567890120E12|1.23456789012E+12|1|0}}" gives "1" [156]

Numerical comparisons don't depend on the output format, compare:

  • "{{#expr:1234567890123}}" gives "1234567890123" [157]
  • "{{#expr:1234567890120}}" gives "1234567890120" [158]

#ifeq: allows to compare strings containing equal signs:

#switch: #ifeq:
"{{#switch:-1.0|-1=okay|fail}}" gives "fail" [159] "{{#ifeq:-1.0|-1|okay|fail}}" gives "okay" [160]
"{{#switch:a=b|a=b=okay|fail}}" gives "fail" [161] "{{#ifeq:a=b|a=b|okay|fail}}" gives "okay" [162]
"{{#switch:a=c|a=b=fail|okay}}" gives "okay" [163] "{{#ifeq:a=c|a=b|fail|okay}}" gives "okay" [164]
Template:-

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*(1-2*(x<0))
  • x*{{#ifexpr:x>0|1|-1}}

(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>0|x|-x}}

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<10|0}}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*(1-2*(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*(1-2*(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.

Error messages

Examples for all known #expr: and #ifexpr: error messages. The error texts are within the tags <strong class="error">..</strong>.

"{{#expr:1/0|Template:1x}}" gives "Division by zero" [165]
"{{#expr:2*|Template:1x}}" gives "Expression error: Missing operand for *" [166]
"{{#expr:1 2|Template:1x}}" gives "Expression error: Unexpected number" [167]
"{{#ifexpr:1*/2|Template:1x}}" gives "Expression error: Unexpected / operator" [168]
"{{#expr: 1 (2)|Template:1x}}" gives "Expression error: Unexpected ( operator" [169]
"{{#expr: (1|Template:1x}}" gives "Expression error: Unclosed bracket" [170]
"{{#expr: 1)|Template:1x}}" gives "Expression error: Unexpected closing bracket" [171]
"{{#expr:2*123,456|Template:1x}}" gives "Expression error: Unrecognised punctuation character ","" [172]
"{{#expr:{{{a}}}|Template:1x}}" gives "Expression error: Unrecognised punctuation character "{"" [173]
"{{#ifexpr:3%2|Template:1x}}" gives "Expression error: Unrecognised punctuation character "%"" [174]
"{{#ifexpr:abc|Template:1x}}" gives "Expression error: Unrecognised word "abc"" [175]
"{{#expr:abc.def|Template:1x}}" gives "Expression error: Unrecognised word "abc"" [176]
{{ #expr:{{ x|102|1000*}} 18 }} gives 1.8E+307
{{ #expr:{{ x|102|1000*}} 179 }} gives 1.79E+308
{{ #expr:{{ x|102|1000*}} 180 }} gives INF (on Wikimedia "INF", but depending on the operating system of the server it may also be e.g. "1.#INF")
{{ #expr:{{ x|33|(1+(}} 1 {{ x|33|))}} }} gives Expression error: Unclosed bracket
{{ #expr:{{ x|34|(1+(}} 1 {{ x|34|))}} }} gives Expression error: Stack exhausted
"{{#expr:3.4.5.6}}" gives "3.4" [177]   (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>10|toolarge|3*4 }} }} gives
Expression error: Unrecognised word "toolarge",
{{ #expr: 2*{{ #ifexpr: 3*4>10|too large|3*4 }} }} gives
Expression error: Unrecognised word "too".

See also #iferror.

Minus sign

Only the hyphen-minushttp://en.wikipedia.org/Hyphen-minus character or minus signhttp://en.wikipedia.org/Minus_sign#Character_codes 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 dashhttp://en.wikipedia.org/Dash#En_dash, em dashhttp://en.wikipedia.org/Dash#Em_dash and others), though often similar visually, are not valid minus signs, but punctuation signs or typographical variants.
hyphen-minushttp://en.wikipedia.org/Hyphen-minus, typed directly as the character '-' (U+002D) "{{#expr:-12}}" Template:!! "-12" Template:!! [178]
hyphen-minus, typed as the numerical character reference &#x2D; "{{#expr:&#x2D;12}}" "Expression error: Unrecognised punctuation character "&"" [179]
hyphen-minus, typed as the numerical character reference &#45; "{{#expr:&#45;12}}" "Expression error: Unrecognised punctuation character "&"" [180]
minus signhttp://en.wikipedia.org/Minus_sign#Character_codes, typed directly as the character '−' (U+2212) "{{#expr:−12}}" Template:!! "-12" Template:!! [181]
minus sign, typed as the numerical character reference &#x2212; "{{#expr:&#x2212;12}}" "Expression error: Unrecognised punctuation character "&"" [182]
minus sign, typed as the numerical character reference &#8722; "{{#expr:&#8722;12}}" "Expression error: Unrecognised punctuation character "&"" [183]
minus sign, typed as the symbolic character reference &minus; "{{#expr:&minus;12}}" "-12" [184]
figure dash, typed directly as the character '‒' (U+2012) "{{#expr:‒12}}" Template:!! "Expression error: Unrecognised punctuation character "�"" Template:!! [185]
figure dash, typed as the numerical character reference &#x2012; "{{#expr:&#x2012;12}}" "Expression error: Unrecognised punctuation character "&"" [186]
figure dash, typed as the numerical character reference &#8210; "{{#expr:&#8210;12}}" "Expression error: Unrecognised punctuation character "&"" [187]
en dashhttp://en.wikipedia.org/Dash#En_dash, typed directly as the character '–' (U+2013) "{{#expr:–12}}" Template:!! "Expression error: Unrecognised punctuation character "�"" Template:!! [188]
en dash, typed as the numerical character reference &#x2013; "{{#expr:&#x2013;12}}" "Expression error: Unrecognised punctuation character "&"" [189]
en dash, typed as the numerical character reference &#8211; "{{#expr:&#8211;12}}" "Expression error: Unrecognised punctuation character "&"" [190]
en dash, typed as the symbolic character reference &ndash; "{{#expr:&ndash;12}}" "Expression error: Unrecognised punctuation character "&"" [191]

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

Guidelines such as w:Wikipedia:Manual of Style (dates and numbers)#Numbershttp://en.wikipedia.org/wiki/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 6e23×1023

Examples:

  • "{{#expr:three}}" gives "Expression error: Unrecognised word "three"" [192]
  • "{{#expr:2<3}}" gives "1" [193]
  • "{{#expr:2≤3}}" gives "Expression error: Unrecognised punctuation character "�"" [194]
  • "{{#expr:2<Template:=3}}" gives "Expression error: Unrecognised punctuation character "["" [195]
  • "{{#expr:2>3}}" gives "0" [196]
  • "{{#expr:2≥3}}" gives "Expression error: Unrecognised punctuation character "�"" [197]
  • "{{#expr:2>Template:=3}}" gives "Expression error: Unrecognised punctuation character "["" [198]
  • "{{#expr:2*3}}" gives "6" [199]
  • "{{#expr:2×3}}" gives "Expression error: Unrecognised punctuation character "�"" [200]
  • "{{#expr:2,300}}" gives "Expression error: Unrecognised punctuation character ","" [201]
  • "{{#expr:6E23}}" gives "6E+23" [202]

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


+/-

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 | Wikipediahttp://en.wikipedia.org/Help:Calculation | Wikiquote | Wiktionary
What links here on Meta or from Meta | Wikipedia | MediaWiki
Reading
Go | Search | Stop words | URL | Namespace | Page name | Section
Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
Logging in and preferences
Logging in | Preferences | User style
Editing
Advanced editing | Editing FAQ | Edit toolbar | Export | Import | Shortcuts
Tracking changes
Recent changes (enhanced) | Related changes | Watching pages | Diff
Page history | Edit summary | User contributions | Minor edit | Patrolled edit
Style & formatting
Wikitext examples | Reference card | HTML in wikitext | List | Table | Sorting | Colors
Special input and output
Inputbox | Special characters | Displaying a formula | Images (uploads) | EasyTimeline
Advanced functioning
Template | Advanced templates | Parser function | ParserFunctions | Parameter default
Variable | Magic word | System message | Substitution | Array | Calculation
Page management
Starting a new page | Renaming (moving) a page | Protecting pages | Deleting a page
Special pages
Talk page | Testing | Sandbox | CentralNotice

Template:-

Personal tools
support the site