shunting-yard-arbitrary-precision

node v4.9.1
version: 2.0.1
endpointsharetweet
Mathematical expression parser with arbitrary precision support
const calc = require("shunting-yard-arbitrary-precision"); +calc('3+2');
A few more simple examples:
+calc('3 +2 ');
+calc('(10+5)/5');
+calc('2 * (2 * (5 - 3))');
+calc('4(6)');
+calc('3+(5*2)*(-3+2)');
+calc('2^3');
Like arbitrary precision?
var decimalFactory = require('arbitrary-precision'); var Decimal = decimalFactory(require('bigjs-adapter')); +calc('0.1+0.2', {Decimal: Decimal});
as opposed to:
+calc('0.1+0.2');
Default precision
calc('1/3').toString();
Arbitrary precision! (depends on adapter/implementation used)
Decimal.setPrecision(50); calc('1/3', {Decimal: Decimal}).toString();
It also supports adding math functions to the context...
var context = new calc.Context(); context.def('abs'); +calc('2*abs(-3)', {context: context});
... as well as constants
context.def('tau', 2 * Math.PI); +calc('3*tau', {context: context});
and arbitrary functions
context.def('javier', a => a * 1.618); +calc('javier(21)', {context: context});
Ramda!!!
const curryN = require('ramda/src/curryN'); const __ = require('ramda/src/__.js'); const curriedCalc = curryN(2, calc); const myCalc = curriedCalc(__, {Decimal: Decimal, context: context}); +myCalc('javier(abs(-3))*.2');
+calc('(1.618*3)*.2');
Loading…

no comments

    sign in to comment