Help:Calculation

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Patrick (talk | contribs) at 07:32, 20 April 2006. It may differ significantly from the current version.

With the WikiMedia extension ParserFunctions one can do computations.

The expr function computes mathematical expressions. 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:

Operator Operation Example
none {{#expr: 123456789012345}} = 1.2345678901234E+14
{{#expr: 0.000001}} = 1.0E-6
* Multiplication {{#expr: 30 * 7}} = 210
/ or div Division {{#expr: 30 / 7}} = 4.2857142857143
{{#expr: 30 div 7}} = 4.2857142857143
+ Addition {{#expr: 30 + 7}} = 37
- Subtraction (or negation) {{#expr: 30 - 7}} = 23
mod Modulo, gives the remainder of a division {{#expr: 30 mod 7}} = 2
round Rounds off the number on the left to the specified number
of digits after the decimal place, given on the right
{{#expr: 30 / 7 round 7}} = 4.2857143
Rounds off the number on the left to the specified power
of 10, given on the right
{{#expr: 1234567 round -3}} = 1235000
= Equality {{#expr: 30 = 7}} = 0
<> or != Inequality {{#expr: 30 <> 7}} = 1
< Less than {{#expr: 30 < 7}} = 0
> Greater than {{#expr: 30 > 7}} = 1
<= Less than or equal to {{#expr: 30 <= 7}} = 0
>= Greater than or equal to {{#expr: 30 >= 7}} = 1
and Logical AND {{#expr: 30 and 7}} = 1
or Logical OR {{#expr: 30 or 7}} = 1
not Logical NOT {{#expr: not 7}} = 0
( ) Grouping operators {{#expr: (30 + 7) * 7 }} = 259

The boolean operators consider 0 to be false and 1 to be true.

Numbers

In the numbers in submitted expressions a decimal point is allowed, and a non-integer result has a decimal point in it. However, commas are neither accepted in input nor produced in output. This applies even though e.g. {{NUMBEROFARTICLES}} uses comma delimiters every three digits, and in some localized versions of MediaWiki point delimiters.

Scientific notation is produced (see Template talk:Pow: output values include 0.0001, but 1E-05, 100000000000, but 1E+12), but not accepted as input.

Thus:

  • {{#expr: 1/10000 *2}} gives 0.0002
  • {{#expr: {{#expr: 1/10000}} *2}} gives 0.0002.
  • {{#expr: 1/100000 *2}} gives 2.0E-5

but

  • {{#expr: {{#expr: 1/100000}} *2}} gives 2.0E-5.

Without the extension

One can use a piped link to a calculation with Google's calculator, with the label as answer, to demonstrate the numbers used in the computation. This way one can check for consistency, see the result with a higher accuracy, and also conveniently update the result.

E.g., in the case of the population density in the Netherlands:

[[google:16318199/33883|482/km²]], giving 482/km².

With a suitable browser the evaluated expression pops up, so the link is useful even without following it.

Suppose that someone updates the population figure but not the density, than the next person can see that the density is not based on the latest figure, replace the first number, press Preview, follow the link, copy the new result to the link label, and save. Thus the update requires a minimum of effort.

Unfortunately, the result 482 has to be entered manually and consistency is not automatically maintained.

Without putting the result on the page

Alternatively, the user has to follow the link to see the result. Using template:popd is convenient and there is no redundancy, hence no worry about consistency:

{{popd|16318199|33883}} gives:

See also