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.php?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)

function rechnen{ var m1eingabe_w=window.document.felder.formel.value; var m1genau_w=100000000000000; var m1formel_a = new Array; var m1ergebnis_w=new Array; var m1ergebnis_text; var m1anzahl; var _min=60; var _h=3600; var _sec=1; var _d=24*3600; var _m=1; var _l=0.001; var _j=1; var _w=1; var _pa=1; var _mm=0.001; var _km=1000; var _cbm=1; var _kj=1000; var _kw=1000; var _bar=100000; var pi=Math.PI; var _ft=0.3048; var _in=0.00254; var _ps=745.7; var _btu=1055; var _mmws=9.813752; var _psi=6895; var _gal=0.003785; var _bar=100000; var _mbar=100; var _cal=4.1855; var _kcal=4185.5; var ln10=Math.LN10; var lge=Math.LOG10E; m1ergebnis_text=""; var j; m1formel_a=m1eingabe_w.split(";"); m1anzahl=m1formel_a.length; m1eingabe_w=""; for (j=0;j<m1anzahl;j++) { m1ergebnis_w[j]=Math.round(m1genau_w*eval(m1formel_a[j]))/m1genau_w; m1ergebnis_text=m1ergebnis_text+m1ergebnis_w[j]+"\n"; formel_temp=m1formel_a[j]; formel_temp=formel_temp.replace(/\n/,""); if (j<m1anzahl-1) { m1eingabe_w=m1eingabe_w+formel_temp+";"+"\n"; } else { m1eingabe_w=m1eingabe_w+formel_temp; } } window.document.felder.ergebnis.value=m1ergebnis_text; window.document.felder.formel.value=m1eingabe_w; } function delete_all { var m1ergebnis_text=""; var m1eingabe_w=""; window.document.felder.ergebnis.value=m1ergebnis_text; window.document.felder.formel.value=m1eingabe_w; } // Funktion zur Interpolation function IPOL(t,x_1,x_2,x_3,x_4,y_1,y_2,y_3,y_4) { var x_1; var x_2; var x_3; var x_4; var y_1; var y_2; var y_3; var y_4; var t; var y_cal; t0=new Array (x_1,x_2,x_3,x_4); dvw=new Array (y_1,y_2,y_3,y_4); y_cal=   dvw[0]*((t-t0[1])*(t-t0[2])*(t-t0[3]))/((t0[0]-t0[1])*(t0[0]-t0[2])*(t0[0]-t0[3]))+ dvw[1]*((t-t0[0])*(t-t0[2])*(t-t0[3]))/((t0[1]-t0[0])*(t0[1]-t0[2])*(t0[1]-t0[3]))+ dvw[2]*((t-t0[0])*(t-t0[1])*(t-t0[3]))/((t0[2]-t0[0])*(t0[2]-t0[1])*(t0[2]-t0[3]))+ dvw[3]*((t-t0[0])*(t-t0[1])*(t-t0[2]))/((t0[3]-t0[0])*(t0[3]-t0[1])*(t0[3]-t0[2])); return y_cal } function ABS(x) { var x; return Math.abs(x); } function CONV(x,a,b) { var x; var a; var b; return x*a/b; } function ACOS(x) { var x; return Math.acos(x); } function COS(x) { var x; return Math.cos(x); } function SIN(x) { var x; return Math.sin(x); } function ASIN(x) { var x; return Math.asin(x); }function TAN(x) { var x; return Math.tan(x); } function ATAN(x) { var x; return Math.atan(x); } function EXP(x) { var x; return Math.exp(x); } function POW(x,y) { var x; return Math.pow(x,y); } function SQRT(x) { var x; var y; return Math.sqrt(x); } function RAD(x) { var x; return Math.PI/180*x; } function DEG(x) { var x; return 180/Math.PI*x; }

function LN(x) { var x; if (x<=0) { alert("Calculation LN(x) with x<=0 not allowed!"); } else { return Math.log(x); } }

function LG(x) { var x; if (x<=0) { alert("Calculation LG(x) with x<=0 not allowed!"); } else { return Math.LOG10E*Math.log(x); } }

function FAC(x) { var x; var y=1; var i; if (Math.ceil(x)!=x) { alert("Calculation FAK(x) only with integer!"); } else{ for (i=1;i<=x;i++){ y=y*i; } return y; } }

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

include ("extensions/calcII.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.