Help talk:Calculation

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search


Please comment the following "bug report" (feature request) on mediazilla:6068 if I got it wrong:

MOD and DIV behave different than in any programming language I've heard of.
1: Apparently "x MOD y" is handled like "trunc(x) - trunc(y) * trunc(x / y)",
  e.g. "8.9 mod 3 = 2", "8 mod 3.2 = 2", "8 mod 2.7 = 0".
2: "x DIV y" is a misleading synonym for "x / y", no integer division.
3: A desperately needed "trunc" is missing, see m:Template talk:YMD2MJD.
Proposed "fix" (or rather feature request):

2': Let "x DIV y" return "trunc( x / y )" also known as integer division.
3': This improved DIV automagically offers "x DIV 1" to get "trunc( x )".
1': Let "x MOD y" return "x - y * (x DIV y)" using the improved DIV.

With these two fixes (not counting 2') porting algorithms like Euclid's
GCD or date + time calculations should be more straight forward.

Using two #ifexpr: plus two #expr: +/-"0.5 round 0" to emulate trunc is
a royal PITA for formulae needing dozens of signed integer divisions.

The equation x MOD y = x - y * (x DIV y) should be okay, otherwise kill
DIV, as is it's at best redundant (but more likely harmful).

-- Omniplex (w:t) 03:01, 24 May 2006 (UTC)

mod and div behave like % and / in PHP. Which is not surprising, since MediaWiki is built using PHP. Tiny green 18:34, 24 December 2006 (UTC)

Scientific notation [...] not allowed[edit]

The article states that "Scientific notation and group separators are not allowed", but suggests no workaround for cases where an inner #expr might return scientific notation itself! For instance, {{#expr:100*{{#expr:(2/3600)*3.1415926/180}}+42}} gives 42.000969627346, since {{#expr:(2/3600)*3.1415926/180}} gives 9.6962734567901E-6 [1]. :-( ... Are there any known solutions? -- Jokes Free4Me 11:35, 1 September 2007 (UTC)

In the example you do not need the inner expr.--Patrick (talk) 12:16, 1 September 2007 (UTC)
FWIW, the inner expr was in a template designed to calculate Sin(x) as a Taylor series... But i see this might not be an issue anymore. :D -- Jokes Free4Me 10:54, 4 January 2012 (UTC)

European numbers[edit]

Is there any way to set the system so as to accept european numbers (For instance english 3.14 would be written in Europe 3,14). For instance, if in an expression => 1 is used, if the number to be tested is 3,14 you would simply get an error message. Is there any way around this problem? Even if it would mean testing the characters and replacing the comma with full stop? 20:36, 28 January 2008 (UTC)

That would require an extension like mw:Extension:StringFunctions.--Patrick (talk) 23:32, 28 January 2008 (UTC)

Square root?[edit]

Is it possible to include a square root function in Wikimedia syntax? I know there are approximations using only arithmetic, but they're cumbersome, and PHP should provide a fairly straightforward way to do it. Thanks for any help. —Ryan McDaniel

Use {{#expr:x^0.5}}, e.g. {{#expr:64^0.5}} gives "8". Jimp 16:26, 11 May 2008 (UTC)

ParserFunctions Version?[edit]

The May 10th updates (power ^, logarithms, trig functions etc.) do not work on our MediaWiki 1.12 installation, although we are running ParserFunctions 1.1.1, same as Meta. Have there been updates to ParserFunctions that are not documented, or does the functionality depend on other software as well? At any rate, Help:ParserFunctions does not yet list these functions. -- 11:17, 8 September 2008 (UTC)

This may be the distinction between development version [2] and released version, see mw:Download.--Patrick (talk) 22:58, 8 September 2008 (UTC)
We've upgraded to MW 1.13.1, and now it works. -- 15:35, 10 September 2008 (UTC)

Question on calculating numbers from different areas[edit]

Hello, I was wondering if it was possible to calculate an expression using this extension with another Extension:Labeled Section Transclusion to take two numbers from two different sections to output a result in a specific place.Daedalus969 00:03, 13 October 2008 (UTC)

Yes, if we have p=12, then we can write .--Patrick (talk) 09:34, 23 June 2010 (UTC)

round endig in Zeros[edit]

I have templat that formula is (x + y)/4 and need it displayed at two decimal places. {{#expr: ((10 + 10)/4) round 2}} does not give me the 5.00 I need, but instead 5, how do I get my trailing zeros that would not also mess with 5.25 ? -- 04:05, 17 April 2009 (UTC)

Welcome to Mediawiki, with yet another way to express formulas unlike anything else you've ever seen....[edit]

I am working on a parser for mediawiki pages, and I am trying to deal with transcluding, and all the cool expressions and conditions that can be embedded in templates. My project is written in Java, so I got a copy of JEP, added it to my project, add a call to parse the #expr "12-(((0.5-(8))round 0)mod 12)" and wound up with NaN as an answer. Darn.

So I was reading the docs on the Help:Calculation page, learning about things like round being an infix operator, the trunc operator as a prefix operator, etc. I wondered if any of these unique ways to express these things were because of mediaWiki being written in PHP, but found PHP does things like round and trunc in its expressions in the usual functional call way.

So it appears that medawiki has just decided to go out and create Yet Another Math Language incompatible with all the other math expression parsers.... Sigh....

Does anyone know of a java project that can parse mediawiki expressions, or do I have to roll my own?

Thanks.... The preceding unsigned comment was added by (talk • contribs) 23:34, 1 December 2010 (UTC).

It seems fine to have just operators, having also a separate function syntax for use in an expression does not seem needed.--Patrick (talk) 00:33, 2 December 2010 (UTC)
There are some parsers listed here that understand MediaWiki syntax, including a couple for Java. Whether they support expressions and how well I don't know. Jafeluv 00:38, 2 December 2010 (UTC)
Java API (Bliki engine) mentions ParserFunctions support.--Patrick (talk) 00:54, 2 December 2010 (UTC)


Can this expression be substituted, so that when this expression occurs in #switch: in a template and when that template is substituted elsewhere, the expression is read as a number rather than an expression? 01:18, 11 February 2011 (UTC)

Never mind. I've found the solution. 01:40, 11 February 2011 (UTC)

Return length of string[edit]

How do you return the length of a string? (Or I could truncate the string and compare it to itself.) I want to trigger a maintenance category if a template parameter is a certain length. It would also work if I could evaluate whether the string contains a hyphen. Kwamikagami 10:57, 23 September 2011 (UTC)

See Help:String functions#Length of a string.--Patrick (talk) 08:41, 24 September 2011 (UTC)
Thanks! Kwamikagami 08:44, 24 September 2011 (UTC)

Dependencies? Mediawiki version?[edit]

Although I'm sure there's a valid reason for why calculations don't work for my MW installation, this help page doesn't mention when any of the listed features became available (have they always been present in MW?), nor what system requirements are needed to enable this functionality (minimum PHP version? any particular installed PHP modules? MW configuration entries?). -- Gth-au 14:21, 14 February 2012 (UTC)

Ah, showing my ignorance. I didn't think Extensions were part of the core help area, but it is obvious now why calculation would require the ParserFunctions functionality. Comprehensive documentation is also available on the ParserFunctions help page -- Gth-au 21:35, 14 February 2012 (UTC)

Work around for Expression error?[edit]

I am trying to get some calculation into one of my templates with {{#expr: but there is always the error saying "Expression error: Unrecognized punctuation character "{".". Here's what I used:

{{#expr: (({{{Bonus}}} - {{{Costs}}}) / {{{Time_hours}}}) }}

with Bonus, Costs and Time_hours as = {1, 2, 3, ... , n}

Any chance to get that solved with a work around? 14:15, 18 November 2013 (UTC)

And that is the case for the template, when you implement the template it should work, the values will appear for the parameters and then be an expression
{{#expr: ((1 - 2) / 3) }} yields -0.33333333333333
If the view of the template is concerning for you, then wrap it in <includeonly></includeonly> — billinghurst sDrewth 14:00, 18 November 2013 (UTC)
Yeah, I am used to templates that are shown different from what they are look like finally used in an article. So I checked it before I asked here in the wiki for a work around. I get the error even if I implement the template somewhere. So there's another error I don't see or a bug that has to be worked around. And that's what I am asking for :) 14:15, 18 November 2013 (UTC)
I found the error - it was a typo in the variables... 14:29, 18 November 2013 (UTC)

Calculations within a table[edit]

Is there a way for one column (or row) of a able to show results of calculations performed on other columns (or rows) of a table?

For example, it would be nice if a table containing GDP and population columns could automatically calculate the contents of a GDP-per-capita column. GPS Pilot (talk) 18:21, 27 December 2014 (UTC)

Make a template with parameters GDP and population that produces the code for the desired table row. - Patrick (talk) 00:18, 28 December 2014 (UTC)
I have the same need for this article Battery case sizes where it'd be great to have the volume computed from the length, width, and height --Hooperbloob (talk) 00:29, 30 August 2018 (UTC)
An example of such a kind of template is w:User:Patrick/volume (see also w:User talk:Patrick/volume). However, this is for use with direct table syntax, not with w:Template:Aligned table. - Patrick (talk) 13:01, 30 August 2018 (UTC)

Number of weekdays from a given date?[edit]

Is there any way I can have Wikipedia compute the number of weekdays from a given date, and display that number? Or even better, compute the number of weekdays from a given date, add that to an integer, and output that number? Robert K S (talk) 22:55, 18 February 2015 (UTC)

Unformatting a number so that calculations can be performed[edit]

In my wiki I have the ContributionScores extension, and I'm trying to use the code (with italian locale):

{{ #ifexpr: {{#cscore:SomeUserName|changes}} > 100|green|red}}

but I get (in italian): Errore nell'espressione: carattere di punteggiatura "?" non riconosciuto. (in english it's: Expression error: Unrecognized punctuation character "?".), where the unrecognized character is shown as a double dot, not as a question mark: I think the double dot comes from the thousand separator, because the extension ContributionScores inserts it with the php function $wgLang->formatNum(), which formats the argument number according to the used locale, and italian formatted numbers use a space as thousand separator.

How can I convert the formatted number to a plain one so that ifexpr receives a plain number? I tried:

{{ #ifexpr: {{formatnum:{{#cscore:SomeUserName|changes}}|R}} > 100|green|red}}

but it doesn't work. 22:38, 23 March 2019 (UTC)