Help:ParserFunctions: Difference between revisions
Content deleted Content added
m Reverted changes by 217.93.91.70 (talk) to last version by Ed de Jonge |
|||
Line 22:
}}
==Functions==
The extension defines ten functions:
<tt>[[#.23expr:|expr]]</tt>,
<tt>[[#.23if:|if]]</tt>,
<tt>[[#.23ifeq:|ifeq]]</tt>,
<tt>[[#.23ifexist:|ifexist]]</tt>,
<tt>[[#.23ifexpr:|ifexpr]]</tt>,
<tt>[[#.23iferror:|iferror]]</tt>,
<tt>[[#.23switch:|switch]]</tt>,
<tt>[[#.23time:|time]]</tt>,
<tt>[[#.23rel2abs:|rel2abs]]</tt>, and
<tt>[[#.23titleparts:|titleparts]]</tt>.
<h3 style="background: beige">#expr:</h3>
* syntax: {{ft|#expr|expression}}
The <tt>expr</tt> function computes mathematical expressions based on permutations of numbers (or [[variable]]s/[[parameter]]s that translate to numbers) and operators. It does not recognize strings (use <tt>[[#.23ifeq:|ifeq]]</tt> below instead). The accuracy and format of numeric results varies with the server's operating system.
Operators are listed below. In a complex expression, operators are evaluated from left to right with the following precedence (explained in more detail in [[Help:Calculation]]):
# grouping (parentheses);
# unary (+/- signs and NOT);
# multiplicative (*, /, div, mod);
# additive (+ and -);
# round;
# comparative (=, !=, <, >, etc.);
# logical AND;
# logical OR.
When evaluating as boolean, "0" is considered false and any other non-space value is true (boolean true is represented as "{{#expr: 30 and 7}}").
Decimal numbers use "." as a decimal point (<code>[[Help:Magic words#Formatting|formatnum:]]</code> can be used to change the decimal point to a comma for the appropriate locales). Scientific notation with <tt>E</tt> plus exponent is '''meanwhile''' supported also on input for expressions (see [[Help:Calculation]]).
{| class="prettytable"
! Operator
! Operation
! Example
! Result
|-
| rowspan="2" colspan="2" align="center"| ''none''
| {{ft|#expr|123456789012345}}
| {{#expr:123456789012345}}
|-
| {{ft|#expr|0.000001}}
| {{#expr:0.000001}}
|-
!colspan="4"| Arithmetic
|-
! +
| Unary positive sign
| {{ft|#expr|+30 * +7}}
| {{#expr:+30 * +7}}
|-
!-
| Unary negative sign
| {{ft|#expr|-30 * -7}}
| {{#expr:-30 * -7}}
|-
! *
| Multiplication
| {{ft|#expr|30 * 7}}
| {{#expr:30 * 7}}
|-
! /<br />div
| Division
| {{ft|#expr|30 / 7}}<br />{{ft|#expr|30 div 7}}
| {{#expr:30 / 7}}<br />{{#expr:30 div 7}}
|-
! <tt>+</tt>
| Addition
| {{ft|#expr|30 + 7}}
| {{#expr:30 + 7}}
|-
! <tt>-</tt>
| Subtraction
| {{ft|#expr|30 - 7}}
| {{#expr:30 - 7}}
|-
!colspan="4"| Logic
|-
! not
| Unary NOT<br />logical NOT
| {{ft|#expr|not 0 * 7}}<br />{{ft|#expr|not 30 + 7}}
| {{#expr: not 0 * 7}}<br />{{#expr:not 30+7}}
|-
! and
| Logical AND
|
{{ft|#expr|4 < 5 and 4 mod 2}}
|{{#expr: 4<5 and 4 mod 2}}
|-
! or
| Logical OR
|
{{ft|#expr|4 < 5 or 4 mod 2}}
|{{#expr: 4<5 or 4 mod 2}}
|-
!colspan="4"| Comparison
|-
! =
| Equality (numerical & logical)
| {{ft|#expr|<nowiki>30 = 7</nowiki>}}
| {{#expr:30 = 7}}
|-
! <><br />!=
| Inequality, logical exclusive or
| {{ft|#expr|30 <> 7}}<br />{{ft|#expr|<nowiki>1 != 0</nowiki>}}
| {{#expr:30 <> 7}}<br />{{#expr:1 != 0}}
|-
! <
| Less than
|
{{ft|#expr|30 < 7}}
|{{#expr: 30 < 7}}
|-
! >
| Greater than
|
{{ft|#expr|30 > 7}}
|{{#expr: 30 > 7}}
|-
! <=
| Less than or equal to
|
{{ft|#expr|<nowiki>30 <= 7</nowiki>}}
|{{#expr: 30 <= 7}}
|-
! >=
| Greater than or equal to
|
{{ft|#expr|<nowiki>30 >= 7</nowiki>}}
|{{#expr: 30 >= 7}}
|-
!colspan="4"| Other
|-
! ( )
| Grouping operators
| {{ft|#expr|(30 + 7) * 7 }}
| {{#expr:(30 + 7) * 7 }}
|-
! mod
| "Modulo" truncates the numbers to whole numbers, divides the left number by the right, and returns the remainder that could not be divided.<br />Note: ''div'' and ''mod'' are different from all programming languages.
| {{ft|#expr|30 mod 7}}<br />{{ft|#expr|-8 mod -3}}<br />{{ft|#expr|8 mod 2.7}}<br />
| {{#expr:30 mod 7}}<br />{{#expr:-8 mod -3}}<br />{{#expr:8 mod 2.7}}<br />
|-
! round
| Rounds off the number on the left to the power of 1/10 given on the right.
|