Numeric Semantics¶
Branchline exposes a single user-facing type, Numeric. Internally, values are tracked as one of four kinds:
- I: fast integer (I64 on JVM; JS safe-integer range)
- F: float (F64 / double)
- BI: BigInt (arbitrary precision integer)
- BD: BigDec (arbitrary precision decimal)
Explicit precision with DEC(...)¶
DEC(...) is the only entry point into precise arithmetic. No operator will implicitly convert
float values (F) into BigDec (BD) or BigInt (BI).
Rules:
DEC("...")- Parses as BD if the string contains
.or an exponent (e.g.,"1.25","1e3"). - Parses as BI otherwise.
DEC(number)I→ BIBI→ BIBD→ BDF→ BD using the platform’s deterministic double→decimal conversion.
Tip: prefer
DEC("0.1")for human-authored precise values.
Arithmetic promotion¶
For +, -, * (same pattern):
I + I → I(overflow promotes toBI)I + F → FF + F → FBI + I → BIBI + F → F- If either operand is
BD, result isBDunless the other operand isF(mixingBDandFis rejected; wrap floats inDEC(...)).
Division¶
/always returns F (double) unless aBDis involved:I / I → FBI / BI → FBD / BD → BDBD / I|BI → BDI|BI / BD → BD//is integer division:I // I → I(truncates toward zero; overflow promotes toBI)BI // BI → BIBD // ...is rejected
Rounding (ROUND)¶
ROUND(x, n) uses round half to even.
ROUND(x, 0)returns an integer kind:I/BI→ unchangedF→Iif within range, otherwiseBIBD→BI(orIif it fits)ROUND(x, n > 0)returns:BDifxisBDFifxisI,BI, orF
JavaScript notes¶
- The
Ikind is restricted to the JS safe-integer range (±(2^53 − 1)). - When integer operations exceed the safe range, results promote to
BI. BDuses the platform BigDec implementation (Double-backed in JS); useDEC("...")for explicit precision and to avoid float rounding surprises.
Try it in the playground: Numeric precision example.