Help talk:Calculation

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

Contents

[edit] mediazilla:6068

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)

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

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)

[edit] European numbers

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? 141.156.51.154 20:36, 28 January 2008 (UTC)

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

[edit] Square root?

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)

[edit] ParserFunctions Version?

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. --84.128.251.118 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. --84.128.251.203 15:35, 10 September 2008 (UTC)

[edit] Question on calculating numbers from different areas

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 p^2=144 .--Patrick (talk) 09:34, 23 June 2010 (UTC)

[edit] round endig in Zeros

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 ? --24.95.237.237 04:05, 17 April 2009 (UTC)

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

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 173.116.117.15 (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)

[edit] Substituted?

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? 60.240.101.246 01:18, 11 February 2011 (UTC)

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

[edit] Return length of string

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)

[edit] Dependencies? Mediawiki version?

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)
Personal tools
Namespaces

Variants
Actions
Navigation
Community
Beyond the Web
Print/export
Toolbox