finalfantasy

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:{{formatnum:1,000|R}}}} → 1000

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.
{{#expr:ceil(2.2)}}3
{{#expr:ceil(2.5)}}3
{{#expr:ceil(2.8)}}3
{{#expr:ceil(-2.2)}}-2
{{#expr:ceil(-2.5)}}-2
{{#expr:ceil(-2.8)}}-2
floor Rounds down to nearest integer.
{{#expr:floor(2.2)}}2
{{#expr:floor(2.5)}}2
{{#expr:floor(2.8)}}2
{{#expr:floor(-2.2)}}-3
{{#expr:floor(-2.5)}}-3
{{#expr:floor(-2.8)}}-3
trunc Removes decimal value.
{{#expr:trunc(2.2)}}2
{{#expr:trunc(2.5)}}2
{{#expr:trunc(2.8)}}2
{{#expr:trunc(-2.2)}}-2
{{#expr:trunc(-2.5)}}-2
{{#expr:trunc(-2.8)}}-2
abs Forces value to be positive.
{{#expr:abs(5)}}5
{{#expr:abs(-5)}}5
sin Sine of a radian angle.
{{#expr:sin(pi/2)}}1
cos Cosine of a radian angle.
{{#expr:cos(pi)}}-1
tan Tangent of a radian angle.
{{#expr:tan(pi/3)}}1.7320508075689
asin Inverse sine; returns a radian angle.
{{#expr:asin(1)}}1.5707963267949
acos Inverse cosine; returns a radian angle.
{{#expr:acos(1)}}0
atan Inverse tangent, returns a radian angle.
{{#expr:atan(1)}}0.78539816339745
ln The natural logarithm (base e).
{{#expr:ln(2)}}0.69314718055995


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:

{{%|fraction}}
{{%|numerator | denominator}}

The function accepts valid numbers.

{{%|1/2}}50%
{{%|50|201}}25%

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.

{{%|{{#expr:2/3}}/1}}67%
{{%|1/{{#expr:2/3}}}}150%

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: min value cannot be greater than max value

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|{{#expr:50*2}}|min={{#expr:50*3}}|max={{#expr:50*5}}}} → 150
{{cap|{{#expr:50*4}}|min={{#expr:50*3}}|max={{#expr:50*5}}}} → 200
{{cap|{{#expr:50*6}}|min={{#expr:50*3}}|max={{#expr:50*5}}}} → 250

The function can be substituted.