Welcome to the MathExt JavaScript library.
This library was made to parse and calculate mathematical formulas of any kind and any complexity. It can easily be integrated into websites and web applications. As developer you can set all kind of settings. You can even specify what commands (» Actions) should be available to the user and in what priority they should be proceeded. The client's web browser just needs to be ECMA-262 3rd edition (= JavaScript 1.5) compliant.
The MathExt project has been developed by Lars Knickrehm.
If you like MathExt, please donate to help keeping its development active. Thanks!
MathExt is offered as free open source library and can be used, modified and redistributed under the terms of the GNU Affero General Public License Version 3 (AGPL3). If you want to use MathExt in a closed source project, please do not hesitate to contact me via mail.
You can download the latest version of MathExt, report bugs or ask for help at the project's page, which is hosted for free by SourceForge.net. This documentation uses the open source projects SyntaxHighlighter and ExplorerCanvas.
Setting up MathExt is quite simple. Just follow these steps and you can already use MathExt including all default commands.
First you need to load the MathExt class. It is somewhat the kernel of MathExt. So just put the following piece of code into your html source:
<script src="MathExt.Class.min.js" type="text/javascript"></script>
Now your page is already loading the MathExt class code.
Next you need to load the actions. To do so do the same as before, but select the actions class and load function:
<script src="MathExt.Actions.min.js" type="text/javascript"></script> <script src="MathExt.Load.min.js" type="text/javascript"></script>
The order of these JavaScript files can be what ever you want. I like to sort them alphabetically...
The function MathExt.Load initializes a new MathExt.Class object for you and it loads all defined functions from the MathExt.Actions class. So let us run that command:
<script type="text/javascript"> var oMathExt = MathExt.Load(); </script>
This piece of code needs to be used after loading the JavaScript files.
I recommend you to define MathExt.Class objects globally to keep user created variables available for further calculations.
To execute a calculation you just need to call oMathExt.Calculate now. After this paragraph you can find a really simple example, which calculates the value of a textbox on submitting.
<html> <head> <script src="MathExt.Actions.min.js" type="text/javascript"></script> <script src="MathExt.Class.min.js" type="text/javascript"></script> <script src="MathExt.Load.min.js" type="text/javascript"></script> <script type="text/javascript"> var oMathExt = MathExt.Load(); function fOnSubmit() { var sInput = document.getElementById('input').value; document.getElementById('output').innerHTML = oMathExt.Calculate(sInput); } </script> </head> <body> <form onsubmit="fOnSubmit(); return false;"> <input id="input" value="" /> <input type="submit" value=" = " /> <div id="output">0</div> </form> </body> </html>
In the MathExt project some words are used, which can be ambiguous or even unknown to some developers. They are explained in the paragraphs below. But it is recommended for all developers to read these explanations!
Mathematical information can be found at Wikipedia in Arithmetic precision.
To simplify the MathExt output it can cut off decimals until a given number. You can enable its arithmetic precision using MathExt.Class.Settings.Output.ArithmeticPrecision [function].
Note: MathExt saves the exact number instead of its output in the answer memory.
MathExt handles error messages in an own object, which can be handled by using the functions in MathExt.Class.Errors [namespace]. Optionally you can enable logging to user defined functions (» Functions), the browser's console (» Console) or an alert message box (» Alert).
Mathematical information can be found at Wikipedia in Fraction (mathematics).
MathExt allows you to choose between three different states of the Fraction setting. You can change them using the Fraction function.
Mathematical information can be found at Wikipedia in Powers of 10.
Most calculators use EXP or E for this feature. The "Power of Ten" is just a short form of 10 ^ and quite often used in physics while processing big and small numbers.
If you use MathExt.Load [function] to create a new MathExt class, E is loaded as function for "Power of Ten".
Note: e is not "Power of Ten", but Euler's number, a constant!
The Power of Ten feature is just used, if the exponent is smaller or higher than a specified value. MathExt allows you to change these values just as you like it.
PowerOfTenLimits requires two arguments. The first one specifies until what value negative exponents are created. The second one affects positive exponents.
For default MathExt uses 7 and 14. Here are some examples:
To calculate things in the right order, MathExt has integrated support for priorities of actions, which require at least one argument before and one argument after their expression.
The following list shows you in what order input types and actions are handled.
MathExt.Class.Settings.Actions.Add [function] allows you to define what priority your actions with arguments before and after the expression should be in.
Mathematical information can be found at Wikipedia in Radian and Degree (angle).
The default angle unit is radian. It can be changed to degree if needed. Though you can pass degree values followed by °. Then the degree value gets converted into radian.
You can specify the default angle unit by using MathExt.Class.Settings.Input.Radian [function].
MathExt allows you to change the used input and output radix char. The radix char can change in different localisations.
Separators can be used in different situations:
Note: You can set multiple input separators, but just one output separator.
To simplify reading big numbers, MathExt allows you to change the input and output thousands separators. The thousands separators can change in different localisations.
Note: It is possible to specify more than one input thousands separator. By default the output thousands separator is empty.
In the MathExt documentation and code the following phrases are used to specify the different input types.
Note: Some input types combine different functionalities. Others are handled as subtypes of an upper rated input type.
Mathematical information can be found at Wikipedia in Absolute value.
Absolute values can be inserted using its specified characters. They get calculated and then handled just like brackets. Example: |−8| returns 8.
In MathExt user defined constants and functions are called action. Some actions require arguments before and / or after its expression in the formula. There are four different action types:
Before | After | Description |
---|---|---|
false | false | Constants such as e and pi do not require any arguments. |
false | true | The default way to define functions is this one. Most functions require arguments after the expression. Example: sin |
true | false | A small number of functions even require arguments in front of the expression. Example: ! |
true | true | Some functions require arguments before and after the expression. Example: mod |
Read about the priority order of actions in the part about Priorities.
Mathematical information can be found at Wikipedia in Bracket (mathematics).
In order to simplify the usage of brackets in mathematical formulas, MathExt supports different brackets and corrects missing brackets.
MathExt.Load registers three bracket character pairs: (), [] and {}. To change the registered bracket characters you can use the functions of MathExt.Class.Settings.Brackets.
MathExt supports numbers, which contain thousands separators and a radix char.
Note: Other calculators handle 1.2E-3 as number, but MathExt creates the number object 1.23 first and multiplies it with the result of E-3, which is 0.001 in this case. That just works if the action E is loaded. If you use MathExt.Load it is done automatically.
Note: The minimum and maximum values of a number and the closest number to zero should be the same in all environments, but they even could be different.
The five basic actions Plus and Minus, Multiply and Divide and Power can be called using single characters, which have a higher priority as normal text.
Example: 1 +sin 90° returns 2. It calls the function sin instead of +sin, which could be called if + is not a registered sign.
It is possible to pass strings to actions. Example: "text" returns text.
Note: If the ending character needs to be inside of the string itself, it needs to be typed two times. Example: "a""b" returns a"b.
MathExt allows you to localise your classes. A localisation translates the originally English error messages and changes some region specific settings for the in- and output of a MathExt class.
Variables can be specified while calculating. Afterwards they can be used to simplify a lot formulas. Variables are saved in the current MathExt class. New classes do not have any registered variables. To use variables MathExt.Load needs to be used.
To set variables you can use the action set or the equal sign: set("a"; 123) or "a" = 123
If you need to get variables back, just use get or ?: get("a") or "a"?
The following paragraphs show you, what expressions can be used after loading MathExt.Load. Please go to the API Reference to learn more about its technical usage.
This list represents all available actions, which can be used in mathemtical expressions.
Action | Symbols | Notes |
---|---|---|
Absolute value | abs | |
Answer | ans | |
Area hyperbolic cosecant | arcsch | |
Area hyperbolic cosine | arcosh | |
Area hyperbolic cotangent | arcoth | |
Area hyperbolic secant | arsech | |
Area hyperbolic sine | arsinh | |
Area hyperbolic tangent | artanh | |
Average | Ø, ø, ave | |
Binary logarithm | lb | Base: 2 |
Binomial coefficient | ncr | |
Centi | c | × 10 ^ (−2) |
Common logarithm | lg | Base: 10 |
Constant | const | |
Convert | conv | |
Cosecant | csc | |
Cosine | cos | |
Cotangent | cot | |
Count | count | |
Cube | ³ | |
Deca | da | × 10 ^ (+1) |
Deci | d | × 10 ^ (−1) |
Degree | ° | |
Euler's number | e | Constant |
Factorial | ! | |
Femto | f | × 10 ^ (−15) |
Five eight | ⅝ | Constant: 0.625 |
Gamma | Г, gamma | |
Get | ?, get | |
Giga | G | × 10 ^ (+9) |
Greatest common divisor | gcd | |
Half | ½ | Constant: 0.5 |
Hecto | h | × 10 ^ (+2) |
Hyperbolic cosecant | csch | |
Hyperbolic cosine | cosh | |
Hyperbolic cotangent | coth | |
Hyperbolic secant | sech | |
Hyperbolic sine | sinh | |
Hyperbolic tangent | tanh | |
Inverse cosecant | acsc, arccsc | |
Inverse cosine | acos, arccos | |
Inverse cotangent | acot, arccot | |
Inverse secant | asec, arcsec | |
Inverse sine | asin, arcsin | |
Inverse tangent | atan, arctan | |
Kilo | k | × 10 ^ (+3) |
Least common multiple | lcm | |
Logarithm | log | |
Mega | M | × 10 ^ (+6) |
Micro | µ, μ | × 10 ^ (−6) |
Milli | m | × 10 ^ (−3) |
Modulo | mod | |
Nano | n | × 10 ^ (−9) |
Natural logarithm | ln | Base: e |
One eight | ⅛ | Constant: 0.125 |
Per mil | ‰ | × 10 ^ (−3) |
Percentage | % | × 10 ^ (−2) |
Permutation | npr | |
Peta | P | × 10 ^ (+15) |
Pi | π, pi | Constant |
Pico | p | × 10 ^ (−12) |
Plus-minus | ± | |
Power of ten | E | Base: 10 |
Product | ∏, Π, product | |
Quarter | ¼ | Constant: 0.25 |
Radian | rad | |
Random | ran, random | |
Root | √, root | |
Round | rnd, round | |
Secant | sec | |
Square | ² | |
Set | =, set | |
Seven eight | ⅞ | Constant: 0.875 |
Sine | sin | |
Square root | sqrt | Degree: 2 |
Summation | Σ, ∑, sum | |
Tangent | tan | |
Terra | T | × 10 ^ (+12) |
Three eight | ⅜ | Constant: 0.375 |
Three fourth | ¾ | Constant: 0.75 |
Please continue reading here.
The following symbols can be used as arguments for the action const to easily get the value of a required pysical constant.
Example: const('c') returns 299792458.
Quantity | Symbol(s) | Value |
---|---|---|
Reduced Planck constant | ħ | 1.054571628 × 10 ^ -34 |
Magnetic flux quantum | Φ0 | 2.067833667 × 10 ^ -15 |
Fine-structure constant | α | 0.0072973525376 |
Gyromagnetic ratio | γp (yp) | 267522209.9 |
Stefan–Boltzmann constant | σ | 5.6704 × 10 ^ -8 |
Magnetic constant (vacuum permeability) | μ0 (µ0) | 1.2566370614 × 10 ^ -6 |
Bohr magneton | μB (µB) | 9.27400915 × 10 ^ -24 |
Magnetic moment (electron) | μe (µe) | -9.28476377 × 10 ^ -24 |
Nuclear magneton | μN (µN) | 5.05078324 × 10 ^ -27 |
Magnetic moment (proton) | μp (µp) | 1.410606662 × 10 ^ -26 |
Electric constant (vacuum permittivity) | ε0 | 8.85418781762 × 10 ^ -12 |
Bohr radius | a0 | 5.2917720859 × 10 ^ -11 |
Atomic mass unit | amu, mu, u | 1.660538782 × 10 ^ -27 |
Standard atmosphere | atm, n0, NL | 1.01325 |
Speed of light | c, c0 | 299792458 |
c1 | 3.74177118 × 10 ^ -16 | |
c2 | 0.014387752 | |
Elementary charge | e | 1.602176487 × 10 ^ -19 |
Faraday constant | F | 96485.3399 |
Gravitational constant | G | 6.67428 × 10 ^ -11 |
Planck constant | h | 6.62606896 × 10 ^ -34 |
Boltzmann constant | k, kB | 1.3806504 × 10 ^ -23 |
Avogadro constant | L, NA | 6.02214179 × 10 ^ 23 |
Rest mass (electron) | me | 9.10938215 × 10 ^ -31 |
Rest mass (neutron) | mn | 1.674927211 × 10 ^ -27 |
Rest mass (proton) | mp | 1.672621637 × 10 ^ -27 |
Gas constant | R, R0 | 8.314472 |
Rydberg constant | R∞ (R8) | 10973731.568527 |
Classical electron radius | re | 2.8179402894 × 10 ^ -15 |
Absolute zero (temperature) | t, T0 | 273.15 |
I guess most users use the default plus and minus, multiply and divide and power characters, but some calculators (such as the one at top) need to use other, more mathematical looking characters. This table lists all signs, which are registered by MathExt.Load. The first character is always the default one.
Plus | |
---|---|
+ | |
Minus | |
- | |
− | U+2212 |
Multiply | |
* | |
× | U+00D7 |
• | U+2715 |
∙ | U+2716 |
∙ | U+u22C5 |
∙ | U+2219 |
∙ | U+2217 |
∙ | U+2062 |
Divide | |
/ | |
: | U+003A |
÷ | U+00F7 |
÷ | U+2215 |
÷ | U+2044 |
Power | |
^ |
The following units can be used to convert values. The units can be combined in any way for each category. The first unit of each category represents the default SI unit. Internally values will be converted to that unit before converting to the required output value.
Example: conv("m/s"; "km/h"; 1) returns 3.6.
It is possible to convert more than one value at the same time. You just need to add more arguments to the action call.
Example: conv("m/s"; "km/h"; 1; 2; 3) returns 3.6; 7.2; 10.8.
Energy | |
---|---|
J | Joule |
cal | Calorie |
Length | |
m | Metre |
ft | Foot |
in | Inch |
mile | Mile |
sm | Nautical mile |
yd | Yard |
Mass | |
g | Gram |
lb | Pound |
Pressure | |
Pa | Pascal |
bar | Bar |
psi | Pounds per square inch |
Power | |
W | Watt |
PS | Pferdestärke |
Speed | |
m/s | Metres per second |
km/h | Kilometres per hour |
Temperature | |
K | Kelvin |
°C | Celsius |
°F | Fahrenheit |
Volume | |
m³ | Cubic metre |
gal | Imperial (UK) gallon |
l | Litre |
This part of the documentation is not finished, yet.
The API Reference is a full documentation for all public objects, which can be used to handle MathExt and MathExt classes.
This function registers a lot stuff, which is used in scientific calculators. A full list of registered functions can be found here.
Optionally you can pass two arguments to MathExt.Load. These arguments are just needed if you manually changed the object names of the MathExt.Class and MathExt.Actions object.
Mathematical information can be found at Wikipedia in Inverse trigonometric functions.
Mathematical information can be found at Wikipedia in Inverse trigonometric functions.
Mathematical information can be found at Wikipedia in Inverse trigonometric functions.
Mathematical information can be found at Wikipedia in Inverse trigonometric functions.
Mathematical information can be found at Wikipedia in Inverse trigonometric functions.
Mathematical information can be found at Wikipedia in Inverse trigonometric functions.
Mathematical information can be found at Wikipedia in Inverse hyperbolic function.
Mathematical information can be found at Wikipedia in Inverse hyperbolic function.
Mathematical information can be found at Wikipedia in Inverse hyperbolic function.
Mathematical information can be found at Wikipedia in Inverse hyperbolic function.
Mathematical information can be found at Wikipedia in Inverse hyperbolic function.
Mathematical information can be found at Wikipedia in Inverse hyperbolic function.
Mathematical information can be found at Wikipedia in Arithmetic mean.
Mathematical information can be found at Wikipedia in Binomial coefficient.
Mathematical information can be found at Wikipedia in Physical constant.
Mathematical information can be found at Wikipedia in Conversion of units.
Mathematical information can be found at Wikipedia in Trigonometric functions.
Mathematical information can be found at Wikipedia in Trigonometric functions.
Mathematical information can be found at Wikipedia in Trigonometric functions.
Mathematical information can be found at Wikipedia in Degree (angle).
Mathematical information can be found at Wikipedia in Factorial.
Mathematical information can be found at Wikipedia in Gamma function.
Mathematical information can be found at Wikipedia in Logarithm.
Mathematical information can be found at Wikipedia in Hyperbolic function.
Mathematical information can be found at Wikipedia in Hyperbolic function.
Mathematical information can be found at Wikipedia in Hyperbolic function.
Mathematical information can be found at Wikipedia in Hyperbolic function.
Mathematical information can be found at Wikipedia in Hyperbolic function.
Mathematical information can be found at Wikipedia in Hyperbolic function.
Mathematical information can be found at Wikipedia in Modulo operation.
Mathematical information can be found at Wikipedia in Permutation.
Mathematical information can be found at Wikipedia in Plus-minus sign.
Mathematical information can be found at Wikipedia in Product (mathematics).
Mathematical information can be found at Wikipedia in Radian.
Mathematical information can be found at Wikipedia in nth root.
Mathematical information can be found at Wikipedia in Rounding.
Mathematical information can be found at Wikipedia in Trigonometric functions.
Mathematical information can be found at Wikipedia in Trigonometric functions.
Mathematical information can be found at Wikipedia in Summation.
Mathematical information can be found at Wikipedia in Trigonometric functions.
Mathematical information can be found at Wikipedia in Radian.
Mathematical information can be found at Wikipedia in Decimal separator.
Mathematical information can be found at Wikipedia in Arithmetic precision.
Mathematical information can be found at Wikipedia in Fraction (mathematics).
Mathematical information can be found at Wikipedia in Powers of 10.
Mathematical information can be found at Wikipedia in Decimal separator.
MathExt.Class.Settings.Localisation [function] can be used to load a MathExt localisation object. To load a MathExt localisation you need to link to its JavaScript file:
<script src="MathExt.Localisations/de.min.js" type="text/javascript"></script>
Afterwards the localisation object can be passed as an argument to MathExt.Class.Settings.Localisation [function]:
<script type="text/javascript"> oMathExt.Settings.Localisation(MathExt.Localisations.de); </script>
العربية (Arabic)
Català (Catalan)
Deutsch (German)
English
Español (Spanish)
Français (French)
Italiano (Italian)
This function should only be used to create new localisations!
Português (Portuguese)
Română (Romanian)
Cрпски језик (Serbian)
Svensk (Swedish)
Tiếng Việt (Vietnamese)
This part of the documentation is not finished, yet.
The private API Reference can be used by action developers. The API is passed as this to all action functions automatically.
It is strongly recommended to take a look at the source code of some action functions of the MathExt.Actions class. They can be used as good and clean development examples for new action functions.
This mixed variable contains the previous calculation result.
Usage example: MathExt.Load registers the variable ans, which can be used in formulas to use the result of the previous calculation.
This array contains all thrown errors. Every single error array consists of:
Mathematical information can be found at Wikipedia in Decimal separator.
Mathematical information can be found at Wikipedia in Radian.
Mathematical information can be found at Wikipedia in Arithmetic precision.
Mathematical information can be found at Wikipedia in Fraction (mathematics).
Mathematical information can be found at Wikipedia in Powers of 10.
Mathematical information can be found at Wikipedia in Decimal separator.
Mathematical information can be found at Wikipedia in Absolute value.
Mathematical information can be found at Wikipedia in Division (mathematics).
Mathematical information can be found at Wikipedia in Greatest common divisor.
Mathematical information can be found at Wikipedia in Least common multiple.
Mathematical information can be found at Wikipedia in Subtraction.
Mathematical information can be found at Wikipedia in Multiplication.
Mathematical information can be found at Wikipedia in Addition.
Mathematical information can be found at Wikipedia in Exponentiation.
This private namespace owns classes, which are used to convert a formula string into an array. They should not be used by action developers and therefore they are not documentated in any way.