Sign in

    arithmetic

    Arithmetic capability traits and checked analytic operations for LunaFlow numeric types, with default real-valued instances.

    math
    arithmetic
    elementary-functions
    Download zip
    Author
    Version
    0.5.1
    License
    Apache-2.0
    Last updated
    6 hours ago
    Downloads
    864

    #arithmetic

    Analytic capability traits for Luna Flow numeric types. arithmetic lets an algorithm state exactly which operations it needs (a square root, an exponential, a checked division, a correctly reported rounding) and how failures must surface, without assuming a universal "real number" type. It ships default instances for Float, Double and the integer types; numeric backends implement the same traits with context-faithful and certified arithmetic.

    #Capability tiers

    TierTraitsReturns
    UncheckedSqrt, Cbrt, Radical, Exponential, Logarithmic, Power, Trigonometric, InverseTrigonometric, Hyperbolic, InverseHyperbolic, ConstantsSelf
    CheckedSqrtChecked, DivChecked, CompareChecked, PowNatChecked, PowIntChecked, ParseCheckedResult[Self, ArithmeticError]
    ContextualAddContextual, SubContextual, MulContextual, DivContextual, AbsContextual, SqrtContextual, ExpContextual, IntegralContextual, AdjacentContextual, ConstantsContextual, HyperbolicContextual, NumericFormatContextualResult[ArithmeticOutcome[Self], ArithmeticError]
    Enclosure relationsContains, Overlaps, DefinitelyLt, DefinitelyLe, MaybeEqBool

    Checked and contextual operations take an explicit, immutable ArithmeticContext (precision, rounding mode, exponent range); there is no global numeric state. Contextual results carry ArithmeticDiagnostics flags, and proof-backed backends report inconclusive evaluations as structured certification failures.

    The built-in Float and Double contextual instances keep native IEEE behaviour: they ignore the context and, except for the Float integer embedding, do not detect rounding.

    #Install

    moon add Luna-Flow/arithmetic@0.5.1

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

    #Example

    fn[T : Add + Mul + @lf_arith.Sqrt] hypot(x : T, y : T) -> T {
    @lf_arith.Sqrt::sqrt(x * x + y * y)
    }

    test "readme" {
    inspect(hypot(3.0, 4.0), content="5")
    let ctx = @lf_arith.ArithmeticContext::decimal64()
    let q = @lf_arith.DivContextual::div_contextual(10.0, 4.0, ctx).unwrap()
    inspect(q.value, content="2.5")
    inspect(@lf_arith.DivChecked::div_checked(1.0, 0.0, ctx) is Err(_), content="true")
    }

    #Packages

    PackageImport pathDocumentation
    core (src/)Luna-Flow/arithmeticAPI, tutorial, design

    #Requirements

    MoonBit moonc 0.10 or later with moon.mod/moon.pkg manifests. All backends (wasm-gc, wasm, js, native) are supported. The Float and Double elementary functions come from Kaida-Amethyst/math.

    #Documentation

    The manual is published at lunaflow.cn/en/arithmetic with Simplified Chinese and Japanese translations. Its English source is doc/manual/index.md; translations are gettext catalogs in doc/locale.

    #Contributing

    Run the gate in doc/manual/verification.md (moon fmt, moon info, moon check --target all, moon test on the default, JavaScript and native targets) and follow doc/manual/conventions.md for documentation. Commits follow Conventional Commits. Release history is in CHANGELOG.md.

    #License

    Apache-2.0. See LICENSE.

    AbsContextual

    pub(open) trait AbsContextual {
    fn abs_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    AddContextual

    pub(open) trait AddContextual {
    fn add_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    AdjacentContextual

    pub(open) trait AdjacentContextual {
    fn next_plus_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    fn next_minus_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    fn next_toward_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    Cbrt

    pub(open) trait Cbrt {
    fn cbrt(Self) -> Self
    }

    impl Cbrt for Float
    impl Cbrt for Double

    CompareChecked

    pub(open) trait CompareChecked {
    fn compare_checked(Self, Self) -> Result[Int, ArithmeticError]
    }

    Constants

    pub(open) trait Constants {
    fn pi() -> Self
    fn tau() -> Self
    fn e() -> Self
    }

    impl Constants for Float
    impl Constants for Double

    ConstantsContextual

    pub(open) trait ConstantsContextual {
    fn pi_contextual(ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    fn tau_contextual(ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    fn e_contextual(ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    Contains

    pub(open) trait Contains {
    fn contains(Self, Self) -> Bool
    }

    DefinitelyLe

    pub(open) trait DefinitelyLe {
    fn definitely_le(Self, Self) -> Bool
    }

    DefinitelyLt

    pub(open) trait DefinitelyLt {
    fn definitely_lt(Self, Self) -> Bool
    }

    DivChecked

    pub(open) trait DivChecked {
    fn div_checked(Self, Self, ArithmeticContext) -> Result[Self, ArithmeticError]
    }

    impl DivChecked for Float

    DivContextual

    pub(open) trait DivContextual {
    fn div_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    ExpContextual

    pub(open) trait ExpContextual {
    fn exp_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    Exponential

    pub(open) trait Exponential {
    fn exp(Self) -> Self
    fn exp2(Self) -> Self
    }

    Hyperbolic

    pub(open) trait Hyperbolic {
    fn sinh(Self) -> Self
    fn cosh(Self) -> Self
    fn tanh(Self) -> Self
    }

    impl Hyperbolic for Float

    HyperbolicContextual

    pub(open) trait HyperbolicContextual {
    fn sinh_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    fn cosh_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    fn tanh_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    IntegralContextual

    pub(open) trait IntegralContextual {
    fn from_int_contextual(Int, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    InverseHyperbolic

    pub(open) trait InverseHyperbolic {
    fn asinh(Self) -> Self
    fn acosh(Self) -> Self
    fn atanh(Self) -> Self
    }

    InverseTrigonometric

    pub(open) trait InverseTrigonometric {
    fn asin(Self) -> Self
    fn acos(Self) -> Self
    fn atan(Self) -> Self
    fn atan2(Self, Self) -> Self
    }

    Logarithmic

    pub(open) trait Logarithmic {
    fn ln(Self) -> Self
    fn log2(Self) -> Self
    fn log10(Self) -> Self
    }

    MaybeEq

    pub(open) trait MaybeEq {
    fn maybe_eq(Self, Self) -> Bool
    }

    MulContextual

    pub(open) trait MulContextual {
    fn mul_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    NumericFormatContextual

    pub(open) trait NumericFormatContextual {
    fn zero_contextual(ArithmeticContext) -> Self
    fn one_contextual(ArithmeticContext) -> Self
    fn epsilon_contextual(ArithmeticContext) -> Self
    fn min_normal_contextual(ArithmeticContext) -> Self
    fn max_finite_contextual(ArithmeticContext) -> Self
    fn classify_contextual(Self) -> FpClass
    }

    Overlaps

    pub(open) trait Overlaps {
    fn overlaps(Self, Self) -> Bool
    }

    ParseChecked

    pub(open) trait ParseChecked {
    fn parse_checked(String, ArithmeticContext) -> Result[Self, ArithmeticError]
    }

    PowIntChecked

    pub(open) trait PowIntChecked {
    fn pow_int_checked(Self, Int, ArithmeticContext) -> Result[Self, ArithmeticError]
    }

    PowNatChecked

    pub(open) trait PowNatChecked {
    fn pow_nat_checked(Self, UInt, ArithmeticContext) -> Result[Self, ArithmeticError]
    }

    Power

    pub(open) trait Power {
    fn pow(Self, Self) -> Self
    }

    impl Power for Int
    impl Power for Int16
    impl Power for Int64
    impl Power for UInt
    impl Power for UInt16
    impl Power for UInt64
    impl Power for Float
    impl Power for Double

    Radical

    pub(open) trait Radical : Sqrt + Cbrt {
    }

    impl Radical for Float
    impl Radical for Double

    Sqrt

    pub(open) trait Sqrt {
    fn sqrt(Self) -> Self
    }

    impl Sqrt for Float
    impl Sqrt for Double

    SqrtChecked

    pub(open) trait SqrtChecked {
    fn sqrt_checked(Self, ArithmeticContext) -> Result[Self, ArithmeticError]
    }

    SqrtContextual

    pub(open) trait SqrtContextual {
    fn sqrt_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    SubContextual

    pub(open) trait SubContextual {
    fn sub_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
    }

    Trigonometric

    pub(open) trait Trigonometric {
    fn sin(Self) -> Self
    fn cos(Self) -> Self
    fn tan(Self) -> Self
    }

    ArithmeticContext

    pub struct ArithmeticContext {
    precision : Int
    rounding : RoundingMode
    e_min : Int?
    e_max : Int?
    clamp : Bool
    } derive(Eq)

    ArithmeticContext::decimal128

    ArithmeticContext::decimal32

    ArithmeticContext::decimal64

    ArithmeticContext::equal

    ArithmeticContext::new

    fn ArithmeticContext::new(precision : Int, rounding? : RoundingMode, e_min? : Int, e_max? : Int, clamp? : Bool) -> ArithmeticContext

    ArithmeticDiagnostics

    pub struct ArithmeticDiagnostics {
    inexact : Bool
    rounded : Bool
    overflow : Bool
    underflow : Bool
    subnormal : Bool
    clamped : Bool
    } derive(Eq,
    Debug
    )

    ArithmeticDiagnostics::combine

    ArithmeticDiagnostics::empty

    ArithmeticDiagnostics::equal

    ArithmeticDiagnostics::new

    fn ArithmeticDiagnostics::new(inexact? : Bool, rounded? : Bool, overflow? : Bool, underflow? : Bool, subnormal? : Bool, clamped? : Bool) -> ArithmeticDiagnostics

    ArithmeticError

    pub struct ArithmeticError {
    kind : ArithmeticErrorKind
    message : String
    } derive(Eq)

    ArithmeticError::certification_failure

    fn ArithmeticError::certification_failure(detail : CertificationFailureDetail) -> ArithmeticError

    ArithmeticError::certification_failure_detail

    fn ArithmeticError::certification_failure_detail(self : ArithmeticError) -> CertificationFailureDetail?

    ArithmeticError::division_by_zero

    fn ArithmeticError::division_by_zero(message : String) -> ArithmeticError

    ArithmeticError::domain_error

    fn ArithmeticError::domain_error(message : String) -> ArithmeticError

    ArithmeticError::equal

    ArithmeticError::format_error

    fn ArithmeticError::format_error(message : String) -> ArithmeticError

    ArithmeticError::is_certification_failure

    fn ArithmeticError::is_certification_failure(self : ArithmeticError) -> Bool

    ArithmeticError::is_division_by_zero

    fn ArithmeticError::is_division_by_zero(self : ArithmeticError) -> Bool

    ArithmeticError::is_domain_error

    fn ArithmeticError::is_domain_error(self : ArithmeticError) -> Bool

    ArithmeticError::is_format_error

    fn ArithmeticError::is_format_error(self : ArithmeticError) -> Bool

    ArithmeticError::is_parse_error

    fn ArithmeticError::is_parse_error(self : ArithmeticError) -> Bool

    ArithmeticError::is_unordered_comparison

    fn ArithmeticError::is_unordered_comparison(self : ArithmeticError) -> Bool

    ArithmeticError::is_unsupported

    fn ArithmeticError::is_unsupported(self : ArithmeticError) -> Bool

    ArithmeticError::parse_error

    fn ArithmeticError::parse_error(message : String) -> ArithmeticError

    ArithmeticError::unordered_comparison

    fn ArithmeticError::unordered_comparison(message : String) -> ArithmeticError

    ArithmeticError::unsupported

    fn ArithmeticError::unsupported(message : String) -> ArithmeticError

    ArithmeticErrorKind

    pub enum ArithmeticErrorKind {
    DivisionByZero
    ParseError
    DomainError
    FormatError
    UnsupportedOperation
    UnorderedComparison
    CertificationFailure(CertificationFailureDetail)
    } derive(Eq)

    ArithmeticErrorKind::equal

    ArithmeticOutcome

    pub struct ArithmeticOutcome[T] {
    value : T
    diagnostics : ArithmeticDiagnostics
    } derive(Eq,
    Debug
    )

    ArithmeticOutcome::equal

    ArithmeticOutcome::exact

    fn[T] ArithmeticOutcome::exact(value : T) -> ArithmeticOutcome[T]

    ArithmeticOutcome::with_diagnostics

    fn[T] ArithmeticOutcome::with_diagnostics(value : T, diagnostics : ArithmeticDiagnostics) -> ArithmeticOutcome[T]

    CertificationFailureDetail

    pub struct CertificationFailureDetail {
    operation : String
    stage : CertificationStage
    reason : CertificationFailureReason
    target_precision : Int
    work_precision : Int
    refinements : Int
    } derive(Eq)

    CertificationFailureDetail::equal

    CertificationFailureDetail::new

    fn CertificationFailureDetail::new(operation : String, stage : CertificationStage, reason : CertificationFailureReason, target_precision : Int, work_precision : Int, refinements : Int) -> CertificationFailureDetail

    CertificationFailureDetail::operation

    CertificationFailureDetail::reason

    CertificationFailureDetail::refinements

    CertificationFailureDetail::stage

    CertificationFailureDetail::target_precision

    fn CertificationFailureDetail::target_precision(self : CertificationFailureDetail) -> Int

    CertificationFailureDetail::work_precision

    fn CertificationFailureDetail::work_precision(self : CertificationFailureDetail) -> Int

    CertificationFailureReason

    pub(all) enum CertificationFailureReason {
    RangeNotCertified
    SeriesDidNotConverge
    InvalidEnclosure
    ResourceLimit
    RefinementBudgetExhausted
    } derive(Eq)

    CertificationFailureReason::equal

    CertificationStage

    pub(all) enum CertificationStage {
    RangeReduction
    SeriesEvaluation
    EnclosurePropagation
    TargetRounding
    } derive(Eq)

    CertificationStage::equal

    FpClass

    pub(all) enum FpClass {
    Finite
    Infinity
    NaN
    } derive(Eq,
    Debug
    )

    FpClass::equal

    fn FpClass::equal(FpClass, FpClass) -> Bool

    RoundingMode

    pub(all) enum RoundingMode {
    ToNearestEven
    TowardZero
    TowardPositive
    TowardNegative
    AwayFromZero
    } derive(Eq)

    RoundingMode::equal