arithmetic
This manual documents release 0.5.2 of Luna-Flow/arithmetic together with
the unreleased MoonBit 0.10 migration listed in
CHANGELOG.md.
Overview
Luna-Flow/arithmetic defines the analytic capabilities of Luna Flow numeric
types: elementary functions, checked operations that return structured errors,
contextual operations that report how a result was rounded under an explicit
precision and rounding mode, and relations between enclosures such as
intervals. It ships default instances for Float, Double and the integer
types, and leaves context-faithful and certified arithmetic to backends that
implement the same traits.
luna-generic says what a type is
(Ring, Field, …). arithmetic says which analytic operations it
supports and how they fail. Numeric backends implement the traits, and
algorithms depend on the traits instead of on concrete number types.
| Tier | Example | Returns | Use it when |
|---|---|---|---|
| Unchecked | Sqrt::sqrt | Self | the type’s own special-value behaviour is acceptable |
| Checked | SqrtChecked::sqrt_checked | Result[Self, ArithmeticError] | invalid input must become a handled error |
| Contextual | SqrtContextual::sqrt_contextual | Result[ArithmeticOutcome[Self], ArithmeticError] | precision and rounding are explicit and diagnostics matter |
| Enclosure relation | DefinitelyLt::definitely_lt | Bool | values are intervals or balls, not points |
Install
moon add Luna-Flow/arithmetic@0.5.2
Then import "Luna-Flow/arithmetic" in your moon.pkg. Luna Flow code uses
the alias @lf_arith:
import {
"Luna-Flow/arithmetic" @lf_arith,
}
The package needs the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20) with
the moon.mod and moon.pkg manifests, and is checked on the wasm-gc,
wasm, js and native backends. It depends on Kaida-Amethyst/math for
the Float and Double elementary functions; its tests also use
Luna-Flow/luna-generic.
Pages
The module has one package, at the source root src/, documented as core.
| Part | Tutorial | API | Design |
|---|---|---|---|
core: capability traits, context, diagnostics, errors, instances | tutorial | API | design |
Guides that span the package:
| Guide | Contents |
|---|---|
| Getting started | install, first generic function, first checked and contextual calls |
| Architecture | source layout, tiers, errors and state |
| Verification | the local gate, CI and what the tests establish |
| Repository conventions | rules for this manual on top of the Luna Flow standard |
The blackbox tests (src/*_test.mbt) and the whitebox test
(src/certification_error_wbtest.mbt) belong to the same package; the
verification guide lists what they establish.
Exported traits
- Unchecked:
Sqrt,Cbrt,Radical,Exponential,Logarithmic,Power,Trigonometric,InverseTrigonometric,Hyperbolic,InverseHyperbolic,Constants - Checked:
SqrtChecked,DivChecked,CompareChecked,PowNatChecked,PowIntChecked,ParseChecked - Contextual:
AddContextual,SubContextual,MulContextual,DivContextual,AbsContextual,SqrtContextual,ExpContextual,IntegralContextual,AdjacentContextual,ConstantsContextual,HyperbolicContextual,NumericFormatContextual - Enclosure relations:
Contains,Overlaps,DefinitelyLt,DefinitelyLe,MaybeEq
Exported types
- Context:
ArithmeticContextwith the presetsdecimal32,decimal64anddecimal128, andRoundingMode - Results:
ArithmeticOutcome[T]andArithmeticDiagnostics - Errors:
ArithmeticError,ArithmeticErrorKind, and the certification evidenceCertificationFailureDetail,CertificationStageandCertificationFailureReason - Classification:
FpClass
Shipped instances
FloatandDouble: every unchecked trait, every checked trait exceptParseChecked, and the contextual arithmetic, absolute value, square root, exponential, integer embedding, adjacent values and format queriesInt,Int16,Int64,UInt,UInt16,UInt64andBigInt:Poweronly- No instance of
ParseChecked,ConstantsContextual,HyperbolicContextualor the enclosure relations; backends provide them
Where to read next
The core tutorial works through tasks with every tier. The core API lists every public item with what each shipped instance does at the edges, and the core design derives the rounding, enclosure and error-bound mathematics behind the contracts.
- New to the package: read getting started, then the core tutorial.
- Using it in a library: keep the core API at hand; its tables say what each shipped instance actually does at the edges (NaN, zero, infinities, overflow).
- Implementing a backend or contributing: read the core design for the contracts and their mathematics, architecture for the layout, and verification and conventions before opening a pull request.
Validation
Recommended release checks:
moon check --target all
moon test
moon test --target js
moon test --target native
The verification guide has the full gate, including the documentation checks.