Install with NPM:
$ npm install evaluatex
Use a minified version in a webapp:
<script src="node_modules/evaluatex/dist/evaluatex.min.js"></script> <script type="text/javascript"> evaluatex(); <script>
Get zipped releases here.
Check it on Github.
Evaluatex is a parser that reads and evaluates LaTeX and ASCII math. It can safely resolve math
expressions, without relying on Javascript's eval
, which is evil. It's especially powerful when
combined with math markup tools such as Mathquill.
const fn = evaluatex(expression, constants = {}, options = {}); const result = fn(variables = {});
evaluatex()
compiles a math expression into a function fn
.expression
is an ASCII or LaTeX expression to be parsed and evaluated.constants
is a map of constant values - values that don't change if you invoke fn
more than
once.
options
is a map of options for the compiler.result
is the numerical result of the calculation.variables
is a map of variables that can change between invocations of fn
.Change the math below and see the result on the right. You may use any functions and objects in Javascript's
Math
.
Evaluatex takes a string containing a math expression and returns a compiled function. The compiled function evaluates the math and returns the result.
Throughout this manual, you may change the underlined math in any of the examples to explore how Evaluatex works.
A one-liner:
const result = evaluatex("1 + 2 * 3 / 4")()
Evaluatex returns a function because you can invoke it multiple times with different variables.
Evaluatex supports all of the features you should expect from a simple calculator, including order of operations. You can use parentheses, square brackets, or curly braces interchangeably.
Exponents use the ^
operator.
You can use named values in expressions that are visible to the expression only. This avoids the need for eval()
.
See the
Constants and variables section for more.
Implicit multiplication with brackets or variables is not a problem.
Be careful with associativity: 1 / 2a
is (1 / 2) * a
.
You have full access to all functions and constants in Javascript's Math
object.
You can omit brackets from simple functions, but be careful with implicit multiplication.
When in doubt, use parentheses.
You can define custom functions as constants.
Multi-argument functions work just as well, but they require parentheses.
There are several functions built in to Evaluatex to help with logs, roots, and trigonometry.
Absolute values work like a charm, as do factorials, which round to the nearest integer before performing the calculation.
You can refer to symbols, such as x
, in a math expression. These symbols can be constants or variables.
Constants are specified in the call to evaluatex()
as the second parameter. Their values are compiled by Evaluatex into the resultant equation. Use constants if you know that they won't change between invocations. Constants may be numeric values or functions, but not expressions like PI/2
.
Variables are specified when calling the compiled function. Their values can be changed between invocations. If you compile an expression with a variable, you must give a value for that variable, otherwise Evaluatex will complain. Variables may only be numeric values, and not functions.
You can combine constants and variables. Constants have priority over variables - if you define a constant, you can't change it without re-compiling the expression.
LaTeX parses math a little differently. Turn on LaTeX behaviour with a flag:
In the previous example, the power takes only the first symbol after it, so it reads like x^2 * 4
. Use curly braces to fix that.
LaTeX commands are also supported, like \frac
. The following example is interpreted as 1 / 20 * 3
.
Report issues on Github. Evaluatex does its best to be correct, but unanticipated bugs features might exist.
Evaluatex is licensed under the MIT license. This means that you can probably use it in your project, as long as you don't pass it off as your own or blame the author for the inevitable heat death of the universe.