Lightweight, zero dependency calculator engine.
Only ≈2kB min + gzipped! ✨
npm install calculy
import { calculate } from "calculy";
calculate("1+2(3!)^2"); // 73
calculate("ans*2", { constants: { ans: 3 } }); // 6
calculate("ans^x", { constants: { ans: 3, x: 2 } }); // 9
calculate("cos(90)"); // -0.44807361612
calculate("cos(90)", { deg: true }); // 0
You can also directly include ./dist/calculy.min.js
from the npm package. This will make all calculy functions available under the global variable Calculy
.
Calculy supports most basic algebra that you would need for regular calculations.
2+2
= 4
2-2
= 0
2*2
= 4
1/2
= 0.5
2^2
= 4
2^(2+1)
= 8
4*5-2^2-3
= 1
e
≈ 2.718281828459045
π
or pi
≈ 3.141592653589793
φ
or phi
≈ 1.618033988749895
τ
or tau
≈ 6.283185307179586
ans*2
= 3 (if the provided constant ans
is 3)
x^2
= 16 (if the provided constant x
is 4)
2pi
≈ 6.283185307179586
(4-2)(3-4)
= -2
3(4-2)^2
= 12
50%
= 0.5
-200%
= -2
2.5e+6
= 2500000
2.5e-3
= 0.0025
|-3|
= 3
|(2*2-6)|
= 2
5!
= 5
0.5!
≈ 0.8862269254527586
√(9)
or √(9, 2)
or sqrt(9)
= 3
√(27, 3)
or cbrt(27)
= 3
√(81, 4)
= 3
log(100)
or log(100, 10)
= 2
log(8, 2)
= 3
ln(e)
or log(e, e)
= 3
sin(pi/2)
= 1
cos(2pi)
= 1
tan(pi/4)
≈ 1
asin(0.5)
≈ 0.5235987755982989
acos(0.5)
≈ 1.0471975511965979
atan(0.5)
≈ 0.4636476090008061
rand()
= random number between 0 and 1
rand(2)
= random number between 0 and 2
rand(2, 5)
= random number between 2 and 5
round(2.6)
= 3
floor(2.6)
= 2
ceil(2.2)
= 3
ceil(2.2)
= 3
Will execute the math and give the answer. Can throw a SyntaxError
.
constants
: Custom constants (Object.<number>
), will override default constants with same name. (default:{}
)deg
: Whether to use degrees instead instead of radians for trigonometry functions. (default:false
)
Will return a list of the tokens that calculy parses. Can come in handy for input validation/formatting.
Will return the raw AST, useful for custom evaluation. Can throw a SyntaxError
.
Will evaluate a calculation expression;