Help:Calculation

From Eugene Neighbors

Jump to: navigation, search
{{#if: Help|
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins Template:Edi

{{#if:|Other languages:}} Template:H-langs:

{{#if: Help||}}|
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins Template:Edi

{{#if:|Other languages:}} Template:H-langs: {{#if: Help||}}}}

The accuracy and format of numeric results varies with the operating system of the server.

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

[edit] Operators

Operator Operation Example
none {{ #expr: 123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: 123456789012345}}
{{ #expr: 0.000001}} {{#ifeq: =|=|=|gives}} {{#expr: 0.000001}}
+ Unary + sign {{ #expr: +30 * +7}} {{#ifeq: =|=|=|gives}} {{#expr: +30 * +7}}
- Unary - sign (negation) {{ #expr: -30 * -7}} {{#ifeq: =|=|=|gives}} {{#expr: -30 * -7}}
not Unary NOT, logical NOT {{ #expr: not 0 * 7}} {{#ifeq: =|=|=|gives}} {{#expr: not 0 * 7}}
{{ #expr: not 30 +7}} {{#ifeq: =|=|=|gives}} {{#expr: not 30 +7}}
* Multiplication {{ #expr: 30*7}} {{#ifeq: =|=|=|gives}} {{#expr: 30*7}}
/ Division, same as div {{ #expr: 30/7}} {{#ifeq: =|=|=|gives}} {{#expr: 30/7}}
div Division, same as /,
not integer division
{{ #expr: 30 div 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 div 7}} (should be 4)
{{ #expr: 5 div 2 * 2 + 5 mod 2}} {{#ifeq: =|=|=|gives}} {{#expr: 5 div 2 * 2 + 5 mod 2}} (should be 5)
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}} {{#ifeq: =|=|=|gives}} {{#expr: 30 mod 7}}
{{ #expr: -8 mod -3}} {{#ifeq: =|=|=|gives}} {{#expr: -8 mod -3}}
{{ #expr: -8 mod +3}} {{#ifeq: =|=|=|gives}} {{#expr: -8 mod +3}}
{{ #expr: 8 mod 2.7}} {{#ifeq: =|=|=|gives}} {{#expr: 8 mod 2.7}} (should be 2.6)
{{ #expr: 8 mod 3.2}} {{#ifeq: =|=|=|gives}} {{#expr: 8 mod 3.2}} (should be 1.6)
{{ #expr: 8.9 mod 3}} {{#ifeq: =|=|=|gives}} {{#expr: 8.9 mod 3}} (should be 2.9)
+ Addition {{ #expr: 30+7}} {{#ifeq: =|=|=|gives}} {{#expr: 30+7}}
- Subtraction {{ #expr: 30-7}} {{#ifeq: =|=|=|gives}} {{#expr: 30-7}}
round Rounds off the number on the left to the power of 1/10 given on the right (PHP function round) {{ #expr: 30/7 round 3}} {{#ifeq: =|=|=|gives}} {{#expr: 30/7 round 3}}
{{ #expr: 30/7 round 0}} {{#ifeq: =|=|=|gives}} {{#expr: 30/7 round 0}}
{{ #expr: 3456 round -2}} = {{#expr: 3456 round -2}}
= Equality (numerical incl. logical) {{ #expr: 30 = 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 = 7}}
<> Inequality, same as != {{ #expr: 30 <> 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 <> 7}}
 != Inequality, same as <>, logical xor {{ #expr: 1 != 0}} {{#ifeq: =|=|=|gives}} {{#expr: 1 != 0}}
< Less than {{ #expr: 30 < 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 < 7}}
> Greater than {{ #expr: 30 > 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 > 7}}
<= Less than or equal to {{ #expr: 30 <= 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 <= 7}}
>= Greater than or equal to {{ #expr: 30 >= 7}} {{#ifeq: =|=|=|gives}} {{#expr: 30 >= 7}}
and Logical AND {{ #expr: 4<5 and 4 mod 2}} {{#ifeq: =|=|=|gives}} {{#expr: 4<5 and 4 mod 2}}
or Logical OR {{ #expr: 4<5 or 4 mod 2}} {{#ifeq: =|=|=|gives}} {{#expr: 4<5 or 4 mod 2}}

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}} {{#ifeq: =|=|=|gives}} {{#expr: (2 < 3) + 1}}. 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}} {{#ifeq: =|=|=|gives}} {{#expr: 2 - 3 + 4 / 5 * 6}}

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

  • {{ #expr: 2 = 5 < 3 + 4}} {{#ifeq: =|=|=|gives}} {{#expr: 2 = 5 < 3 + 4}}

(first +, then =, then <).

  • {{ #expr:1.234 + 1.234 round 1 + 1 }} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:1.234 + 1.234 round 1 + 1 }}

(first additions, then round)

  • {{ #expr:3 * 4 mod 10 * 10 }} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:3 * 4 mod 10 * 10 }}

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

Parentheses can force a different precedence: {{ #expr: (2 + 3) * 4}} {{#ifeq: =|=|=|gives}} {{#expr: (2 + 3) * 4}}

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{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:7mod3|||}}|{{#ifeq:-|+|{{#expr:7mod3||}}|{{#ifeq:-|+|{{#expr:7mod3|}}|{{#expr:7mod3}}}}}}}}
  • {{ #expr:7.5round0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:7.5round0|||}}|{{#ifeq:-|+|{{#expr:7.5round0||}}|{{#ifeq:-|+|{{#expr:7.5round0|}}|{{#expr:7.5round0}}}}}}}}
  • {{ #expr:0and1{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:0and1|||}}|{{#ifeq:-|+|{{#expr:0and1||}}|{{#ifeq:-|+|{{#expr:0and1|}}|{{#expr:0and1}}}}}}}}
  • {{ #expr:0or not0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:0or not0|||}}|{{#ifeq:-|+|{{#expr:0or not0||}}|{{#ifeq:-|+|{{#expr:0or not0|}}|{{#expr:0or not0}}}}}}}}
  • {{ #expr:0ornot0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:0ornot0|||}}|{{#ifeq:-|+|{{#expr:0ornot0||}}|{{#ifeq:-|+|{{#expr:0ornot0|}}|{{#expr:0ornot0}}}}}}}}
  • {{ #expr:123 456{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:123 456|||}}|{{#ifeq:-|+|{{#expr:123 456||}}|{{#ifeq:-|+|{{#expr:123 456|}}|{{#expr:123 456}}}}}}}}
  • {{ #expr:not not3{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:not not3|||}}|{{#ifeq:-|+|{{#expr:not not3||}}|{{#ifeq:-|+|{{#expr:not not3|}}|{{#expr:not not3}}}}}}}}
  • {{ #expr:notnot3{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:notnot3|||}}|{{#ifeq:-|+|{{#expr:notnot3||}}|{{#ifeq:-|+|{{#expr:notnot3|}}|{{#expr:notnot3}}}}}}}}
  • {{ #expr:---2{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:---2|||}}|{{#ifeq:-|+|{{#expr:---2||}}|{{#ifeq:-|+|{{#expr:---2|}}|{{#expr:---2}}}}}}}}
  • {{ #expr:-+-2{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-+-2|||}}|{{#ifeq:-|+|{{#expr:-+-2||}}|{{#ifeq:-|+|{{#expr:-+-2|}}|{{#expr:-+-2}}}}}}}}
  • {{ #expr:2*-3{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2*-3|||}}|{{#ifeq:-|+|{{#expr:2*-3||}}|{{#ifeq:-|+|{{#expr:2*-3|}}|{{#expr:2*-3}}}}}}}}
  • {{ #expr:-not-not-not0{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:-not-not-not0|||}}|{{#ifeq:-|+|{{#expr:-not-not-not0||}}|{{#ifeq:-|+|{{#expr:-not-not-not0|}}|{{#expr:-not-not-not0}}}}}}}}
  • {{ #expr:2*/3{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:2*/3|||}}|{{#ifeq:-|+|{{#expr:2*/3||}}|{{#ifeq:-|+|{{#expr:2*/3|}}|{{#expr:2*/3}}}}}}}}

[edit] Numbers as input

Leading zeros are allowed, as well as a trailing decimal point (for an integer) and trailing zeros in a number with a decimal point.

  • {{ #expr: +01.20}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: +01.20}}
  • {{ #expr: 12.}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 12.}}

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

In [1] we have

define( 'EXPR_NUMBER_CLASS', '0123456789.' );
...
elseif ( false !== strpos( EXPR_NUMBER_CLASS, $char ) ) { 
// Number
if ( $expecting != 'expression' ) {throw new ExprError('unexpected_number');}
// Find the rest of it
$length = strspn( $expr, EXPR_NUMBER_CLASS, $p );
// Convert it to float, silently removing double decimal points
$operands[] = floatval( substr( $expr, $p, $length ) );
$p += $length;$expecting = 'operator';continue;}

Thus the part of the expression representing a number is a sequence of digits and points; due to floatval a second point and any digits and points immediately after it are ignored, and do not give an error message. Scientific notation and group separators are not allowed: an e or E is considered an unrecognized word after the number, 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}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 123}}
  • {{ #expr: 123.456}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 123.456}}
  • {{ #expr: 0.456}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 0.456}}
  • {{ #expr: 0}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 0}}

Accepted, although in some cases more or less odd:

  • {{ #expr: .456}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: .456}}
  • {{ #expr: 123.}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 123.}}
  • {{ #expr: 000123}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 000123}}
  • {{ #expr: 123.456.789}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 123.456.789}}
  • {{ #expr: .}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: .}}

Wrong:

  • {{ #expr: 2e-5}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 2e-5}}
  • {{ #expr: 2E-5}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 2E-5}}
  • {{ #expr: 123,456}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 123,456}}
  • {{ #expr: 123 456}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr: 123 456}}

Due to the specifier R ("raw"), {{ NUMBEROFARTICLES:R{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} = {{#ifeq:-|+|175|{{#ifeq:-|+|175|{{#ifeq:-|+|175|175}}}}}} etc. produce numbers without group separators, which can be used in computations.

As opposed to ParserFunctions, "{{#if:Language-dependent_word_conversions|{{#if:plural|plural|Help:Magic_words#Language-dependent_word_conversions}}|{{#if:plural|plural|Help:Magic_words}}}}http://wiki.eugeneneighbors.org{{#if:Language-dependent_word_conversions|/wiki/Help:Magic_words#Language-dependent_word_conversions|/wiki/Help:Magic_words}}" accepts points and commas in numbers and interprets them in a site-language-specific way (depending on $separatorTransformTable in Messagesxx.php); on this site:

  • {{plural:1.000|a|b|c}} gives a
  • {{plural:1,000|a|b|c}} gives b

(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.

[edit] 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). Thus numbers produced include (depending on the operating system of the server):

  • {{ #expr: .000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: .000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123456789012345}}
  • {{ #expr: .00000123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: .00000123456789012345}}
  • {{ #expr: .0000123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: .0000123456789012345}}
  • {{ #expr: .000123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: .000123456789012345}}
  • {{ #expr: 123456789012.345}} {{#ifeq: =|=|=|gives}} {{#expr: 123456789012.345}}
  • {{ #expr: 1234567890123.45}} {{#ifeq: =|=|=|gives}} {{#expr: 1234567890123.45}}
  • {{ #expr: 12345678901234.5}} {{#ifeq: =|=|=|gives}} {{#expr: 12345678901234.5}}
  • {{ #expr: 123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: 123456789012345}}
  • {{ #expr: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456}} {{#ifeq: =|=|=|gives}} {{#expr: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456}}

For negative numbers simply a minus sign is prefixed:

  • {{ #expr: -123456789012345}} {{#ifeq: =|=|=|gives}} {{#expr: -123456789012345}}

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}} {{#ifeq: =|=|=|gives}} {{#expr: 1/10000 *2}},
Template:Evdn,
{{ #expr: 1/100000 *2}} {{#ifeq: =|=|=|gives}} {{#expr: 1/100000 *2}}, but
Template:Evdn.
Long YYYYMMDDhhmmss timestamps can run into the same problem:
{{ CURRENTTIMESTAMP}} {{#ifeq: +|-|{{{2}}}|gives}} 20080808190444,
Template:Evdn.
Timestamps without seconds are 12 digits, just short enough:
{{ #expr:20080808190444/100}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:20080808190444/100}}

To allow the output of an expression to be used as input, the expression can be replaced by e.g.

  • {{#ifexpr: ''expression''/10000000000 >1 | {{#expr:''expression''/10000000000}} * 10000000000 | ''expression'' }}

Examples:

  • {{#ifexpr: 123456789012345/10000000000 >1 | {{#expr:123456789012345/10000000000}} * 10000000000 | 123456789012345 }} gives {{#ifexpr: 123456789012345/10000000000 >1 | {{#expr:123456789012345/10000000000}} * 10000000000 | 123456789012345 }}
  • {{#ifexpr: 12345.6789012345/10000000000 >1 | {{#expr:12345.6789012345/10000000000}} * 10000000000 | 12345.6789012345 }} gives {{#ifexpr: 12345.6789012345/10000000000 >1 | {{#expr:12345.6789012345/10000000000}} * 10000000000 | 12345.6789012345 }}

This increases the upper limit by a factor 1e10.

See Template:Links-core for producing output of large integers in non-scientific notation, suitable as input in another computation. For producing numbers with commas as group separators, see Template:Links-core.

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 )}} {{#ifeq: =|=|=|gives}} {{#expr: ( -0.2 round 0 )}}
  • {{ #expr: 1*(-0.2 round 0)}} {{#ifeq: =|=|=|gives}} {{#expr: 1*(-0.2 round 0)}}
  • {{ #expr: 0+(-0.2 round 0)}} {{#ifeq: =|=|=|gives}} {{#expr: 0+(-0.2 round 0)}}

[edit] Accuracy

The accuracy and format of numeric results varies with the operating system of the server. Some remarks in this section are specific for Wikimedia.

Accuracy of the result of #expr is less than internally used within the computation of an expression:

  • {{ #expr:(2/3)}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:(2/3)}}
  • {{ #expr:(2/3)*1000000-666666}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:(2/3)*1000000-666666}}
  • Template:Evdn
  • {{ #expr:1234567890.1234567890-1234567890}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:1234567890.1234567890-1234567890}} (conclusion: 16 or 17 digits of 1234567890.1234567890 used)
  • {{ #expr:1234567890.1234567890}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:1234567890.1234567890}} (result is rounded to a total of 12 digits)
  • Template:Evdn
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 above):
{{ #expr:1=1+0.00000000000000011102230246251566636831481088739149080826}} {{#ifeq: =| =| =|gives}} {{#expr:1=1+0.00000000000000011102230246251566636831481088739149080826}}
{{ #expr:1=1+0.00000000000000011102230246251566636831481088739149080825}} {{#ifeq: =| =| =|gives}} {{#expr:1=1+0.00000000000000011102230246251566636831481088739149080825}}
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}} {{#ifeq
=|=|=|gives}} {{#expr:1+0.000000000004999889392}}
{{ #expr:1+0.000000000004999889391}} {{#ifeq
=|=|=|gives}} {{#expr:1+0.000000000004999889391}} below about 1 + 5E-12.

[edit] Branching depending on an expression

The function #ifexpr produces one of two specified results, depending on the value of a boolean expression involving numbers and booleans (not strings). Example:

{{#ifexpr: {{CURRENTDOW}} = 0 or {{CURRENTDOW}} = 6 | weekend | Mo-Fr}} yields {{#ifexpr: 5 = 0 or 5 = 6|weekend|Mo-Fr}} because today is Friday with {{ CURRENTDOW{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} = {{#ifeq
-|+|Template:CURRENTDOW|{{#ifeq:-|+|Template:CURRENTDOW|{{#ifeq:-|+|Template:CURRENTDOW|5}}}}}}.

[edit] 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.
  • {{ #switch:FR{{#if:|{{{{{1n}}}}}}}{{#ifeq:NL=Amsterdam|NL=Amsterdam||UK=London|FR=Paris|NL=Amsterdam|{{#ifeq:FR=Paris|FR=Paris||UK=London|FR=Paris|{{#ifeq:UK=London|UK=London||UK=London|}}}}}}}} gives {{#ifeq:NL=Amsterdam|NL=Amsterdam|{{#switch:FR|UK=London|FR=Paris|NL=Amsterdam}}|{{#ifeq:FR=Paris|FR=Paris|{{#switch:FR|UK=London|FR=Paris}}|{{#ifeq:UK=London|UK=London|{{#switch:FR|UK=London}}|{{#switch:FR}}}}}}}}
  • {{ #switch:UK{{#if:|{{{{{1n}}}}}}}{{#ifeq:FR=Paris|FR=Paris||UK|GB=London|FR=Paris|{{#ifeq:GB=London|GB=London||UK|GB=London|{{#ifeq:UK|UK||UK|}}}}}}}} gives {{#ifeq:FR=Paris|FR=Paris|{{#switch:UK|UK|GB=London|FR=Paris}}|{{#ifeq:GB=London|GB=London|{{#switch:UK|UK|GB=London}}|{{#ifeq:UK|UK|{{#switch:UK|UK}}|{{#switch:UK}}}}}}}}
  • {{ #switch:ZZ{{#if:|{{{{{1n}}}}}}}{{#ifeq:no match|no match||UK=London|FR=Paris|no match|{{#ifeq:FR=Paris|FR=Paris||UK=London|FR=Paris|{{#ifeq:UK=London|UK=London||UK=London|}}}}}}}} gives {{#ifeq:no match|no match|{{#switch:ZZ|UK=London|FR=Paris|no match}}|{{#ifeq:FR=Paris|FR=Paris|{{#switch:ZZ|UK=London|FR=Paris}}|{{#ifeq:UK=London|UK=London|{{#switch:ZZ|UK=London}}|{{#switch:ZZ}}}}}}}}
  • {{ #ifeq:3{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||3.0|1|0|{{#ifeq:1|1||3.0|1|{{#ifeq:3.0|3.0||3.0|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:3|3.0|1|0}}|{{#ifeq:1|1|{{#ifeq:3|3.0|1}}|{{#ifeq:3.0|3.0|{{#ifeq:3|3.0}}|{{#ifeq:3}}}}}}}}
  • {{ #ifeq:3{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||03|1|0|{{#ifeq:1|1||03|1|{{#ifeq:03|03||03|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:3|03|1|0}}|{{#ifeq:1|1|{{#ifeq:3|03|1}}|{{#ifeq:03|03|{{#ifeq:3|03}}|{{#ifeq:3}}}}}}}}
  • {{ #ifeq:0.00003456{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||3.456E-05|1|0|{{#ifeq:1|1||3.456E-05|1|{{#ifeq:3.456E-05|3.456E-05||3.456E-05|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:0.00003456|3.456E-05|1|0}}|{{#ifeq:1|1|{{#ifeq:0.00003456|3.456E-05|1}}|{{#ifeq:3.456E-05|3.456E-05|{{#ifeq:0.00003456|3.456E-05}}|{{#ifeq:0.00003456}}}}}}}}
    Note that #ifeq: unlike #expr: accepts exponential notation on input.
  • {{ #ifeq:1234567890123{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||1234567890120|1|0|{{#ifeq:1|1||1234567890120|1|{{#ifeq:1234567890120|1234567890120||1234567890120|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:1234567890123|1234567890120|1|0}}|{{#ifeq:1|1|{{#ifeq:1234567890123|1234567890120|1}}|{{#ifeq:1234567890120|1234567890120|{{#ifeq:1234567890123|1234567890120}}|{{#ifeq:1234567890123}}}}}}}}
  • {{ #ifeq:1234567890123{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||1.23456789012E+12|1|0|{{#ifeq:1|1||1.23456789012E+12|1|{{#ifeq:1.23456789012E+12|1.23456789012E+12||1.23456789012E+12|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:1234567890123|1.23456789012E+12|1|0}}|{{#ifeq:1|1|{{#ifeq:1234567890123|1.23456789012E+12|1}}|{{#ifeq:1.23456789012E+12|1.23456789012E+12|{{#ifeq:1234567890123|1.23456789012E+12}}|{{#ifeq:1234567890123}}}}}}}}
  • {{ #ifeq:1234567890120{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||1.23456789012E+12|1|0|{{#ifeq:1|1||1.23456789012E+12|1|{{#ifeq:1.23456789012E+12|1.23456789012E+12||1.23456789012E+12|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:1234567890120|1.23456789012E+12|1|0}}|{{#ifeq:1|1|{{#ifeq:1234567890120|1.23456789012E+12|1}}|{{#ifeq:1.23456789012E+12|1.23456789012E+12|{{#ifeq:1234567890120|1.23456789012E+12}}|{{#ifeq:1234567890120}}}}}}}}
  • {{ #ifeq:1.234567890120E12{{#if:|{{{{{1n}}}}}}}{{#ifeq:0|0||1.23456789012E+12|1|0|{{#ifeq:1|1||1.23456789012E+12|1|{{#ifeq:1.23456789012E+12|1.23456789012E+12||1.23456789012E+12|}}}}}}}} gives {{#ifeq:0|0|{{#ifeq:1.234567890120E12|1.23456789012E+12|1|0}}|{{#ifeq:1|1|{{#ifeq:1.234567890120E12|1.23456789012E+12|1}}|{{#ifeq:1.23456789012E+12|1.23456789012E+12|{{#ifeq:1.234567890120E12|1.23456789012E+12}}|{{#ifeq:1.234567890120E12}}}}}}}}
    Numerical comparisons don't depend on the output format, compare:
  • {{ #expr:1234567890123{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:1234567890123|||}}|{{#ifeq:-|+|{{#expr:1234567890123||}}|{{#ifeq:-|+|{{#expr:1234567890123|}}|{{#expr:1234567890123}}}}}}}}
  • {{ #expr:1234567890120{{#if:|{{{{{1n}}}}}}}{{#ifeq:+|-|||||{{#ifeq:-|+||||{{#ifeq:-|+|||}}}}}}}} gives {{#ifeq:-|+|{{#expr:1234567890120|||}}|{{#ifeq:-|+|{{#expr:1234567890120||}}|{{#ifeq:-|+|{{#expr:1234567890120|}}|{{#expr:1234567890120}}}}}}}}

#ifeq: allows to compare strings containing equal signs:

#switch: result #ifeq: result
{{{{{1n}}}}}}}{{#ifeq:+|-||-1=okay|fail||{{#ifeq:fail|fail||-1=okay|fail|{{#ifeq:-1=okay|-1=okay||-1=okay|}}}}}}}} </td><td> {{#ifeq:-|+|{{#switch:-1.0|-1=okay|fail|}}|{{#ifeq:fail|fail|{{#switch:-1.0|-1=okay|fail}}|{{#ifeq:-1=okay|-1=okay|{{#switch:-1.0|-1=okay}}|{{#switch:-1.0}}}}}}}} {{{{{1n}}}}}}}{{#ifeq:fail|fail||-1|okay|fail|{{#ifeq:okay|okay||-1|okay|{{#ifeq:-1|-1||-1|}}}}}}}} </td><td> {{#ifeq:fail|fail|{{#ifeq:-1.0|-1|okay|fail}}|{{#ifeq:okay|okay|{{#ifeq:-1.0|-1|okay}}|{{#ifeq:-1|-1|{{#ifeq:-1.0|-1}}|{{#ifeq:-1.0}}}}}}}}
{{{{{1n}}}}}}}{{#ifeq:+|-||a=b=okay|fail||{{#ifeq:fail|fail||a=b=okay|fail|{{#ifeq:a=b=okay|a=b=okay||a=b=okay|}}}}}}}} </td><td> {{#ifeq:-|+|{{#switch:a=b|a=b=okay|fail|}}|{{#ifeq:fail|fail|{{#switch:a=b|a=b=okay|fail}}|{{#ifeq:a=b=okay|a=b=okay|{{#switch:a=b|a=b=okay}}|{{#switch:a=b}}}}}}}} {{{{{1n}}}}}}}{{#ifeq:fail|fail||a=b|okay|fail|{{#ifeq:okay|okay||a=b|okay|{{#ifeq:a=b|a=b||a=b|}}}}}}}} </td><td> {{#ifeq:fail|fail|{{#ifeq:a=b|a=b|okay|fail}}|{{#ifeq:okay|okay|{{#ifeq:a=b|a=b|okay}}|{{#ifeq:a=b|a=b|{{#ifeq:a=b|a=b}}|{{#ifeq:a=b}}}}}}}}
{{{{{1n}}}}}}}{{#ifeq:+|-||a=b=fail|okay||{{#ifeq:okay|okay||a=b=fail|okay|{{#ifeq:a=b=fail|a=b=fail||a=b=fail|}}}}}}}} </td><td> {{#ifeq:-|+|{{#switch:a=c|a=b=fail|okay|}}|{{#ifeq:okay|okay|{{#switch:a=c|a=b=fail|okay}}|{{#ifeq:a=b=fail|a=b=fail|{{#switch:a=c|a=b=fail}}|{{#switch:a=c}}}}}}}} {{{{{1n}}}}}}}{{#ifeq:okay|okay||a=b|fail|okay|{{#ifeq:fail|fail||a=b|fail|{{#ifeq:a=b|a=b||a=b|}}}}}}}} </td><td> {{#ifeq:okay|okay|{{#ifeq:a=c|a=b|fail|okay}}|{{#ifeq:fail|fail|{{#ifeq:a=c|a=b|fail}}|{{#ifeq:a=b|a=b|{{#ifeq:a=c|a=b}}|{{#ifeq:a=c}}}}}}}}

[edit] Length of expressions

To find the absolute value of a numeric expression x without using a separate template at least doubles the length of the expression:

  • x*(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.

[edit] Error messages

Examples for all known #expr: and #ifexpr: error messages
Expression Error message
{{ #expr:1/0}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr:1/0}}
{{ #expr:2*}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr:2*}}
{{ #expr:1 2}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr:1 2}}
{{ #ifexpr:1*/2}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#ifexpr:1*/2}}
{{ #expr: 1 (2)}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr: 1 (2)}}
{{ #expr: (1 }} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr: (1 }}
{{ #expr: 1) }} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr: 1) }}
{{ #expr:2*123,456}} {{#ifeq: +|-|{{{2}}}|   </td><td>}} {{#expr:2*123,456}}
{{ #expr:{{{a}}}}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr:{{{a}}}}}
{{ #ifexpr:3%2}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#ifexpr:3%2}}
{{ #ifexpr:abc}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#ifexpr:abc}}
{{ #expr:abc.def}} {{#ifeq: +|-|{{{2}}}|</td><td>}} {{#expr:abc.def}}
{{ #expr:{{ x|102|1000*}} 18 }} gives {{#ifeq:Eugene_Neighbors|Meta|{{#expr:Template:X18}}|{{#expr:1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*18}}}}
{{ #expr:{{ x|102|1000*}} 179 }} gives {{#ifeq:Eugene_Neighbors|Meta|{{#expr:Template:X179}}|{{#expr:1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*179}}}}
{{ #expr:{{ x|102|1000*}} 180 }} gives {{#ifeq:Eugene_Neighbors|Meta|{{#expr:Template:X180}}|{{#expr:1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*180}}}} (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 {{#ifeq:Eugene_Neighbors|Meta|{{#expr:Template:X1Template:X}}|{{#expr:(1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+(1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))}}}}
{{ #expr:{{ x|34|(1+(}} 1 {{ x|34|))}} }} gives {{#ifeq:Eugene_Neighbors|Meta|{{#expr:Template:X1Template:X}}|{{#expr:(1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+(1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))}}}}
{{ #expr:3.4.5.6}} {{#ifeq: +|-|{{{2}}}|gives}} {{#expr:3.4.5.6}}   (no feature, only an oddity)

Note: Template:Links-core 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}}} {{#expr:2*3}}} (one closing brace too many; the last of the three is interpreted as plain text, so that the rest works fine)
{{{#expr:2*3}} {{{#expr:2*3}} (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
{{#expr:2*{{#ifexpr:3*4>10|toolarge|3*4}}}},
{{ #expr: 2*{{ #ifexpr: 3*4>10|too large|3*4 }} }} gives
{{#expr:2*{{#ifexpr:3*4>10|too large|3*4}}}}.

[edit] See also

{{#if: Help|{{#if: Help|Template:Ph:Calculation}}

[edit] Links to other help pages

Help contents (all help pages)
Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s:
Versions of this help page (for other languages see below)
Meta | Wikinews | {{#if:|{{#if:Wikipedia|Wikipedia|w:Help:Calculation#}}|{{#if:Wikipedia|Wikipedia|w:Help:Calculation}}}}http://en.wikipedia.org{{#if:|/wiki/Help:Calculation#|/wiki/Help:Calculation}} | Wikiquote | Wiktionary
Links to this page from Meta (int) Meta (ext) 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 | Editing toolbar | Export | Import
Editing tips and tricks | Editing 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 | Wikitext reference | Reference card
HTML in wikitext | HTML elements
List | Table | Sorting
Special input and output
Inputbox | Special characters | Displaying a formula | Images and other uploaded files | 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 | Maintenance | Merging and moving pages | Protecting pages
Resolving disputes | Deleting a page
Special pages
Talk page | Testing | Sandbox
Lists of resources
Lists of Categories | Copyrights | Infobox
Redirect | Reference Desk | Shortcuts | Stub types

Other languages: Template:H-langs:Calculation |{{#if: Help|Template:Ph:Calculation}}

[edit] Links to other help pages

Help contents (all help pages)
Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s:
Versions of this help page (for other languages see below)
Meta | Wikinews | {{#if:|{{#if:Wikipedia|Wikipedia|w:Help:Calculation#}}|{{#if:Wikipedia|Wikipedia|w:Help:Calculation}}}}http://en.wikipedia.org{{#if:|/wiki/Help:Calculation#|/wiki/Help:Calculation}} | Wikiquote | Wiktionary
Links to this page from Meta (int) Meta (ext) 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 | Editing toolbar | Export | Import
Editing tips and tricks | Editing 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 | Wikitext reference | Reference card
HTML in wikitext | HTML elements
List | Table | Sorting
Special input and output
Inputbox | Special characters | Displaying a formula | Images and other uploaded files | 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 | Maintenance | Merging and moving pages | Protecting pages
Resolving disputes | Deleting a page
Special pages
Talk page | Testing | Sandbox
Lists of resources
Lists of Categories | Copyrights | Infobox
Redirect | Reference Desk | Shortcuts | Stub types

Other languages: Template:H-langs:Calculation }}

Personal tools