Extension:CalcII

Description
By putting the tag predefined expression somewhere in the wikitext of a page, this extension produces a calculator at that position of the page with the predefined expression. It has an input field and a results field, and a button to start the calculation.

Find a working example at the following website (german language): http://www.technikundkultur.de/wiki1/index.php5?title=Rechner

The input field allows any sequence of mathematical expressions and assignment statements (in JavaScript syntax), separated by semi-colons. If a predefined expression is inserted, it will be shown first but it can be changed or deleted. The interpretation of the Javascript expressions is done by a separate Javascript which has to be stored in the extension folder together with the PHP-script.

While the mathematical Javascript functions and constants can be used directly, the extension also allows the following additional functions, constants, and abbreviations:


 * Functions:
 * absolute value: ABS(x)
 * square root: SQRT(x)
 * xy: POW(x,y)
 * ex: EXP(x)
 * natural logarithm: LN(x)
 * logarithm base 10: LG(x)
 * change radian to degree: DEG(x)
 * change degree to radian: RAD(x)
 * x!: FAC(x)
 * trigonometric functions SIN(x) COS(x) TAN(x) ASIN(x) ACOS(x) ATAN(x)
 * unit conversion, where a and b are units (see below): CONV(x,a,b)


 * Constants: pi e lge ln10
 * Units: _ft (in m) _in (in m) _ps (in W) _btu (in J) _mmWS (in Pa) _psi (in Pa) _gal (US gallon in l) _bar (in Pa) _mbar (in Pa) _cal (in J) _km (in m) _m (in m) _mm (in m) _j (in J) _w (in W) _kw _kj _qbm (in l) _pa (in Pa) _sec (in Sec) _h (in Sec) _d (in Sec) _min (in Sec)

Example (the left and center column are the boxes on the wiki page):

Input: 12*13;a=CONV(25,_km/_h,_m/_sec);b=a*10*_h;Radius=5;Area=POW(Radius*2,2)*pi/4

(predefined expression must be inserted without any line break, the expression is optional)

Result:

Please note:


 * type the formula in the left field
 * separate formulas by ";"
 * define variables (i.e. a=SQRT(4))
 * use decimal point, no comma
 * accuracy: 8 decimal places
 * the functions in the extension-file are defined as JavaScript functions and may be changed for individual requests

Download and installation
Step 1: Copy-paste this code into a file calcII.php and place it in extensions directory.

';   $output .= ' '; $output .= ' '; $output .= ''; $output .= 'rechnen;'; $output .= ' '; $output .= ''; return $output; }

Step 2: Copy-paste this code into a file calcII.js and place it in extensions directory.

Attention: Extension folder must be set readable by everyone - adjust path to javascript file in php script (above)

Step 3: Add the following line to LocalSettings.php:

Step 4: To insert the calculator to a page, put </CalcII> or <CalcII>Expression 1;Expression 2;...;Expression n</CalcII> in the wikitext.

License
You are free to use this extension for any reason and mutilate it to your heart's liking. If you feel your work might benefit others, post your changes here.

Release 0.1.0: The script has been devided in a php part and a javascript part. It is now possible to define an expression directly in the edit mode of the wikipage. The expression can be deleted or changed in the input field at view mode but will always be in the input field when the page opens.

At the moment it is not possible to set line breaks between the predefined expressions. If you do, the calculation won't work. If there is any syntax mistake, the calculation doesn't work too. So check your input and test the calculation properly.