Built-in Math Functions
Trigonometric Functions
Default angle unit is degrees. Switch to radians with set_config.
| Function | Description | Example |
|---|---|---|
sin(x) | Sine | sin(90) → 1 |
cos(x) | Cosine | cos(0) → 1 |
tan(x) | Tangent | tan(45) → 1 |
asin(x) | Arcsine | asin(0.5) → 30 |
acos(x) | Arccosine | acos(0.5) → 60 |
atan(x) | Arctangent | atan(1) → 45 |
javascript
import { calc } from "a-calc";
calc("sin(0)"); // '0'
calc("sin(30)"); // '0.5'
calc("sin(90)"); // '1'
calc("cos(0)"); // '1'
calc("cos(60)"); // '0.5'
calc("tan(45)"); // '1'Switching Angle Units
javascript
import { calc, set_config } from "a-calc";
// Default: degrees
calc("sin(90)"); // '1'
// Switch to radians
set_config({ _angle_unit: "rad" });
calc("sin(3.14159265358979 / 2)"); // ≈ '1'
// Switch back
set_config({ _angle_unit: "deg" });Hyperbolic Functions
sinh, cosh, tanh, asinh, acosh, atanh
Exponential & Logarithm
| Function | Description | Example |
|---|---|---|
exp(x) | e^x | exp(1) → 2.718... |
log(x) | Natural log (ln) | log(e()) → 1 |
ln(x) | Natural log (alias) | ln(e()) → 1 |
log10(x) | Base-10 log | log10(100) → 2 |
log2(x) | Base-2 log | log2(8) → 3 |
javascript
calc("exp(1)"); // '2.7182818284590452...'
calc("log10(1000)"); // '3'
calc("log2(256)"); // '8'Powers & Roots
| Function | Description | Example |
|---|---|---|
pow(x, y) | x to the power y | pow(2, 10) → 1024 |
sqrt(x) | Square root | sqrt(16) → 4 |
cbrt(x) | Cube root | cbrt(27) → 3 |
javascript
calc("pow(2, 8)"); // '256'
calc("sqrt(144)"); // '12'
calc("cbrt(-8)"); // '-2'Rounding
| Function | Description | Example |
|---|---|---|
floor(x) | Round down | floor(3.7) → 3 |
ceil(x) | Round up | ceil(3.2) → 4 |
round(x) | Round half up | round(3.5) → 4 |
trunc(x) | Truncate toward zero | trunc(-3.7) → -3 |
javascript
calc("floor(-3.2)"); // '-4'
calc("ceil(-3.7)"); // '-3'
calc("trunc(-3.9)"); // '-3'Absolute Value & Sign
javascript
calc("abs(-123)"); // '123'
calc("sign(-100)"); // '-1'
calc("sign(0)"); // '0'
calc("sign(100)"); // '1'Statistics
| Function | Description | Example |
|---|---|---|
max(a, b, ...) | Maximum | max(1, 5, 3) → 5 |
min(a, b, ...) | Minimum | min(1, 5, 3) → 1 |
clamp(x, min, max) | Clamp to range | clamp(150, 0, 100) → 100 |
javascript
calc("max(1, 2, 3, 4, 5)"); // '5'
calc("clamp(150, 0, 100)"); // '100'
calc("clamp(-50, 0, 100)"); // '0'Constants
javascript
calc("pi()"); // '3.1415926535897932...'
calc("e()"); // '2.7182818284590452...'
// Usage
calc("2 * pi() * r", { r: 5 }); // circumference
calc("pi() * r ** 2", { r: 3 }); // areaConditional Function
javascript
// if(condition, trueValue, falseValue)
calc("if(x > 0, x, 0)", { x: 5 }); // '5'
calc("if(x > 0, x, 0)", { x: -3 }); // '0'Combination Examples
javascript
// Pythagorean theorem
calc("sqrt(pow(a, 2) + pow(b, 2))", { a: 3, b: 4 }); // '5'
// Compound interest
calc("principal * pow(1 + rate, years)", {
principal: 10000,
rate: 0.05,
years: 10,
});