Core API
Purpose
arithmetic extends luna-generic with analytic capabilities, checked operations, contextual outcomes, and enclosure relations. Public traits remain small so algorithms can request only the capabilities they use.
Unchecked Capability Traits
Sqrt,Cbrt,RadicalExponential,Logarithmic,PowerTrigonometric,InverseTrigonometricHyperbolic,InverseHyperbolicConstants
These traits are defined in src/elementary.mbt and return direct backend values.
Checked Surface
Shared Types
FpClassRoundingModeArithmeticContextCertificationStageCertificationFailureReasonCertificationFailureDetailArithmeticErrorKindArithmeticError
Context Construction
ArithmeticContext::new accepts precision, rounding mode, optional e_min and e_max, and an optional clamp flag. Precision is clamped to at least 1. Construction aborts when both exponent limits are present and e_min exceeds e_max.
The package also provides these presets:
ArithmeticContext::decimal32: precision7, exponent range-95...96ArithmeticContext::decimal64: precision16, exponent range-383...384ArithmeticContext::decimal128: precision34, exponent range-6143...6144
All three presets use ToNearestEven and enable clamping.
Checked Traits
SqrtCheckedDivCheckedCompareCheckedPowNatCheckedPowIntCheckedParseChecked
These traits return Result[..., ArithmeticError]. Operations that require an arithmetic context accept it explicitly.
Contextual Outcome Surface
Result Types
ArithmeticDiagnostics contains the flags inexact, rounded, overflow, underflow, subnormal, and clamped. empty and new construct diagnostic values; combine merges two values using logical OR for every flag.
ArithmeticOutcome[T] stores value : T and its diagnostics. exact creates an outcome with empty diagnostics, while with_diagnostics preserves supplied flags.
Contextual Traits
AddContextual,SubContextual,MulContextual,DivContextualAbsContextual,SqrtContextual,ExpContextualIntegralContextual,AdjacentContextualConstantsContextual,HyperbolicContextualNumericFormatContextual
IntegralContextual::from_int_contextual embeds a MoonBit Int under the target context and returns conversion diagnostics. Wider integer sources use separate capabilities. AdjacentContextual exposes next_plus_contextual, next_minus_contextual, and next_toward_contextual. ConstantsContextual produces pi, tau, and e under a context and may return an ArithmeticError whose kind is ArithmeticErrorKind::CertificationFailure when a proof-backed implementation cannot certify target rounding.
HyperbolicContextual exposes contextual sinh, cosh, and tanh, each as an ArithmeticOutcome inside Result.
NumericFormatContextual provides contextual zero, one, epsilon, minimum normal value, maximum finite value, and classification as Finite, Infinity, or NaN.
Enclosure Relations
ContainsOverlapsDefinitelyLtDefinitelyLeMaybeEq
These are relations rather than a scalar total-order abstraction.
Shipped Instances
FloatandDoubleimplement the broad elementary surface throughKaida-Amethyst/math.FloatandDoubleimplement the checked square-root, division, comparison, and integer-power surfaces.FloatandDoubleimplement the original contextual surface plusIntegralContextualandAdjacentContextual.FloatandDoubleintentionally do not implementConstantsContextualorHyperbolicContextual; context-faithful numeric backends provide those capabilities.BigIntand the integer family implement the narrower exact subset that remains closed on each concrete type.
Semantic Notes
- Unchecked traits leave domain restrictions and branch semantics to the concrete instance.
- Built-in checked square root returns
DomainErrorfor negative real input. - Built-in checked division classifies
0 / 0and infinity divided by infinity asDomainError; non-zero division by zero remainsDivisionByZero. - Built-in contextual division and square root delegate to checked operations and wrap successful values with exact diagnostics.
- Built-in
Floatinteger embedding setsinexactandroundedwhen the source integer is not exactly representable;Doubleexactly embeds every MoonBitInt. - Built-in adjacent operations follow the fixed IEEE binary representation, including signed-zero and infinity boundaries. Selecting the adjacent value is exact in that fixed representable set and returns empty diagnostics.
- The built-in implementations do not apply context-controlled precision, rounding, exponent clamping, or status-flag detection.
- Signed integer and
BigIntPowerimplementations require non-negative exponents; negative exponents abort. PowNatCheckedtreatsx^0as the multiplicative identity, including0^0.