This site is supported by donations to The OEIS Foundation.

Help:Calculation

From OeisWiki
Jump to: navigation, search


This article is under construction.            

Please do not rely on any information it contains.            


The precision and format of numerics varies with the server; even on a single page, while for any rendering there is consistency within the page, there is no consistency among renderings.


The MediaWiki extension ParserFunctions enables users to perform simple mathematical computations.

The #expr: parser function (see also the {{expr}} mathematical function template) evaluates numerical expressions, and also boolean expressions involving numbers and booleans (not strings). The syntax is

{{#expr: expression }}

The spaces shown in the syntax above are not required, but spaces within numbers are not allowed.

The supported operators (roughly in order of precedence, from highest to lowest) are

  1. not,
  2. unary +, unary -,
  3. ^,
  4. *, /, div, mod,
  5. binary +, binary -,
  6. round,
  7. =, <> or !=, <=, >=, <, >,
  8. and,
  9. or.

Maximum and minimum numbers allowed

Cf. Maximum and minimum numbers allowed.

Operators

No operators
Operator/function template Operation Examples
none none
{{#expr: 123456789012345 }} 1.2345678901234E+14
{{#expr: 1.23456789012345 }} 1.2345678901235
{{#expr: .000001 }} 1.0E-6
{{#expr: .00000123456789012345 }} 1.2345678901234E-6
{{#expr: .123e4 }} 1230
{{#expr: 123E-2 }} 1.23
{{#expr: -123E-2 }} -1.23
{{#expr: -9.5E300 }} -9.5E+300
{{#expr: -9.5E-300 }} -9.5E-300
{{#expr: e }} 2.718281828459
{{#expr: pi }} 3.1415926535898
{{expr|pi}} 3.1415926535898

Wrong:

{{#expr: e4 }} Expression error: Unexpected number.

Sgn function template and abs/trunc/floor/ceil operators

Sgn function template and Abs/trunc/floor/ceil operators
Operator/function template Operation Examples
sgn (not supported)

{{sgn}}

Sign function
{{#expr: sgn -1.2 }} Expression error: Unrecognized word "sgn".
{{#expr: sgn 0 }} Expression error: Unrecognized word "sgn".
{{#expr: sgn 1.2 }} Expression error: Unrecognized word "sgn".
{{sgn|-1.2}} -1
{{sgn|0}} 0
{{sgn|1.2}} 1
abs

{{abs}}

Absolute value
{{#expr: abs -1.2 }} 1.2
{{#expr: abs 0 }} 0
{{#expr: abs 1.2 }} 1.2
{{math|{{abs|{{op|-}}1.2}} {{=}} {{abs|{{op|-}}1.2|#}}|&}}
|  − 1.2 |
= 1.2
{{math|{{abs|0}} {{=}} {{abs|0|#}}|&}}
| 0 |
= 0
{{math|{{abs|1.2}} {{=}} {{abs|1.2|#}}|&}}
| 1.2 |
= 1.2
trunc Truncation (rounding towards 0)
{{#expr: trunc 1.2 }} 1
{{#expr: trunc -1.2 }} -1
{{trunc|1.2}} Template:Trunc
{{trunc|-1.2}} Template:Trunc
floor

{{floor}}

Floor function (rounding towards )
{{#expr: floor 1.2 }} 1
{{#expr: floor -1.2 }} -2
{{math|{{floor|1.2}} {{=}} {{floor|1.2|#}}|&}}
⌊  1.2⌋
= 1
{{math|{{floor|{{op|-}}1.2}} {{=}} {{floor|{{op|-}}1.2|#}}|&}}
⌊   − 1.2⌋
=  − 2
ceil

{{ceil}}

Ceiling function (rounding towards )
{{#expr: ceil 1.2 }} 2
{{#expr: ceil -1.2 }} -1
{{math|{{ceil|1.2}} {{=}} {{ceil|1.2|#}}|&}}
⌈ 1.2⌉
= 2
{{math|{{ceil|{{op|-}}1.2}} {{=}} {{ceil|{{op|-}}1.2|#}}|&}}
⌈  − 1.2⌉
=  − 1

Exponential/logarithmic operators and function templates

Exponential/logarithmic operators
Operator/function template Operation Examples
exp Natural exponential
{{#expr: exp 1.2 }} 3.3201169227365
{{#expr: e^1.2 }} 3.3201169227365
{{exp|1.2}} Template:Exp
ln

log (not supported)

{{log}} or {{ln}} or {{log_e}} (base e)


{{log_2}} (base 2)

{{log_10}} (base 10)

{{log_b}} (base b)


Natural logarithm



Binary logarithm

Decimal logarithm

Logarithm base b

{{#expr: ln 2 }} 0.69314718055995
{{#expr: log 2 }} Expression error: Unrecognized word "log".
{{ln|2}} 0.69314718055995
{{log|2}} 0.69314718055995
{{log_e|2}} 0.69314718055995

Hence, the binary logarithm log_2 of e.g. 10:

{{log_2|10}} 3.3219280948874
{{#expr: ln 10 / ln 2 }} 3.3219280948874

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

{{log_10|2}} 0.30102999566398
{{#expr: ln 2 / ln 10 }} 0.30102999566398

Hence, the base logarithm log_b of e.g. 2 (base 5):

{{log_b|2|5}} 0.43067655807339
{{log_b|2|base = 5}} 0.43067655807339
{{#expr: ln 2 / ln 5 }} 0.43067655807339

Trigonometric operators and function templates

Circular operators and function templates

Circular operators and function templates
Operator/function template Operation Examples
sin

{{sin}}

Sine
{{#expr: sin 0.1 }} 0.099833416646828
{{sin|0.1}} 0.099833416646828

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

{{#expr: sin(30*pi/180) }} 0.5
{{sin|30*pi/180}} 0.5
cos

{{cos}}

Cosine
{{#expr: cos 0.1 }} 0.99500416527803
{{cos|0.1}} 0.99500416527803
tan

{{tan}}

Tangent
{{#expr: tan 0.1 }} 0.10033467208545
{{tan|0.1}} 0.10033467208545
csc (not supported)

{{csc}}

Cosecant
{{#expr: 1 / (sin 0.1) }} 10.016686131635
{{#expr: csc 0.1 }} Expression error: Unrecognized word "csc".
{{csc|0.1}} Template:Csc
sec (not supported)

{{sec}}

Secant
{{#expr: 1 / (cos 0.1) }} 1.0050209184005
{{#expr: sec 0.1 }} Expression error: Unrecognized word "sec".
{{sec|0.1}} Template:Sec
cot (not supported)

{{cot}}

Cotangent
{{#expr: 1 / (tan 0.1) }} 9.9666444232592
{{#expr: cot 10 }} Expression error: Unrecognized word "cot".
{{cot|10}} Template:Cot
arcsin

asin

{{arcsin}}

{{asin}}

Arc sine
{{#expr: arcsin 0.1}} Expression error: Unrecognized word "arcsin".
{{#expr: asin 0.1}} 0.10016742116156
{{arcsin|0.1}} 0.10016742116156
{{asin|0.1}} 0.10016742116156
arccos

acos

{{arccos}}

{{acos}}

Arc cosine
{{#expr: arccos 0.1}} Expression error: Unrecognized word "arccos".
{{#expr: acos 0.1}} 1.4706289056333
{{arccos|0.1}} 1.4706289056333
{{acos|0.1}} 1.4706289056333
arctan

atan

{{arctan}}

{{atan}}

Arc tangent
{{#expr: arctan 0.1}} Expression error: Unrecognized word "arctan".
{{#expr: atan 0.1}} 0.099668652491162
{{arctan|0.1}} 0.099668652491162
{{atan|0.1}} 0.099668652491162
arctan2 (not supported)

atan2 (not supported)

{{arctan2}}

{{atan2}}

Four quadrant arc tangent


(default cut at π radians, cut = odd_number)
(cut at radians, cut = even_number)

{{atan2|y = 0|x = 1}} 0
{{atan2|y = 1|x = 0}} 1.5707963267949
{{atan2|y = 0|x = -1}} 3.1415926535898
{{atan2|y = -1|x = 0}} -1.5707963267949
{{atan2|y = 0|x = 1|cut = 2}} 0
{{atan2|y = 1|x = 0|cut = 2}} 1.5707963267949
{{atan2|y = 0|x = -1|cut = 2}} 3.1415926535898
{{atan2|y = -1|x = 0|cut = 2}} 4.7123889803847
arccsc (not supported)

acsc (not supported)

{{arccsc}}

{{acsc}}

Arc cosecant
{{#expr: asin (1 / 10)}} 0.10016742116156
{{#expr: acsc 10}} Expression error: Unrecognized word "acsc".
{{asin|1 / 10}} 0.10016742116156
{{acsc|10}} Template:Acsc
arcsec (not supported)

asec (not supported)

{{arcsec}}

{{asec}}

Arc secant
{{#expr: acos (1 / 10)}} 1.4706289056333
{{#expr: asec 10}} Expression error: Unrecognized word "asec".
{{acos|1 / 10}} 1.4706289056333
{{asec|10}} Template:Asec
arccot (not supported)

acot (not supported)

{{arccot}}

{{acot}}

Arc cotangent
{{#expr: atan (1 / 10)}} 0.099668652491162
{{#expr: acot 10 }} Expression error: Unrecognized word "acot".
{{atan|1 / 10}} 0.099668652491162
{{acot|10}} Template:Acot

Hyperbolic operators and function templates

Hyperbolic operators and function templates
Operator/function template Operation Examples
sinh (not supported)

{{sinh}}

Hyperbolic sine
{{#expr: sinh 0.1}} Expression error: Unrecognized word "sinh".
{{sinh|0.1}} 0.10016675001984
cosh (not supported)

{{cosh}}

Hyperbolic cosine
{{#expr: cosh 0.1}} Expression error: Unrecognized word "cosh".
{{cosh|0.1}} 1.0050041680558
tanh (not supported)

{{tanh}}

Hyperbolic tangent
{{#expr: tanh 0.1}} Expression error: Unrecognized word "tanh".
{{tanh|0.1}} 0.099667994624956
csch (not supported)

{{csch}}

Hyperbolic cosecant
{{#expr: csch 0.1}} Expression error: Unrecognized word "csch".
{{csch|0.1}} Template:Csch
sech (not supported)

{{sech}}

Hyperbolic secant
{{#expr: sech 0.1}} Expression error: Unrecognized word "sech".
{{sech|0.1}} Template:Sech
coth (not supported)

{{coth}}

Hyperbolic cotangent
{{#expr: coth 0.1}} Expression error: Unrecognized word "coth".
{{coth|0.1}} 10.033311132254
arsinh (not supported)

asinh (not supported)

{{arsinh}}

{{asinh}}

Inverse hyperbolic sine
{{#expr: arsinh 0.1}} Expression error: Unrecognized word "arsinh".
{{#expr: asinh 0.1}} Expression error: Unrecognized word "asinh".
{{arsinh|0.1}} 0.099834078899208
{{asinh|0.1}} Template:Asinh
arcosh (not supported)

acosh (not supported)

{{arcosh}}

{{acosh}}

Inverse hyperbolic cosine
{{#expr: arcosh 0.1}} Expression error: Unrecognized word "arcosh".
{{#expr: acosh 0.1}} Expression error: Unrecognized word "acosh".
{{arcosh|3.1}} 1.7974565682677
{{acosh|3.1}} Template:Acosh
artanh (not supported)

atanh (not supported)

{{artanh}}

{{atanh}}

Inverse hyperbolic tangent
{{#expr: artanh 0.1}} Expression error: Unrecognized word "artanh".
{{#expr: atanh 0.1}} Expression error: Unrecognized word "atanh".
{{artanh|0.1}} 0.10033534773108
{{atanh|0.1}} Template:Atanh
arcsch (not supported)

acsch (not supported)

{{arcsch}}

{{acsch}}

Inverse hyperbolic cosecant
{{#expr: arcsch 0.1}} Expression error: Unrecognized word "arcsch".
{{#expr: acsch 0.1}} Expression error: Unrecognized word "acsch".
{{arcsch|0.1}} Template:Arcsch
{{acsch|0.1}} Template:Acsch
arsech (not supported)

asech (not supported)

{{arsech}}

{{asech}}

Inverse hyperbolic secant
{{#expr: arsech 0.1}} Expression error: Unrecognized word "arsech".
{{#expr: asech 0.1}} Expression error: Unrecognized word "asech".
{{arsech|3.1}} Template:Arsech
{{asech|3.1}} Template:Asech
arcoth (not supported)

acoth (not supported)

{{arcoth}}

{{acoth}}

Inverse hyperbolic cotangent
{{#expr: arcoth 0.1}} Expression error: Unrecognized word "arcoth".
{{#expr: acoth 0.1}} Expression error: Unrecognized word "acoth".
{{arcoth|0.1}} Template:Arcoth
{{acoth|0.1}} Template:Acoth

Unary arithmetic operators

Unary arithmetic operators
Operator/function template Operation Examples
+ Unary + sign
{{#expr: +12 }} 12
{{#expr: ++12 }} 12
{{#expr: +-12 }} -12
- Unary - sign (negation)
{{#expr: -12 }} -12
{{#expr: -+12 }} -12
{{#expr: --12 }} 12

Unary bitwise operator

Unary bitwise operator
Operator/function template Operation Examples
{{(~)}} Bitwise NOT
{{(~)| 0 }} Template:(~)
{{(~)| 1 }} Template:(~)
{{(~)| 2 }} Template:(~)
{{(~)| 9 }} Template:(~)

Unary logical operator

Unary logical operator
Operator/function template Operation Examples
not Logical NOT
{{#expr: not -1 }} 0
{{#expr: 0^(abs -1) }} 0
{{#expr: not not -1 }} 1
 
{{#expr: not 0 }} 1
{{#expr: 0^(abs 0) }} 1
{{#expr: not not 0 }} 0
 
{{#expr: not 0.3 }} 0
{{#expr: 0^(abs 0.3) }} 0
{{#expr: not not 0.3 }} 1
 
{{#expr: not 1 }} 0
{{#expr: 0^(abs 1) }} 0
{{#expr: not not 1 }} 1
 
{{#expr: not 2 }} 0
{{#expr: 0^(abs 2) }} 0
{{#expr: not not 2 }} 1

Binary arithmetic operators

Binary arithmetic operators
Operator/function template Operation Examples
^^ (not supported)

{{^^}}

Tetration:
  • tetra-powers () or
  • tetra-exponentials ()
{{#expr: 2^^7 }} Expression error: Unexpected ^ operator.
{{^^|2|4}} 65536
{{^^|3|3}} 7625597484987
{{^^|5|0}} 1
{{^^|0|0}} 1
{{^^|e|3}} 3814279.1047602
{{^^|(1/e)^e|32}} 0.53143981759252
{{^^|2|.5}} Tetration error: Second argument (height) must be a nonnegative integer
{{^^|-2|1}} -2
{{^^|-2|.5}} Tetration error: Second argument (height) must be a nonnegative integer
^ Exponentiation:
  • powers () or
  • exponentials ()
{{#expr: 2 ^ 7 }} 128
{{#expr: 2 ^ -7 }} 0.0078125
{{#expr: (-2) ^ 7 }} -128
{{#expr: (-2) ^ 8 }} 256
{{#expr: 2 ^ 1.2 }} 2.2973967099941
{{#expr: 2 ^ .5 }} 1.4142135623731
{{#expr: (-2) ^ 1.2 }} NAN
{{#expr: (-2) ^ .5 }} NAN
* Multiplication
{{#expr: 30 * 7 }} 210
{{#expr: 30 * -7 }} -210
Help:Calculation/ Real division
{{#expr: 30 / 7 }} 4.2857142857143
{{#expr: 30 / -7 }} -4.2857142857143
{{#expr: 60 / 11 }} 5.4545454545455
{{#expr: 30 / 13 }} 2.3076923076923
{{#expr: -24 / 7 }} -3.4285714285714
{{#expr: 51.5 / 7 }} 7.3571428571429
div

\ (not supported)

Integer division
Wrong results!
{{#expr: 30 \ 7 }} Expression error: Unrecognized punctuation character "\".  
{{#expr: 30 div 7 }} 4.2857142857143 (should be 4)
{{#expr: 30 div -7 }} -4.2857142857143 (should be -5)
{{#expr: 60 div 11 }} 5.4545454545455 (should be 5)
{{#expr: 30 div 13 }} 2.3076923076923 (should be 2)
{{#expr: -24 div 7 }} -3.4285714285714 (should be -4)
{{#expr: 51.5 div 7 }} 7.3571428571429 (should be 7)

Use the {{div}} template instead
{{div|30|7}} Template:Div (should be 4)
{{div|30|-7}} Template:Div (should be -5)
{{div|60|11}} Template:Div (should be 5)
{{div|30|13}} Template:Div (should be 2)
{{div|-24|7}} Template:Div (should be -4)
{{div|51.5|7}} Template:Div (should be 7)
mod

% (not supported)

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 }} 2  
{{#expr: 30 % 7 }} Expression error: Unrecognized punctuation character "%".  
{{#expr: 30 mod -7 }} 2  
{{#expr: -30 mod 7 }} -2  
{{#expr: -30 mod -7 }} -2  
{{#expr: -8 mod -3 }} -2  
{{#expr: -8 mod 2.9 }} 0 (should be 2.6)
{{#expr: -8 mod 3.2 }} -2 (should be 1.6)
{{#expr: 8.9 mod 3 }} 2 (should be 2.9)
+ Addition
{{#expr: 30 + 7 }} 37
- Subtraction
{{#expr: 30 - 7 }} 23

Binary bitwise operators

Binary bitwise operators
Operator/function template Operation Examples
{{(*)}} Bitwise AND
{{(*)| 2 | 7 }} Template:(*)
{{(^)}} Bitwise XOR
{{(^)| 2 | 7 }} Template:(^)
{{(+)}} Bitwise OR
{{(+)| 2 | 7 }} Template:(+)

Round operator

Round operator
Operator/function template Operation Examples
round Rounds off the number on the left to the power of 10-1 given on the right (PHP function round)


NOTE: The round operator uses the PHP round function with ​$mode​ default's value:

​ float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) ​
{{#expr: 4.2857 round 3 }} 4.286
{{#expr: 4.2857 round 0 }} 4
{{#expr: 42857 round -2 }} 42900
{{#expr: 4.5 round 0 }} 5
{{#expr: -4.5 round 0 }} -5

Comparison operators

Comparison operators
Operator/function template Operation Examples
= Equality (numerical equality incl. logical equality)
{{#expr: 3 = 03.0 }} 1
!= Inequality, same as <>, Logical XOR
{{#expr: 3 != 03.0 }} 0
{{#expr: 3 != 4 }} 1
<> Inequality, same as !=
{{#expr: 3 <> 03.0 }} 0
{{#expr: 3 <> 4 }} 1
< Less than
{{#expr: 3 < 2 }} 0
{{#expr: 3 < 3 }} 0
{{#expr: 3 < 4 }} 1
> Greater than
{{#expr: 3 > 2 }} 1
{{#expr: 3 > 3 }} 0
{{#expr: 3 > 4 }} 0
<= Less than or equal to
{{#expr: 3 <= 2 }} 0
{{#expr: 3 <= 3 }} 1
{{#expr: 3 <= 4 }} 1
>= Greater than or equal to
{{#expr: 3 >= 2 }} 1
{{#expr: 3 >= 3 }} 1
{{#expr: 3 >= 4 }} 0

Binary logical operators

Binary logical operators
Operator/function template Operation Examples
and Logical AND
{{#expr: 3 and 4 }} 1
{{#expr: 0 and 3 }} 0
{{#expr: -3 and 0 }} 0
{{#expr: 0 and 0 }} 0
or Logical OR
{{#expr: 3 or 4 }} 1
{{#expr: 0 or 3 }} 1
{{#expr: -3 or 0 }} 1
{{#expr: 0 or 0 }} 0

The boolean operators consider 0 to be "false" and any nonzero number to be "true". An intermediate or final result "true" is identified with 1. Thus {{#expr: (2 < 3) + 1}} → 2. 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 }} → 3.8

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

{{#expr: 2 = 5 < 3 + 4 }} → 1

(first +, then =, then <).

{{#expr: 1.234 + 1.234 round 1 + 1 }} → 2.47

(first additions, then round)

{{#expr: 3 * 4 mod 10 * 10 }} → 20

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

Parentheses can force a different precedence: {{#expr: (2 + 3) * 4 }} → 20

Blank spaces are good for readability but not needed for working properly, except between not and an adjacent and/div/mod/not/or/round operator, and within numbers not allowed

Code Result
{{#expr: 7mod3 }} 1
{{#expr: 7.5round0 }} 8
{{#expr: 0and1 }} 0
{{#expr: 0or not0 }} 1
{{#expr: 0ornot0 }} Expression error: Unrecognized word "ornot".
{{#expr: 123 456 }} Expression error: Unexpected number.
{{#expr: not not3 }} 1
{{#expr: notnot 3 }} Expression error: Unrecognized word "notnot".
   
{{#expr: ---2 }} -2
{{#expr: -+-2 }} 2
{{#expr: 2*-3 }} -6
{{#expr: -not-not-not0 }} -1
{{#expr: 2*/3 }} Expression error: Unexpected / operator.
{{#expr: sinln1.1 }} 0.095165945236752
{{#expr: sin ln1.1 }} 0.095165945236752

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.

Code Result
{{#expr: +01.20 }} 1.2
{{#expr: 12. }} 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. Group separators are not allowed: a comma is considered an unrecognised punctuation character:

Thus a number can only consist of one or more digits, or zero or more digits, a point, and zero or more digits. (For the purpose of evaluating an expression a plus or minus sign is considered a unary operator instead of part of the number.)

Canonical form

Code Result
{{#expr: 123 }} 123
{{#expr: 123.456 }} 123.456
{{#expr: 0.456 }} 0.456
{{#expr: 0 }} 0
{{#expr: 2.0E-5 }} 2.0E-5

Accepted, although in some cases more or less odd

Code Result
{{#expr: .456 }} 0.456
{{#expr: 123. }} 123
{{#expr: 000123 }} 123
{{#expr: 123.456.789 }} 123.456
{{#expr: .}} 0
{{#expr: 2e-5 }} 2.0E-5
{{#expr: 2E-5 }} 2.0E-5
{{#expr: E }} 2.718281828459
{{#expr: e }} 2.718281828459
{{#expr: E-5 }} -2.281718171541
{{#expr: e-5 }} -2.281718171541
{{#expr: 1e-5 }} 1.0E-5

Wrong:

Code Result
{{#expr: 123,456 }} Expression error: Unrecognized punctuation character ",".
{{#expr: 123 456 }} Expression error: Unexpected number.

Due to the specifier R ("raw"), {{NUMBEROFARTICLES:R}} → 179160 etc., i.e. numbers without group separators, which can be used in computations.

As opposed to ParserFunctions, "Template:Ml" accepts points and commas in numbers and interprets them in a site-language-specific way (depending on $separatorTransformTable in Messagesxx.php); on this site

Code Result
{{plural: 1.000 |a|b|c}} a
{{plural: 1,000 |a|b|c}} 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.

Numbers as output

A non-integer result has a decimal point in it. Scientific notation is produced for numbers with small absolute value ( less than 1E-4) and for numbers with large absolute value ( greater than or equal to 1E+12; however, even within MediaWiki there is no uniformity, larger numbers in 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)

Code Result
{{#expr: .000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123456789012345 }} 1.2345678901234E-106
{{#expr: .00000123456789012345 }} 1.2345678901234E-6
{{#expr: .0000123456789012345 }} 1.2345678901235E-5
{{#expr: .000123456789012345 }} 0.00012345678901234
{{#expr: 123456789012.345 }} 123456789012.35
{{#expr: 1234567890123.45 }} 1234567890123.4
{{#expr: 12345678901234.5 }} 12345678901234
{{#expr: 123456789012345 }} 1.2345678901234E+14
{{#expr: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456 }} 1.2345678901235E+105
{{#expr: 1.234e4 }} 12340
{{#expr: 1.234e-4 }} 0.0001234

For negative numbers simply a minus sign is prefixed

{{#expr: -123456789012345 }} gives -1.2345678901234E+14

Long YYYYMMDDhhmmss timestamps can run into the same problem:

{{CURRENTTIMESTAMP}} gives 20181019034000,
{{#expr:|CURRENTTIMESTAMP}} gives .

Timestamps without seconds are 12 digits, just short enough:

{{#expr: {{CURRENTTIMESTAMP}} / 100 }} gives 201810190340

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. {{roote}} and {{rooteval}}; for convenience, for the final result another version can be made simply calling the other one and applying #expr: to the result, e.g. {{rootfineval}}.

Two methods for representing intermediate results are

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

For example, {{powe}} produces the power of a number in the form of an expression

{{powe|31.18|12}} gives Template:Powe

However, the expression can get quite long, for {{roote}} 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 a linked expression gives Expression error: Unrecognized punctuation character "["., but it can also be used in a further computation, unlike the value just mentioned.

2. Writing numbers splitting off factors which are powers of 10 but written plainly.

Example

{{rooteval|.0000000000001}} gives Template:Rooteval

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 {{rootfineval}}, giving

Template:Fineval, but the result can also be used in a further computation.

Compare

{{root|.0000000000001}} giving
2  .0000000000001

This uses the same method, but with intermediate application of #expr:, giving an error because of intermediate 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
{{#expr: 1*(-0.2 round 0) }} gives -0
{{#expr: 0+(-0.2 round 0) }} gives 0

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

Precision of numerics

The precision and format of numerics varies with the operating system of the MediaWiki server.

The precision of the result of #expr: is less than internally used within the computation of an expression.

Code Result Comment
Precision of numerics with OEIS Wiki
{{#expr: 1234567891.1234567891 - 1234567891 }} 0.12345671653748 (internally using 1 + 52 = 53 bits significand; 53 × log10(2) = 15.954589770191 or 16 decimal digits significand)
{{#expr: 1234567891.1234567891 }} 1234567891.1235 (result is rounded to a maximum of 14 decimal digits significand)
{{#expr: (2/3) * 1000000 - 666666 }} 0.66666666662786 (internally using 1 + 52 = 53 bits significand; 53 × log10(2) = 15.954589770191 or 16 decimal digits significand)
{{#expr: (2/3) }} 0.66666666666667 (result is rounded to a maximum of 14 decimal digits significand)

The machine epsilon (smallest x such that 1 + x ≠ 1) of OEIS Wiki is 2 -52 ≅ 2.2204460492503E-16 (double precision machine epsilon).

{{truth| {{#expr: (1 + 2^(-53)) = 1 }} }} = TRUE
{{truth| {{#expr: (1 + 2^(-52)) = 1 }} }} = FALSE

That's the normal behaviour for Double Precision (64 bits) IEEE Floating Point Standard[1] (1 sign bit, 1 + 52 significand bits, 11 exponent bits).

The smallest expression result greater than 1 is affected by the rounding to a maximum of 14 decimal digits significand of the result

1 + 2 -44 ≅ 1 + 5.6843418860808E-14: {{#expr: 1 + 2^(-44) }} = 1.0000000000001 (result is rounded to a maximum of 14 decimal digits significand)
1 + 2 -45 ≅ 1 + 2.8421709430404E-14: {{#expr: 1 + 2^(-45) }} = 1 (result is rounded to a maximum of 14 decimal digits significand)

IEEE Floating Point Standard (Double precision)

The bit pattern of two words in a 32-bit machine represents the real number (actually a rational number)

(-1) s x 2 e-1023 x (1.f)2

where , , and .

IEEE Floating Point Standard (Double precision)[1]
sign bit biased exponent fraction from normalized significand
1 bit 11 bits 52 bits
s e f

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 Friday and so {{CURRENTDOW}} gives 5.
{{#ifexpr: 2^10 = 1024 |1|0}} yields 1

Note that rounding errors can affect a result

{{#expr: sin pi }} 1.2246467991474E-16
{{#ifexpr: sin pi = 0 |1|0}} 0
{{#ifexpr: abs sin pi < 1e-15 |1|0}} 1

Similarly

{{#expr: sin pi = 0 }} 0
{{#expr: abs sin pi < 1e-15 }} 1

Comparisons

The function #ifeq: compares numbers and strings for equality (equal if both represent the same number or both are equal strings).

The function #switch: compares one string with multiple others, and correspondingly produces one of multiple specified results.


Code Result
{{#switch: FR |UK|London|FR|Paris|NL|Amsterdam}} Amsterdam
{{#switch: UK |UK|3=GB=London|4=FR=Paris}} GB=London
{{#switch: ZZ |2=UK=London|3=FR=Paris|4=no match}}
{{#ifeq: 3 |3.0|1|0}} 1
{{#ifeq: 3 |03|1|0}} 1

Note that #ifeq: unlike #expr: accepts exponential notation on input

Code Result
{{#ifeq: 0.00003456 |3.456E-05|1|0}} 1
{{#ifeq: 1234567890123 |1234567890120|1|0}} 0
{{#ifeq: 1234567890123 |1.23456789012E+12|1|0}} 0
{{#ifeq: 1234567890120 |1.23456789012E+12|1|0}} 1
{{#ifeq: 1.234567890120E12 |1.23456789012E+12|1|0}} 1

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

Code Result
{{#expr: 1234567890123 }} 1234567890123
{{#expr: 1234567890120 }} 1234567890120

#ifeq: allows to compare strings containing equal signs

#switch: result #ifeq: result
{{#switch: -1.0 |2=-1=okay|3=fail|s=</td><td>}} {{#ifeq: -1.0 |-1|okay|fail|s=</td><td>}} okay
{{#switch: a=b |2=a=b=okay|3=fail|s=</td><td>}} {{#ifeq: a=b |2=a=b|3=okay|4=fail|s=</td><td>}} 4=fail
{{#switch: a=c |2=a=b=fail|3=okay|s=</td><td>}} {{#ifeq: a=c |2=a=b|3=fail|4=okay|s=</td><td>}} 4=okay

Length of expressions

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

x*(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)

For the pre-expand include size maximum the first method does not help, but the second does.

If the number of possible results of a long expression is small, a switch allows arbitrary conversion, including the absolute value and providing a leading zero, etc., without repeating the expression.

Error messages

Examples for all known #expr: and #ifexpr: error messages. The error texts are within the tags <strong class="error">..</strong>. Note that with the old preprocessor the "=" in "class=error" disturbs the numbering of unnamed parameters, if one of them produces an error message: {{tlx|{{#expr: 1/0 }}}} gives {{[[Template:Division by zero.|Division by zero.]]}}.


Expression Error message
{{}} {{}}
{{ #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 ( "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 }} → 3.4   (no feature, only an oddity)

Note: {{x}} 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: Unrecognized word "toolarge".,
{{ #expr: 2*{{ #ifexpr: 3*4>10|too large|3*4 }} }} gives
Expression error: Unrecognized word "too"..

See also #iferror:.

Minus sign

Only the ASCII Template:Mlw (Template:Mlw) and Template:Mlws work as minus signs; dashes are not permitted. The output, however, will always be in the form of a hyphen-minus

hyphen-minus {{#expr: -12}} -12
Unicode hyphen (U+2010, ‐) {{#expr: ‐12}} Expression error: Unrecognized punctuation character "&".
− produced by &minus; {{#expr: −12}} -12
&minus; {{#expr: &minus;12}} -12
— produced by &mdash; {{#expr: —12}} Expression error: Unrecognized punctuation character "�".
&mdash; {{#expr: &mdash;12}} Expression error: Unrecognized punctuation character "&".
– produced by &ndash; {{#expr: –12}} Expression error: Unrecognized punctuation character "�".
&ndash; {{#expr: &ndash;12}} Expression error: Unrecognized punctuation character "&".

Also many other calculation programs require a hyphen. Therefore, in order to be able to copy rendered numbers and expressions to the edit box or input them through a copy operation into other calculation programs, displayed minus signs also need to be hyphens.

Displaying numbers and numeric expressions

Guidelines such as WP:Manual of Style (dates and numbers)#Numbers focus on number display as end product. However, a point of consideration can also be the possibility to apply the rendered output to #expr or #ifexpr, or to input it without conversion into other calculation programs. This would require the following

  • use digits, not words
  • as mentioned above, use the hyphen as minus sign
  • use *, <=, and >=, not ×, ≤, or ≥
  • do not use thousands separators (however, some programs allow them)
  • use output like 6E23 or 6e23 rather than 6Template:E

Examples

Code Result
{{#expr: three }} Expression error: Unrecognized word "three".
{{#expr: 2 < 3 }} 1
{{#expr: 2 ≤ 3 }} Expression error: Unrecognized punctuation character "�".
{{#expr: 2 <= 3 }} 1
{{#expr: 2 > 3 }} 0
{{#expr: 2 ≥ 3 }} Expression error: Unrecognized punctuation character "�".
{{#expr: 2 >= 3 }} 0
{{#expr: 2 * 3 }} 6
{{#expr: 2 × 3 }} Expression error: Unrecognized punctuation character "�".
{{#expr: 2,300 }} Expression error: Unrecognized punctuation character ",".
{{#expr: 6E23 }} 6.0E+23

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

Notes

External links