A decimal.js extension to
evaluate arithmetic expressions
This is a lightweight expression parser. See math.js for a much more powerful and full-featured expression parser and math library.
Usage example
;; evaluate; let result = Decimal; console; // '0.3'console; // true Decimalprecision = 30; result = Decimal; console; // '0.282240016119734444201489605616'console; // '2*sin(x)'
Test
$ npm test $ node test/eval "cos(1 + sin(0.5))"
Licence
Credits
The implementation is derived from the article Top Down Operator Precedence by Douglas Crockford, available at
http://crockford.com/javascript/tdop/tdop.html.
Also see:
https://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing
http://effbot.org/zone/simple-top-down-parsing.htm
Scope example
const scope = NaN Infinity // Constants to 40 d.p. E: '2.7182818284590452353602874713526624977572' LN2: '0.6931471805599453094172321214581765680755' LN10: '2.3025850929940456840179914546843642076011' LOG2E: '1.4426950408889634073599246810018921374266' LOG10E: '0.4342944819032518276511289189166050822944' PI: '3.1415926535897932384626433832795028841972' SQRT1_2: '0.7071067811865475244008443621048490392848' SQRT2: '1.4142135623730950488016887242096980785697' PHI: '1.6180339887498948482045868343656381177203' x x x x x x x Decimal x x x x x x x x Decimal Decimal Decimal Decimal Decimal x Decimal x x x x x x; result = Decimal; console; // '9.39562279835805409202716773322'console; // '3.4564564645*E+4*(PHI-cos(LN2)+exp(PI))/2.003e+12'
Notes
The results of operations are calculated to the number of significant digits specified by the value of Decimal.precision
and rounded using the rounding mode of Decimal.rounding
.
Decimalprecision = 5;Decimal; // '0.33333'
The comparison operators return 1
for true
and 0
for false
.
Decimal; // '0'
As with JavaScript, the logical operators &&
and ||
return one of their operands.
Decimal; // '3'
Exponential notation is supported.
Decimal; // '1'
As shown above, user-defined variables and functions are supported through a scope object passed as the second argument.
Decimal; // '0.4233600241796016663'
Once a scope is defined, it exists until it is replaced by a new scope.
Decimal; // '0.3'Decimal; // '1'Decimal; // '0.04'Decimal; // 'Decimal.eval: unknown symbol: x'
Multiplication is implicit for immediately adjacent values, but note that multiplication and division are left-associative:
Decimal; // '2' (1/2)*xDecimal; // '0.125' 1/(2*x)
The evaluated expression is available at Decimal.eval.expression
.
Decimal;Decimalevalexpression; // '2*x*(3*x + 4*y)'
Each variable's value and each function's return value is passed to the Decimal
constructor, so a number, string or Decimal can be used in the definition in the scope object.
Decimal; // '3'
The values that are passed to functions are Decimal
values, so their prototype methods are available.
Decimal; // '0.3'
The definition of a valid identifier for a variable or function is the same as for JavaScript except that the only non-ASCII characters allowed are the letters of the Unicode Greek and Coptic range: \u0370-\u03FF
.
I.e. an identifier must start with a letter, _
or $
, and any further characters must be letters, numbers, _
or $
.
Decimal; // '6.283185307179586'
A variable or function's value can be changed without having to redefine the scope, by passing an object containing the updated definitions as the first and only argument.
Below, the value of π
is updated and the last expression '2πr'
is re-evaluated each time.
Decimal; // '6.28318530717958647692'Decimal; // '6'
A new scope has not been created so r
is still available.
Decimal; // 1Decimal; // 'Decimal.eval: unknown symbol: r'
When a new expression is passed to Decimal.eval
it is tokenized and those tokens are re-evaluated on each subsequent eval
call until a new expression is passed and a new set of tokens is created.
Decimal; // '8' 2^3Decimal; // '0.125' 2^-3Decimal; // '0.015625' 4^-3 Decimal; // '1'Decimal; // '1'Decimal; // '256' 4^4
Note, though, that new variables or functions cannot be added via the updating object.
Decimal; // 'Decimal.eval: identifier not in scope: z'