Arithmetic functions
CalcTree supports a full range of arithmetic functions using MathJS syntax. These functions form the foundation of most calculations, supporting scalars, vectors, units, and element-wise operations.
You can use these with both raw values (e.g. 3) and unit-aware values (e.g. 3 kN, 200 mm).
Core Arithmetic
add(a, b)
Addition
add(2, 3) → 5
subtract(a, b)
Subtraction
subtract(10, 4) → 6
multiply(a, b)
Multiplication
multiply(3, 4) → 12
divide(a, b)
Division
divide(10, 2) → 5
mod(a, b)
Modulus (remainder)
mod(7, 3) → 1
pow(a, b)
Power (a^b)
pow(2, 3) → 8
unaryMinus(x)
Negate a value
unaryMinus(5) → -5
unaryPlus(x)
Returns value unchanged
unaryPlus(5) → 5
Element-wise (Array) Arithmetic
dotAdd(a, b)
(Not explicit in MathJS, but implied)
add([1,2], [3,4]) → [4,6]
dotSubtract(a,b)
(Same idea as above)
subtract([5,6], [1,2]) → [4,4]
dotMultiply(a,b)
Element-wise multiplication
dotMultiply([2,3], [4,5]) → [8,15]
dotDivide(a,b)
Element-wise division
dotDivide([6,8], [2,4]) → [3,2]
dotPow(a, b)
Element-wise power
dotPow([2, 3], 2) → [4, 9]
Roots, Logs & Exponents
sqrt(x)
Square root
sqrt(25) → 5
cbrt(x)
Cube root
cbrt(27) → 3
nthRoot(x, n)
n-th root
nthRoot(16, 4) → 2
nthRoots(x)
All n-th roots (returns array)
nthRoots(4) → [2, -2]
exp(x)
Euler’s number e to the power of x
exp(1) → 2.718...
expm1(x)
e^x - 1
expm1(1) → 1.718...
log(x)
Natural logarithm (base e)
log(10) → 2.302...
log10(x)
Base-10 log
log10(1000) → 3
log2(x)
Base-2 log
log2(8) → 3
log1p(x)
log(x + 1)
log1p(1) → 0.693...
Rounding & Sign
round(x [, n])
Round to nearest integer or decimal
round(2.345, 2) → 2.35
ceil(x)
Round up
ceil(2.1) → 3
floor(x)
Round down
floor(2.9) → 2
fix(x)
Round toward zero
fix(-2.7) → -2
sign(x)
Returns 1 if positive, -1 if negative
sign(-8) → -1
Other Utilities
abs(x)
Absolute value
abs(-5) → 5
cube(x)
x * x * x
cube(3) → 27
square(x)
x * x
square(4) → 16
hypot(...)
Euclidean length (Pythagorean)
hypot(3, 4) → 5
norm(x [, p])
Vector or matrix norm
norm([3, 4]) → 5
Discrete Math Functions
gcd(a, b)
Greatest common divisor
gcd(18, 24) → 6
xgcd(a, b)
Extended GCD — returns coefficients
xgcd(8, 12) → {gcd: 4, x: -1, y: 1}
lcm(a, b)
Least common multiple
lcm(6, 8) → 24
invmod(a, b)
Modular inverse: x such that a·x ≡ 1 (mod b)
invmod(3, 11) → 4
Notes for CalcTree
All arithmetic functions support unit-aware values: e.g.,
sqrt(25 m²)→5 mdot*functions are useful when applying operations across arrays from tablesUse
round(..., n)when formatting outputs for display (e.g. 2 decimal places)Avoid
^for arrays — preferpow()ordotPow()to be explicit
Last updated