Skip to content

Built-in Math Functions

Trigonometric Functions

Default angle unit is degrees. Switch to radians with set_config.

FunctionDescriptionExample
sin(x)Sinesin(90)1
cos(x)Cosinecos(0)1
tan(x)Tangenttan(45)1
asin(x)Arcsineasin(0.5)30
acos(x)Arccosineacos(0.5)60
atan(x)Arctangentatan(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

FunctionDescriptionExample
exp(x)e^xexp(1)2.718...
log(x)Natural log (ln)log(e())1
ln(x)Natural log (alias)ln(e())1
log10(x)Base-10 loglog10(100)2
log2(x)Base-2 loglog2(8)3
javascript
calc("exp(1)"); // '2.7182818284590452...'
calc("log10(1000)"); // '3'
calc("log2(256)"); // '8'

Powers & Roots

FunctionDescriptionExample
pow(x, y)x to the power ypow(2, 10)1024
sqrt(x)Square rootsqrt(16)4
cbrt(x)Cube rootcbrt(27)3
javascript
calc("pow(2, 8)"); // '256'
calc("sqrt(144)"); // '12'
calc("cbrt(-8)"); // '-2'

Rounding

FunctionDescriptionExample
floor(x)Round downfloor(3.7)3
ceil(x)Round upceil(3.2)4
round(x)Round half upround(3.5)4
trunc(x)Truncate toward zerotrunc(-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

FunctionDescriptionExample
max(a, b, ...)Maximummax(1, 5, 3)5
min(a, b, ...)Minimummin(1, 5, 3)1
clamp(x, min, max)Clamp to rangeclamp(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 }); // area

Conditional 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,
});

Released under the MIT License