arithmetic

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.

This manual describes version 0.5.0 together with the unreleased MoonBit 0.10 migration listed in CHANGELOG.md.

Where it sits

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.

TierExampleReturnsUse it when
UncheckedSqrt::sqrtSelfthe type’s own special-value behaviour is acceptable
CheckedSqrtChecked::sqrt_checkedResult[Self, ArithmeticError]invalid input must become a handled error
ContextualSqrtContextual::sqrt_contextualResult[ArithmeticOutcome[Self], ArithmeticError]precision and rounding are explicit and diagnostics matter
Enclosure relationDefinitelyLt::definitely_ltBoolvalues are intervals or balls, not points

Packages

The module has one package, at the source root src/, documented as core.

PackageImport pathContentsPages
coreLuna-Flow/arithmeticcapability traits, ArithmeticContext, diagnostics, errors and certification details, Float/Double/integer instancesAPI, tutorial, design

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.

Reading paths

  • 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.

Install

moon add Luna-Flow/arithmetic@0.5.0
import {
  "Luna-Flow/arithmetic" @lf_arith,
}

The package depends on Kaida-Amethyst/math for the Float and Double elementary functions. Its tests also use Luna-Flow/luna-generic.

Toolchain

The code targets MoonBit moonc 0.10 or later with the moon.mod and moon.pkg manifests, and is checked on the wasm-gc, wasm, js and native backends.

Guides