Help:Calculation: Difference between revisions
mNo edit summary |
No edit summary |
||
Line 108: | Line 108: | ||
*<nowiki>{{#expr: 2 = 5 < 3 + 4 }}</nowiki> = {{#expr: 2 = 5 < 3 + 4 }} |
*<nowiki>{{#expr: 2 = 5 < 3 + 4 }}</nowiki> = {{#expr: 2 = 5 < 3 + 4 }} |
||
(first +, then |
(first +, then =, then <). |
||
==Numbers== |
==Numbers== |
Revision as of 10:58, 24 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 increasing 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.
Precedence:
- {{#expr: 2 - 3 + 4 / 5 * 6 }} = 3.8
(+ and - have equal precedence, * and / also, both higher than the former two).
- {{#expr: 2 = 5 < 3 + 4 }} = 1
(first +, then =, then <).
Numbers
In the numbers in submitted expressions a decimal point is allowed, and a non-integer result has a decimal point in it. 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: 2E-05 * 2}} gives 4.0E-5.
- {{#expr: {{#expr: 1/100000}} *2}} gives 2.0E-5.
Commas
Commas are neither accepted in input nor produced in output:
- {{#expr: 123,456}} gives Expression error: Unrecognized punctuation character ",".
This applies even though e.g. {{NUMBEROFARTICLES}} produces (in this project) 143,447, i.e., it uses comma delimiters every three digits, and in some localized versions of MediaWiki point delimiters.
If a number uses point delimiters every three digits (as in continental Europe) and the number is an integer below one million, removing the point can be done by multiplying by 1000 (#expr interprets the point as a decimal point).
See also
- Category:Mathematical templates
- Template:Eval
- Template:Evals
- w:en:Category:Mathematical templates
- Template:Example table with computations
- Template:Csn - separates digits of an integer in groups of three.
Links to other help pages
- Help contents
- Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
- Versions of this help page (for other languages see further)
- What links here on Meta or from Meta · Wikipedia · MediaWiki
- Reading
- Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
- Tracking changes
- Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
- Logging in and preferences
- Logging in · Preferences
- Editing
- Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
- Referencing
- Links · URL · Interwiki linking · Footnotes
- Style and formatting
- Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
- Fixing mistakes
- Show preview · Reverting edits
- Advanced functioning
- Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
- Others
- Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors