Skip to content

Basic Methods

Low-level arithmetic functions for maximum performance.

Binary Operations (Multi-argument)

MethodDescriptionExample
addAdditionadd(0.1, 0.2)0.3
subSubtractionsub(0.3, 0.1)0.2
mulMultiplicationmul(0.1, 0.2)0.02
divDivisiondiv(0.3, 0.1)3
modModulomod(10, 3)1
powPowerpow(2, 10)1024
idivInteger divisionidiv(10, 3)3

Unary Operations

MethodDescriptionExample
absAbsolute valueabs(-5)5
negNegateneg(5)-5
sqrtSquare rootsqrt(16)4
lnNatural logln(2.718)0.999...
expe^xexp(1)2.718...

Return Type

Default return type is number. Pass 'string' as the last argument to get a string:

javascript
import { add, mul, div } from "a-calc";

add(1, 2); // 3 (number)
add(1, 2, "string"); // '3' (string)
mul(2, 3, "string"); // '6' (string)

Multi-argument Support

javascript
add(1, 2, 3, 4, 5); // 15
sub(100, 20, 30); // 50
mul(2, 3, 4); // 24
div(1000, 10, 5); // 20

r-prefix Variants (String I/O)

The r* prefix functions always take and return strings, and follow _compute_mode:

javascript
import { radd, rmul, rdiv } from "a-calc";

radd("0.1", "0.2"); // "0.3"
rmul("0.1", "0.2"); // "0.02"
rdiv("1", "3"); // "0.33333333333333333333"

vs calc

FeatureBasic methodscalc
Performancefasterbaseline
Formatting
Variables
Complex expressions
Multi-argument

Real-World Usage

javascript
import { mul, add } from "a-calc";

const price = 99.9;
const quantity = 3;
const shipping = 10;

const subtotal = mul(price, quantity); // 299.7
const total = add(subtotal, shipping); // 309.7

Released under the MIT License