Jump to content

Help:Calculation: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
rm old sections (before ParserFunctions existed)
Line 111: Line 111:
but
but
*<nowiki>{{#expr: {{#expr: 1/100000}} *2}}</nowiki> gives {{#expr: {{#expr: 1/100000}} *2}}.
*<nowiki>{{#expr: {{#expr: 1/100000}} *2}}</nowiki> gives {{#expr: {{#expr: 1/100000}} *2}}.

==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 [[w:Netherlands|Netherlands]]:

<nowiki>[[google:16318199/33883|482/km²]]</nowiki>, giving [[google:16318199/33883|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:

<nowiki>{{popd|16318199|33883}}</nowiki> gives:

{{popd|16318199|33883}}


==See also==
==See also==
*[[w:en:Category:Mathematical templates]]
*[[w:en:Category:Mathematical templates]]
*{{tim|Example table with computations}}
*{{tim|Example table with computations}}
*[[Help:Template#Simple_computations|simple computations]]

Revision as of 08:32, 20 April 2006

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
{{#expr: not 0}} = 1
( ) Grouping operators {{#expr: (30 + 7) * 7 }} = 259

The boolean operators consider 0 to be false and any other number to be true. As output, "true" produces 1.

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 for numbers less than 1E-4 and for numbers greater than or equal to 1E+12 (see Template talk:Pow), 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.

See also