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.

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

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.

PartTutorialAPIDesign
core: capability traits, context, diagnostics, errors, instancestutorialAPIdesign

Guides that span the package:

GuideContents
Getting startedinstall, first generic function, first checked and contextual calls
Architecturesource layout, tiers, errors and state
Verificationthe local gate, CI and what the tests establish
Repository conventionsrules 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: ArithmeticContext with the presets decimal32, decimal64 and decimal128, and RoundingMode
  • Results: ArithmeticOutcome[T] and ArithmeticDiagnostics
  • Errors: ArithmeticError, ArithmeticErrorKind, and the certification evidence CertificationFailureDetail, CertificationStage and CertificationFailureReason
  • Classification: FpClass

Shipped instances

  • Float and Double: every unchecked trait, every checked trait except ParseChecked, and the contextual arithmetic, absolute value, square root, exponential, integer embedding, adjacent values and format queries
  • Int, Int16, Int64, UInt, UInt16, UInt64 and BigInt: Power only
  • No instance of ParseChecked, ConstantsContextual, HyperbolicContextual or the enclosure relations; backends provide them

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.