Mathematical functions allow expressions to be calculated before being used. While calculations can be done on-wiki, this is useful for templates where there are parameter variables, or for cases where a value is being modified for presentational purposes, though how we decide to present the data may change (e.g. percentage or rounding).
The main mathematical function is {{#expr}}, which is currently the base for all other mathematical functions with exception to those instead built on {{#ifexpr}}.
#expr
The {{#expr}} function allows equations to be calculated and displayed on the page. It allows arithmetic operations, and also logical operations (that return either 1 or 0). The logical operations could be used in an {{#ifeq}} to match either 1 or 0, however {{#ifexpr}} has these capabilities already built in.
Numbers
Any number is allowed, though numbers containing commas cause an error.
{{#expr:1,000}}→ Expression error: Unrecognized punctuation character ",".
To avoid an error, {{formatnum}} can be used with a parameter value of R to do reverse formatting.
{{formatnum:1,000|R}}→ 1000
And therefore the value can be used in {{#expr}}:
{{#expr:→ 1000{{formatnum:1,000|R}}}}
Decimals values are allowed. There are also other allowed keyword values that print specific numbers. These are:
| Operator | Meaning | Value |
|---|---|---|
| e | Euler's number | 2.718281828459 |
| pi | Pi | 3.1415926535898 |
2pi does not work, and instead one would have to use 2 * pi.
Arithmetic
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction. |
| * | Multiplication |
| / | Division. |
| ^ | Exponentiation. |
| mod | Modulus. |
| round | Rounding. |
As examples:
{{#expr:5}}→ 5{{#expr:5 + 2}}→ 7{{#expr:5 - 2}}→ 3{{#expr:5 * 2}}→ 10{{#expr:5 / 2}}→ 2.5{{#expr:5 ^ 2}}→ 25{{#expr:5 mod 2}}→ 1{{#expr:5.6789 round 2}}→ 5.68{{#expr:5678.9 round -1}}→ 5680
Functions
| Operator | Meaning | Example |
|---|---|---|
| ceil | Rounds up to nearest integer. | |
| floor | Rounds down to nearest integer. | |
| trunc | Removes decimal value. | |
| abs | Forces value to be positive. | |
| sin | Sine of a radian angle. |
|
| cos | Cosine of a radian angle. |
|
| tan | Tangent of a radian angle. |
|
| asin | Inverse sine; returns a radian angle. |
|
| acos | Inverse cosine; returns a radian angle. |
|
| atan | Inverse tangent, returns a radian angle. |
|
| ln | The natural logarithm (base e). |
|
Logic
{{#expr}} can use logic, and a true result outputs 1 and a false result outputs 0.
| Operator | Meaning |
|---|---|
| = | Equal to. |
| < | Less than. |
| <= | Less than or equal to. |
| > | Greater than. |
| >= | Greater than or equal to. |
| != | Not equal to. |
| <> | Not equal to. |
{{#expr:5 < 6}}→ 1{{#expr:5 > 6}}→ 0{{#expr:5 = 6}}→ 0{{#expr:5 != 6}}→ 1{{#expr:5 <> 6}}→ 1{{#expr:5 = 5}}→ 1{{#expr:5 < 5}}→ 0{{#expr:5 > 5}}→ 0{{#expr:5 <= 5}}→ 1{{#expr:5 >= 5}}→ 1{{#expr:5 <> 5}}→ 0{{#expr:5 != 5}}→ 0
Each side of the equation can still use normal expression operators such as addition and modulus.
There are also and and or operators available that allow multiple conditions to be tested.
{{#expr:5 > 6 and 7 > 8}}→ 0{{#expr:5 < 6 and 7 > 8}}→ 0{{#expr:5 < 6 and 7 < 8}}→ 1{{#expr:5 > 6 or 7 > 8}}→ 0{{#expr:5 < 6 or 7 > 8}}→ 1{{#expr:5 < 6 or 7 < 8}}→ 1
Any number can be stacked:
{{#expr:5 < 6 and 7 < 8 and 9 = 9}}→ 1{{#expr:5 = 5 or 5 = 6 or 5 = 7}}→ 1{{#expr:(5 = 6 and 6 = 5) or 1 = 1}}→ 1
The not operators gives the opposite result.
{{#expr:not (5 < 6)}}→ 0
%
The {{%}} function converts a fraction into percentage form. There are two potential implementations:
The function accepts valid numbers.
The fraction is automatically rounded to 0dp. Another parameter allows the decimal places to be stated.
{{%|50/201|4}}→ 24.8756%
It does not show more 0s than necessary
{{%|1|2|4}}→ 50%
Expressions can be used as numbers without the {{#expr}} function, though it can lead to undesired results so it is suggested that {{#expr}} is used.
The template can be substituted to print the output onto the page.
cap
The {{cap}} function determines how high or low a value can be.
{{cap|(value =) value | min = minimum cap | max = maximum cap}}
The min and max parameters are both optional, though without one of them the function will just return the original value.
{{cap|4}}→ 4{{cap|4|min=5}}→ 5{{cap|4|max=5}}→ 4{{cap|4|min=3}}→ 4{{cap|4|max=3}}→ 3{{cap|2|min=3|max=5}}→ 3{{cap|3|min=3|max=5}}→ 3{{cap|4|min=3|max=5}}→ 4{{cap|5|min=3|max=5}}→ 5{{cap|6|min=3|max=5}}→ 5
There is error handling:
{{cap|x|min=5}}→ 5{{cap|4|min=y}}→ Error:min(y) not a valid number{{cap|4|max=z}}→ Error:max(z) not a valid number{{cap|4|min=5|max=3}}→ Error:minvalue cannot be greater thanmaxvalue
And the following does not throw an error since no calculations are required:
{{cap|x}}→ x
{{#expr}} must be called to run calculations on the values.
{{cap|→ 150{{#expr:50*2}}|min={{#expr:50*3}}|max={{#expr:50*5}}}}{{cap|→ 200{{#expr:50*4}}|min={{#expr:50*3}}|max={{#expr:50*5}}}}{{cap|→ 250{{#expr:50*6}}|min={{#expr:50*3}}|max={{#expr:50*5}}}}
The function can be substituted.