Evaluate math in strings safely
npm i math-exec
CDN Links:
- https://cdn.jsdelivr.net/npm/math-exec@1.0.0/math-exec.js
- https://www.unpkg.com/math-exec@1.0.0/math-exec.js
// ES6
import mathExec from "math-exec";
// commonjs
const mathExec = require("math-exec");
mathExec("1 + 2 * 3 / 1");
mathExec("sin(45) ^ 2");
mathExec("2 + 3 % 2");
mathExec("log(10, 100) * 2");
Make sure to console.log()
the values!
Operator names | Operator names | Operator names |
---|---|---|
** | + | - |
* | / | % |
^ | // | log |
log10 | min | max |
random | sqrt | abs |
round | ceil | floor |
sin | cos | tan |
asin | acos | atan |
exp | floorDiv | mod |
roundTo | cbrt | expm1 |
hypot | sign | trunc |