Arithmetic capability traits and checked analytic operations for LunaFlow numeric types, with default real-valued instances.
Dependencies
moon add Luna-Flow/arithmetic@0.4.0
moon add Luna-Flow/luna-generic@0.3.1import {
"Luna-Flow/luna-generic" @lf_alg,
"Luna-Flow/arithmetic" @lf_arith,
}using @lf_alg { trait Add, trait Mul }
using @lf_arith { trait Sqrt }
fn hypot2[T : Add + Mul + Sqrt](x : T, y : T) -> T {
Sqrt::sqrt(x * x + y * y)
}
let context = @lf_arith.ArithmeticContext::decimal64()
let outcome = @lf_arith.DivContextual::div_contextual(10.0, 4.0, context).unwrap()
inspect(outcome.value, content="2.5")
inspect(outcome.diagnostics.inexact, content="false")moon fmt
moon info
moon check --target all
moon testpub(open) trait AbsContextual {
fn abs_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl AbsContextual for Floatfn abs_contextual(value : Float, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl AbsContextual for Doublefn abs_contextual(value : Double, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]pub(open) trait AddContextual {
fn add_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl AddContextual for Floatfn add_contextual(left : Float, right : Float, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl AddContextual for Doublefn add_contextual(left : Double, right : Double, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]pub(open) trait Cbrt {
fn cbrt(Self) -> Self
}pub(open) trait Constants {
fn pi() -> Self
fn tau() -> Self
fn e() -> Self
}pub(open) trait DivChecked {
fn div_checked(Self, Self, ArithmeticContext) -> Result[Self, ArithmeticError]
}impl DivChecked for Floatfn div_checked(lhs : Float, rhs : Float, _ctx : ArithmeticContext) -> Result[Float, ArithmeticError]impl DivChecked for Doublefn div_checked(lhs : Double, rhs : Double, _ctx : ArithmeticContext) -> Result[Double, ArithmeticError]pub(open) trait DivContextual {
fn div_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl DivContextual for Floatfn div_contextual(left : Float, right : Float, context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl DivContextual for Doublefn div_contextual(left : Double, right : Double, context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]pub(open) trait ExpContextual {
fn exp_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl ExpContextual for Floatfn exp_contextual(value : Float, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl ExpContextual for Doublefn exp_contextual(value : Double, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]pub(open) trait Exponential {
fn exp(Self) -> Self
fn exp2(Self) -> Self
}pub(open) trait Hyperbolic {
fn sinh(Self) -> Self
fn cosh(Self) -> Self
fn tanh(Self) -> Self
}pub(open) trait InverseHyperbolic {
fn asinh(Self) -> Self
fn acosh(Self) -> Self
fn atanh(Self) -> Self
}pub(open) trait InverseTrigonometric {
fn asin(Self) -> Self
fn acos(Self) -> Self
fn atan(Self) -> Self
fn atan2(Self, Self) -> Self
}impl InverseTrigonometric for Floatpub(open) trait Logarithmic {
fn ln(Self) -> Self
fn log2(Self) -> Self
fn log10(Self) -> Self
}pub(open) trait MulContextual {
fn mul_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl MulContextual for Floatfn mul_contextual(left : Float, right : Float, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl MulContextual for Doublefn mul_contextual(left : Double, right : Double, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]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
}pub(open) trait ParseChecked {
fn parse_checked(String, ArithmeticContext) -> Result[Self, ArithmeticError]
}pub(open) trait PowIntChecked {
fn pow_int_checked(Self, Int, ArithmeticContext) -> Result[Self, ArithmeticError]
}impl PowIntChecked for Floatfn pow_int_checked(base : Float, exponent : Int, ctx : ArithmeticContext) -> Result[Float, ArithmeticError]impl PowIntChecked for Doublefn pow_int_checked(base : Double, exponent : Int, ctx : ArithmeticContext) -> Result[Double, ArithmeticError]pub(open) trait PowNatChecked {
fn pow_nat_checked(Self, UInt, ArithmeticContext) -> Result[Self, ArithmeticError]
}impl PowNatChecked for Floatfn pow_nat_checked(base : Float, exponent : UInt, _ctx : ArithmeticContext) -> Result[Float, ArithmeticError]impl PowNatChecked for Doublefn pow_nat_checked(base : Double, exponent : UInt, _ctx : ArithmeticContext) -> Result[Double, ArithmeticError]pub(open) trait Power {
fn pow(Self, Self) -> Self
}pub(open) trait Sqrt {
fn sqrt(Self) -> Self
}pub(open) trait SqrtChecked {
fn sqrt_checked(Self, ArithmeticContext) -> Result[Self, ArithmeticError]
}pub(open) trait SqrtContextual {
fn sqrt_contextual(Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl SqrtContextual for Floatfn sqrt_contextual(value : Float, context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl SqrtContextual for Doublefn sqrt_contextual(value : Double, context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]pub(open) trait SubContextual {
fn sub_contextual(Self, Self, ArithmeticContext) -> Result[ArithmeticOutcome[Self], ArithmeticError]
}impl SubContextual for Floatfn sub_contextual(left : Float, right : Float, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Float], ArithmeticError]impl SubContextual for Doublefn sub_contextual(left : Double, right : Double, _context : ArithmeticContext) -> Result[ArithmeticOutcome[Double], ArithmeticError]pub(open) trait Trigonometric {
fn sin(Self) -> Self
fn cos(Self) -> Self
fn tan(Self) -> Self
}pub struct ArithmeticContext {
precision : Int
rounding : RoundingMode
e_min : Int?
e_max : Int?
clamp : Bool
} derive(Eq)fn ArithmeticContext::new(precision : Int, rounding? : RoundingMode, e_min? : Int, e_max? : Int, clamp? : Bool) -> ArithmeticContextfn ArithmeticDiagnostics::combine(self : ArithmeticDiagnostics, other : ArithmeticDiagnostics) -> ArithmeticDiagnosticsfn ArithmeticDiagnostics::new(inexact? : Bool, rounded? : Bool, overflow? : Bool, underflow? : Bool, subnormal? : Bool, clamped? : Bool) -> ArithmeticDiagnosticsfn ArithmeticError::certification_failure_detail(self : ArithmeticError) -> CertificationFailureDetail?pub enum ArithmeticErrorKind {
DivisionByZero
ParseError
DomainError
FormatError
UnsupportedOperation
UnorderedComparison
CertificationFailure(CertificationFailureDetail)
} derive(Eq)fn[T] ArithmeticOutcome::with_diagnostics(value : T, diagnostics : ArithmeticDiagnostics) -> ArithmeticOutcome[T]pub struct CertificationFailureDetail {
operation : String
stage : CertificationStage
reason : CertificationFailureReason
target_precision : Int
work_precision : Int
refinements : Int
} derive(Eq)fn CertificationFailureDetail::new(operation : String, stage : CertificationStage, reason : CertificationFailureReason, target_precision : Int, work_precision : Int, refinements : Int) -> CertificationFailureDetailfn CertificationFailureDetail::reason(self : CertificationFailureDetail) -> CertificationFailureReasonpub(all) enum CertificationFailureReason {
RangeNotCertified
SeriesDidNotConverge
InvalidEnclosure
ResourceLimit
RefinementBudgetExhausted
} derive(Eq)pub(all) enum CertificationStage {
RangeReduction
SeriesEvaluation
EnclosurePropagation
TargetRounding
} derive(Eq)Arithmetic capability traits and checked analytic operations for LunaFlow numeric types, with default real-valued instances.
Dependencies