Arbitrary-precision binary, decimal, and interval arithmetic for MoonBit, with checked operations and explicit numeric semantics.
moon add Luna-Flow/floating@0.11.0import {
"Luna-Flow/floating/binary",
"Luna-Flow/floating/decimal",
"Luna-Flow/floating/interval",
}///|
test "floating quick start" {
// One third, correctly rounded to binary64, with its IEEE status.
let ctx = @binary.BinaryContext::binary64()
let (third, flags) = @binary.Binary::from_int(1).div_ctx(
@binary.Binary::from_int(3),
ctx,
)
inspect(third.to_shortest_string(), content="0.3333333333333333")
inspect(flags.inexact(), content="true")
// A decimal keeps the exponent of its literal.
let (price, _) = @decimal.Decimal::from_string_ctx(
"12.3400",
@decimal.DecimalContext::decimal64(),
)
inspect(price.exponent10(), content="-4")
// An interval contains every exact result.
let one = @interval.Interval::from_int(1, precision=53)
let enclosure = one.div(@interval.Interval::from_int(3, precision=53))
inspect(enclosure.contains(third), content="true")
}| Package | Purpose | Result model |
|---|---|---|
| binary | arbitrary-precision binary floating point, IEEE 754 binary operations, binary16/32/64/128 interchange | value, or (value, BinaryFlags) under a BinaryContext |
| decimal | IEEE 754 decimal arithmetic, decimal32/64/128 DPD and BID interchange | value, or (value, DecimalFlags) under a DecimalContext |
| decimal_gda | General Decimal Arithmetic with sticky status and traps | GdaOutcome with the defined result and the next context |
| interval | outward-rounded bare and decorated intervals (IEEE 1788) | interval, or (interval, IntervalFlags) under an IntervalContext |
| binary_checked | binary pipeline that stops at the first error | Result[Binary, ArithmeticError] in a wrapper |
| decimal_checked | IEEE decimal pipeline that accumulates flags | value with latest and accumulated flags |
| decimal_gda_checked | GDA pipeline that stops at a trap | one threaded GdaOutcome |
| interval_checked | interval pipeline that stops at the first error | Result[Interval, ArithmeticError] in a wrapper |
| def | shared vocabulary: Sign, PartialOrder, the Floating trait | — |
Install
Download zipArbitrary-precision binary, decimal, and interval arithmetic for MoonBit, with checked operations and explicit numeric semantics.