The calc
function takes a string argument and returns the value of the resulting calculation.
npm install @motardo/calc
var calc = require("@motardo/calc");
calc("1+2") // 3
calc("-1 / 2") // -0.5
calc("2 + 3 * 4") // 14
calc("(2+3)*4") // 20
calc("5^2") // 25
Try it out at https://motardo.github.io/calc/web/index.html
Run the tests with:
npm test
calc uses the shunting yard algorithm and two separate stacks, one for numerical values, and one for operators. The operators currently supported are + - * / and ^. Parenthesis can be used to override operator precedence.
- Add support for shorthand scientific notation as in
1e9
for 1 billion - Add support for trigonometric functions
- Add support for
log
andln
notation
Bug reports and contributions are welcome. Open an issue on github or email the author at nswilson@gmail.com