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 on JVM; 64-bit on JS)
- BD: BigDec (arbitrary precision on JVM; Double-backed on JS)
Explicit precision with DEC(...)¶
DEC(...) is the primary explicit 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.
Notes:
- Large integer literals may parse as BI.
- INT("...") can return BI for large values.
- NUMBER("...") does not produce BI or BD.
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(...)).
This same BD/F mixing rule applies to comparisons and equality.
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 → BIBDorFwith//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. - On JS,
BIis 64-bit (Long-backed), not arbitrary precision. 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.
JSON input/output rules¶
Branchline treats JSON numeric interoperability separately from in-language arithmetic:
- Safe mode (default) preserves precision by parsing large integers as
BigIntand high-precision decimals asBigDec. When emitting JSON,BigIntandBigDecare encoded as JSON strings to avoid silent precision loss. - Strict mode rejects numbers outside the safe JSON numeric range.
- Extended mode emits large numbers as numeric literals for systems that can handle them.
Control the behavior with --json-numbers strict|safe|extended in the CLI (safe is the default).