Skip to content

Compute Modes

a-calc supports three compute engines to balance precision and performance.

Three Modes

ModeDescriptionBest For
decimalHigh-precision decimalDefault, general use
bigintLarge integer arithmeticFinance, big numbers
wasmWebAssemblyPerformance-critical

Quick Start

javascript
import { set_config, get_config, calc } from "a-calc";

get_config("_compute_mode"); // "decimal" (default)

set_config({ _compute_mode: "bigint" });
set_config({ _compute_mode: "wasm" });
set_config({ _compute_mode: "decimal" });

Decimal Mode (Default)

50-digit precision, full feature set.

javascript
calc("sqrt(2)"); // "1.4142135623730950488..."
calc("sin(90)"); // "1"
calc("0.1 + 0.2"); // "0.3"

BigInt Mode

Lossless integer arithmetic using fixed-point representation.

javascript
set_config({ _compute_mode: "bigint" });

// Large integers — perfectly accurate
calc("12345678901234567890 * 98765432109876543210");
// "1219326311370217952237463801111263526900"

// Division precision (configurable)
get_config("_div_precision"); // 20 (default)
calc("1 / 3"); // "0.33333333333333333333"

set_config({ _div_precision: 10 });
calc("1 / 3"); // "0.3333333333"

Unsupported operations (e.g. sqrt, sin) automatically fall back to Decimal.

WASM Mode

High-performance WebAssembly engine with 28-digit precision.

javascript
import { load_wasm, is_wasm_loaded, set_config, calc } from "a-calc";

await load_wasm();
is_wasm_loaded(); // true

set_config({ _compute_mode: "wasm" });
calc("0.1 + 0.2"); // "0.3"

WARNING

WASM loads asynchronously. Before loading completes, Decimal is used as fallback automatically.

wcalc — Direct WASM Expression

javascript
import { load_wasm, wcalc } from "a-calc";

await load_wasm();

wcalc("1 + 2 * 3"); // "7"
wcalc("sqrt(16) + pow(2, 3)"); // "12"

Fallback Mechanism

All modes have automatic fallbacks:

  • BigInt: unsupported ops → Decimal
  • WASM: before load → Decimal; unsupported ops → Decimal
  • All modes: division by zero → _error value

Mode Selection Guide

Use CaseRecommended Mode
Generaldecimal
Finance / large integersbigint
Scientific computingdecimal
Performance-criticalwasm

Operator Support

Operatordecimalbigintwasm
Basic arithmetic
sqrt, cbrt🔄✅/🔄
exp, ln, log10🔄
Trig functions🔄✅/🔄
Rounding
Comparison

🔄 = falls back to Decimal

Released under the MIT License