binary_checked API
用途
binary_checked 提供 BinaryChecked,它是对 Result[Binary, ArithmeticError] 的封闭包装。每个运算都接受并返回 BinaryChecked:当所有操作数均为成功值时,执行相应的 binary 运算,否则不执行运算而直接传递第一个错误。该包装不保留任何 IEEE 标志:受检运算把无效运算和除以零异常变为错误,算术 _ctx 方法则丢弃它们。教程逐步构建运算流水线;设计页面将该包装建模为错误单子(error monad),并证明了它所依赖的复合律。
导入
在你的 moon.pkg 中添加该包装,以及类型出现在其签名中的各个包:
import {
"Luna-Flow/floating/binary",
"Luna-Flow/floating/binary_checked",
"Luna-Flow/floating/def",
"Luna-Flow/arithmetic" @lf_arith,
}
示例中以 @binary_checked.、@binary.、@def. 和 @lf_arith.(即 Luna-Flow/arithmetic,在接口中打印为 @arithmetic)调用这些包。示例使用以下辅助函数输出结果:
///|
fn show(r : @binary_checked.BinaryChecked) -> String {
match r.result() {
Ok(v) => v.to_string()
Err(e) => "error: " + e.message
}
}
Binary::to_string 以 coefficient p exponent 的形式打印精确值,因此 3p-1 表示 。
包装类型
BinaryChecked
BinaryChecked 要么是一个成功的 Binary,要么是一个 ArithmeticError。
pub struct BinaryChecked {
// private fields
}
唯一的字段是私有的,保存一个 Result[@binary.Binary, @arithmetic.ArithmeticError]。该类型没有 Eq 或 Show 实例;请通过 result() 进行比较或打印。
构造
BinaryChecked::ok, BinaryChecked::err, BinaryChecked::from_result
这些函数包装已有的值、错误或 Result,不做任何改变。
pub fn BinaryChecked::ok(@binary.Binary) -> Self
pub fn BinaryChecked::err(@arithmetic.ArithmeticError) -> Self
pub fn BinaryChecked::from_result(Result[@binary.Binary, @arithmetic.ArithmeticError]) -> Self
对每个 r 都有 from_result(r).result() == r,而 ok(x) 是设计页面中所述单子的单位元。
BinaryChecked::from_int, BinaryChecked::from_coefficient, BinaryChecked::from_double, BinaryChecked::from_float
这些构造函数通过对应的 Binary 构造函数,由 MoonBit 数值构建一个成功的包装值。
pub fn BinaryChecked::from_int(Int, precision? : Int) -> Self
pub fn BinaryChecked::from_coefficient(@binary.BinCoeff, precision? : Int, negative? : Bool) -> Self
pub fn BinaryChecked::from_double(Double, precision? : Int) -> Self
pub fn BinaryChecked::from_float(Float, precision? : Int) -> Self
| 构造子 | 默认 precision | 委托给 |
|---|---|---|
from_int | 53 | Binary::from_int |
from_coefficient | 53(negative 默认为 false) | Binary::from_coefficient |
from_double | 53 | Binary::from_double |
from_float | 24 | Binary::from_float |
它们从不产生错误:NaN 与无穷输入会变成成功的 NaN 或无穷值;无法容纳于 precision 的值按被委托的构造函数那样以就近舍入(偶数优先)处理。from_float 经由宿主的 Double 扩宽,因此信号 Float NaN 到达时可能已是静默的(参见 Binary::from_float)。
观察
BinaryChecked::result, BinaryChecked::error, BinaryChecked::is_ok, BinaryChecked::is_err
这些方法暴露被包装的 Result、它的错误,并测试其分支。它们是检查层中流水线类型的出口:DecimalChecked 和 GdaDecimalChecked 有同样的四个。
pub fn BinaryChecked::result(Self) -> Result[@binary.Binary, @arithmetic.ArithmeticError]
pub fn BinaryChecked::error(Self) -> @arithmetic.ArithmeticError?
pub fn BinaryChecked::is_ok(Self) -> Bool
pub fn BinaryChecked::is_err(Self) -> Bool
is_err(r) == !is_ok(r),并且 error(r) 恰好在 result(r) 为 Err(e) 时是 Some(e)。只在处理错误的边界处调用一次 result()。
组合
BinaryChecked::map
map(f) 将一个不会失败的函数作用于成功值,错误则保持不变。
pub fn BinaryChecked::map(Self, (@binary.Binary) -> @binary.Binary) -> Self
且 ;遇到错误时不会调用 f。map 永远不会把成功值变成错误。
BinaryChecked::bind
bind(f) 应用一个自身可能失败的函数。
pub fn BinaryChecked::bind(Self, (@binary.Binary) -> Self) -> Self
且 。与 ok 一起,它满足单子律(左单位律、右单位律与结合律),推导见设计页面。
///|
test "map and bind" {
let halve = fn(x : @binary.Binary) { x * @binary.Binary::from_double(0.5) }
let positive = fn(x : @binary.Binary) {
if x.signum() == @def.Sign::Positive {
@binary_checked.BinaryChecked::ok(x)
} else {
@binary_checked.BinaryChecked::err(
@lf_arith.ArithmeticError::domain_error("expected a positive value"),
)
}
}
let good = @binary_checked.BinaryChecked::from_int(3).map(halve).bind(positive)
inspect(show(good), content="3p-1")
let bad = @binary_checked.BinaryChecked::from_int(-3).bind(positive).map(halve)
inspect(show(bad), content="error: expected a positive value")
}
一元值映射
BinaryChecked::neg, BinaryChecked::abs, BinaryChecked::ulp, BinaryChecked::normalized, BinaryChecked::with_precision
这些方法是同名 Binary 方法的 map,从不引入错误。
pub fn BinaryChecked::neg(Self) -> Self
pub fn BinaryChecked::abs(Self) -> Self
pub fn BinaryChecked::ulp(Self) -> Self
pub fn BinaryChecked::normalized(Self) -> Self
pub fn BinaryChecked::with_precision(Self, Int, @arithmetic.RoundingMode) -> Self
with_precision(p, mode) 以方向 mode 舍入到 位,指数无界;ulp 是该值精度下的末位单位(对非有限值为 NaN)。
算术
BinaryChecked::add, BinaryChecked::sub, BinaryChecked::mul
这些方法用 Binary 运算符组合两个包装值。
pub fn BinaryChecked::add(Self, Self) -> Self
pub fn BinaryChecked::sub(Self, Self) -> Self
pub fn BinaryChecked::mul(Self, Self) -> Self
若 self 是错误则返回它;否则若 other 是错误则返回该错误;否则结果为 Ok(lhs op rhs)。Binary 运算符以两个操作数中较大的精度进行就近舍入(偶数优先),且从不失败:诸如 的无效情形会产生一个成功的 NaN。
BinaryChecked::div
div 对两个包装值做除法,并报告无效除法和除以零。
pub fn BinaryChecked::div(Self, Self) -> Self
在处理完操作数错误(先左后右)之后,结果为 Binary 的 DivChecked::div_checked 在操作数较大精度下的结果:对 、 或信号 NaN 操作数返回 DomainError,对有限非零被除数除以零除数()返回 DivisionByZero 错误,其余情况返回 IEEE 754-2019 结果:静默 NaN 操作数(包括 NaN)得到静默 NaN, 得到 ,否则得到舍入后的商。
BinaryChecked::min_num, BinaryChecked::max_num
这些方法用 Binary::min_num / Binary::max_num 取较小或较大的操作数,二者会忽略 NaN 操作数而取另一个,并在两个操作数比较相等时返回接收者(因此 min(-0, +0) 为 ,min(+0, -0) 为 )。
pub fn BinaryChecked::min_num(Self, Self) -> Self
pub fn BinaryChecked::max_num(Self, Self) -> Self
BinaryChecked::clamp
clamp(min~, max~) 将值限制在一个区间内。
pub fn BinaryChecked::clamp(Self, min~ : Self, max~ : Self) -> Self
错误按 self、min、max 的顺序选取;之后 Binary::clamp_checked 在某个边界为 NaN 或 min > max 时返回 DomainError,否则返回截断后的值。
///|
test "arithmetic keeps the first error" {
let one = @binary_checked.BinaryChecked::from_int(1)
let zero = @binary_checked.BinaryChecked::from_int(0)
inspect(show(one + one * one), content="1p1")
inspect(show(one / zero), content="error: div: division by zero")
let left = @binary_checked.BinaryChecked::err(
@lf_arith.ArithmeticError::unsupported("left"),
)
inspect(show(left + one / zero), content="error: left")
inspect(show(one / zero + left), content="error: div: division by zero")
let clamped = @binary_checked.BinaryChecked::from_int(5).clamp(
min=zero,
max=@binary_checked.BinaryChecked::from_int(3),
)
inspect(show(clamped), content="3p0")
let reversed = one.clamp(min=@binary_checked.BinaryChecked::from_int(3), max=zero)
inspect(show(reversed), content="error: min must not exceed max")
}
上下文式算术
BinaryChecked::add_ctx, BinaryChecked::sub_ctx, BinaryChecked::mul_ctx, BinaryChecked::div_ctx
这些方法在显式的 BinaryContext 下执行 Binary::*_ctx 运算,只保留其值。
pub fn BinaryChecked::add_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::sub_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::mul_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::div_ctx(Self, Self, @binary.BinaryContext) -> Self
结果具有上下文的精度、舍入方向和指数范围,并按 IEEE 处理上溢、下溢和次正规数。该步的标志与 add、div 和初等函数方法中的映射一致:invalid operation 为 DomainError,divideByZero 为 DivisionByZero;上溢、下溢和 inexact 被丢弃。因此除数为零的 div_ctx 是 DivisionByZero,、、 和 是 DomainError,与不带上下文时完全一致;静默 NaN 操作数仍然是成功的 NaN。完整的表见设计页。需要原始默认结果和标志时,请使用 Binary::div_ctx 及其同类方法。
///|
test "context arithmetic rounds to the context and maps the flags" {
let binary32 = @binary.BinaryContext::binary32()
let tenth = @binary_checked.BinaryChecked::from_double(0.1)
let zero = @binary_checked.BinaryChecked::from_int(0)
inspect(show(tenth.add_ctx(zero, binary32)), content="13421773p-27")
let one = @binary_checked.BinaryChecked::from_int(1)
inspect(show(one.div_ctx(zero, binary32)), content="error: div: division by zero")
inspect(show(one.div(zero)), content="error: div: division by zero")
inspect(show(zero.div_ctx(zero, binary32)), content="error: div: invalid operation")
}
幂与方根
BinaryChecked::sqrt, BinaryChecked::sqrt_ctx
sqrt 以操作数的精度求平方根;sqrt_ctx 在上下文下求平方根。
pub fn BinaryChecked::sqrt(Self) -> Self
pub fn BinaryChecked::sqrt_ctx(Self, @binary.BinaryContext) -> Self
sqrt 即 Binary::sqrt,对负的非零参数(包括 )以及信号 NaN 返回 DomainError;,任一符号的静默 NaN 得到一个成功的 NaN。sqrt_ctx 在上下文下行为相同:负参数是 DomainError。
BinaryChecked::pow_nat, BinaryChecked::pown, BinaryChecked::pown_ctx
这些方法将值提升到整数次幂。
pub fn BinaryChecked::pow_nat(Self, UInt) -> Self
pub fn BinaryChecked::pown(Self, Int) -> Self
pub fn BinaryChecked::pown_ctx(Self, Int, @binary.BinaryContext) -> Self
pow_nat(n) 调用 Luna-Flow/arithmetic 的 trait 方法 PowNatChecked::pow_nat_checked,参数为 ArithmeticContext::new(x.precision()),binary 将其映射为该精度下的二进制上下文(BinaryContext::new(precision=p)),采用就近偶数舍入。pown 对零底数与负指数返回 DivisionByZero 错误,对信号 NaN 底数(包括零指数,pow_nat 同样如此)返回 DomainError,其余情形在操作数精度下返回舍入后的幂。_ctx 形式以同样方式映射标志:零底数与负指数是 DivisionByZero 错误,信号 NaN 底数是 DomainError。
BinaryChecked::rootn, BinaryChecked::rootn_ctx
rootn(n) 用 Binary::try_rootn_ctx 计算实数 次方根。
pub fn BinaryChecked::rootn(Self, Int) -> Self
pub fn BinaryChecked::rootn_ctx(Self, Int, @binary.BinaryContext) -> Self
rootn 使用操作数精度下的无界上下文;rootn_ctx 使用给定的上下文。二者在次数为 (对任意操作数,安静 NaN 与 也包括在内,且绝不是 DivisionByZero:IEEE 754-2019 表 9.1,“n = 0: invalid operation”)、对负数(包括 )开偶次方根以及遇到信号 NaN 时报告 DomainError,对 的 报告 DivisionByZero 错误(IEEE 754-2019 §9.2.1),并且可能报告 CertificationFailure。负数的奇次方根为负:rootn(-8, 3) 为 。完整的特殊值表见 Binary::rootn。
BinaryChecked::pow, BinaryChecked::pow_ctx
pow(y) 用 Binary::try_pow_ctx 计算二进制指数下的 。
pub fn BinaryChecked::pow(Self, Self) -> Self
pub fn BinaryChecked::pow_ctx(Self, Self, @binary.BinaryContext) -> Self
pow 使用较大操作数精度下的无界上下文。操作数的错误优先(先底数,后指数);运算本身在底数为负的有限数且指数非整数时(例如 )以及遇到信号 NaN 操作数时报告 DomainError,在零底数配有限负指数时(,IEEE 754-2019 §9.2.1)报告 DivisionByZero 错误,并且可能报告 CertificationFailure。底数为 、负底数配任意大小的整数指数以及无穷指数,都得到 Binary::pow 中列出的 IEEE 754 值,例如 和 。
BinaryChecked::rsqrt, BinaryChecked::rsqrt_ctx
,委托给 Binary::try_rsqrt_ctx。
pub fn BinaryChecked::rsqrt(Self) -> Self
pub fn BinaryChecked::rsqrt_ctx(Self, @binary.BinaryContext) -> Self
错误:负操作数(含 )和信号 NaN 给出 DomainError; 给出 DivisionByZero 错误。
BinaryChecked::compound, BinaryChecked::compound_ctx
,委托给 Binary::try_compound_ctx。
pub fn BinaryChecked::compound(Self, Int) -> Self
pub fn BinaryChecked::compound_ctx(Self, Int, @binary.BinaryContext) -> Self
错误:小于 (含 )和信号 NaN 给出 DomainError; 时 给出 DivisionByZero 错误。
BinaryChecked::powr, BinaryChecked::powr_ctx
时的 ,委托给 Binary::try_powr_ctx。
pub fn BinaryChecked::powr(Self, Self) -> Self
pub fn BinaryChecked::powr_ctx(Self, Self, @binary.BinaryContext) -> Self
先报告操作数自身的错误(先底数,后指数)。操作本身对负底数、信号 NaN 以及无效组合 、、 报告 DomainError,对有限 的 报告 DivisionByZero 错误。
BinaryChecked::asinpi, BinaryChecked::asinpi_ctx
,委托给 Binary::try_asinpi_ctx。
pub fn BinaryChecked::asinpi(Self) -> Self
pub fn BinaryChecked::asinpi_ctx(Self, @binary.BinaryContext) -> Self
错误:(含 )和信号 NaN 给出 DomainError。
BinaryChecked::acospi, BinaryChecked::acospi_ctx
,委托给 Binary::try_acospi_ctx。
pub fn BinaryChecked::acospi(Self) -> Self
pub fn BinaryChecked::acospi_ctx(Self, @binary.BinaryContext) -> Self
错误:同 asinpi。
BinaryChecked::atanpi, BinaryChecked::atanpi_ctx
,委托给 Binary::try_atanpi_ctx。
pub fn BinaryChecked::atanpi(Self) -> Self
pub fn BinaryChecked::atanpi_ctx(Self, @binary.BinaryContext) -> Self
错误:仅信号 NaN 给出 DomainError; 给出 。
BinaryChecked::atan2pi, BinaryChecked::atan2pi_ctx
点 以半圈为单位的角度,委托给 Binary::try_atan2pi_ctx。
pub fn BinaryChecked::atan2pi(Self, Self) -> Self
pub fn BinaryChecked::atan2pi_ctx(Self, Self, @binary.BinaryContext) -> Self
错误按纵坐标、横坐标、运算本身的顺序处理。atan2pi 使用操作数较大精度下的 BinaryContext::new(precision=p)。零和无穷操作数给出不触发异常的精确常量;信号 NaN 操作数是 DomainError。
BinaryChecked::hypot, BinaryChecked::hypot_ctx
hypot(y) 用 Binary::try_hypot_ctx 计算 ,中间过程不会上溢。
pub fn BinaryChecked::hypot(Self, Self) -> Self
pub fn BinaryChecked::hypot_ctx(Self, Self, @binary.BinaryContext) -> Self
hypot 使用较大操作数精度下的无界上下文。信号 NaN 操作数为 DomainError;hypot 不触发其他异常(上溢和下溢是值)。
///|
test "powers and roots" {
let r = fn(n : Int) { @binary_checked.BinaryChecked::from_int(n) }
inspect(show(@binary_checked.BinaryChecked::from_int(81, precision=48).sqrt()), content="9p0")
inspect(show(r(-4).sqrt()), content="error: sqrt: invalid operation")
inspect(show(r(3).pow_nat(10)), content="59049p0")
inspect(show(r(0).pown(-1)), content="error: pown: division by zero")
inspect(show(r(-8).rootn(3)), content="-1p1")
inspect(show(r(8).rootn(0)), content="error: rootn: invalid operation")
inspect(show(r(3).hypot(r(4))), content="5p0")
}
初等函数
每个一元初等函数 name 都有两个包装方法。name() 是 Binary::try_name_ctx 在 BinaryContext::new(precision=x.precision()) 下的 bind,即以就近舍入(偶数优先)舍入到操作数的精度,且无指数限制;name_ctx(ctx) 则改用给定的上下文。成功值是 binary 的正确舍入值。该步骤的标志按与 div 相同的方式映射:无效运算异常(信号 NaN 操作数,或超出 IEEE 754-2019 表 9.1 定义域的参数)为 DomainError,除以零(极点,§9.2)为 DivisionByZero 错误,上溢、下溢和不精确则被丢弃。除下文列出的错误外,当 binary 无法在其细化预算内认证舍入时,每个函数都可能报告 CertificationFailure(参见 binary 初等函数),其 CertificationFailureDetail 指明函数名。其他错误的消息也指明函数名:name: invalid operation 和 name: division by zero,这样在一连串步骤中,消息就能说明是哪一步失败。静默 NaN 操作数得到一个成功的 NaN。
BinaryChecked::exp, BinaryChecked::exp_ctx
,委托给 Binary::try_exp_ctx。
pub fn BinaryChecked::exp(Self) -> Self
pub fn BinaryChecked::exp_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::expm1, BinaryChecked::expm1_ctx
,委托给 Binary::try_expm1_ctx。
pub fn BinaryChecked::expm1(Self) -> Self
pub fn BinaryChecked::expm1_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::exp2m1, BinaryChecked::exp2m1_ctx
,委托给 Binary::try_exp2m1_ctx。
pub fn BinaryChecked::exp2m1(Self) -> Self
pub fn BinaryChecked::exp2m1_ctx(Self, @binary.BinaryContext) -> Self
错误:信号 NaN 是 DomainError; 给出 。
BinaryChecked::exp10m1, BinaryChecked::exp10m1_ctx
,委托给 Binary::try_exp10m1_ctx。
pub fn BinaryChecked::exp10m1(Self) -> Self
pub fn BinaryChecked::exp10m1_ctx(Self, @binary.BinaryContext) -> Self
错误:同 exp2m1。
BinaryChecked::exp2, BinaryChecked::exp2_ctx
,委托给 Binary::try_exp2_ctx。
pub fn BinaryChecked::exp2(Self) -> Self
pub fn BinaryChecked::exp2_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::exp10, BinaryChecked::exp10_ctx
,委托给 Binary::try_exp10_ctx。
pub fn BinaryChecked::exp10(Self) -> Self
pub fn BinaryChecked::exp10_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::ln, BinaryChecked::ln_ctx
,委托给 Binary::try_ln_ctx。
pub fn BinaryChecked::ln(Self) -> Self
pub fn BinaryChecked::ln_ctx(Self, @binary.BinaryContext) -> Self
错误:负参数(包括 )为 DomainError; 为 DivisionByZero 错误。
BinaryChecked::log1p, BinaryChecked::log1p_ctx
,委托给 Binary::try_log1p_ctx。
pub fn BinaryChecked::log1p(Self) -> Self
pub fn BinaryChecked::log1p_ctx(Self, @binary.BinaryContext) -> Self
错误:小于 时(包括 )为 DomainError; 为 DivisionByZero 错误。
BinaryChecked::log2p1, BinaryChecked::log2p1_ctx
,委托给 Binary::try_log2p1_ctx。
pub fn BinaryChecked::log2p1(Self) -> Self
pub fn BinaryChecked::log2p1_ctx(Self, @binary.BinaryContext) -> Self
错误:低于 (含 )以及信号 NaN 为 DomainError; 为 DivisionByZero 错误。
BinaryChecked::log10p1, BinaryChecked::log10p1_ctx
,委托给 Binary::try_log10p1_ctx。
pub fn BinaryChecked::log10p1(Self) -> Self
pub fn BinaryChecked::log10p1_ctx(Self, @binary.BinaryContext) -> Self
错误:同 log2p1。
BinaryChecked::log2, BinaryChecked::log2_ctx
,委托给 Binary::try_log2_ctx。
pub fn BinaryChecked::log2(Self) -> Self
pub fn BinaryChecked::log2_ctx(Self, @binary.BinaryContext) -> Self
错误:负参数(包括 )为 DomainError; 为 DivisionByZero 错误。
BinaryChecked::log10, BinaryChecked::log10_ctx
,委托给 Binary::try_log10_ctx。
pub fn BinaryChecked::log10(Self) -> Self
pub fn BinaryChecked::log10_ctx(Self, @binary.BinaryContext) -> Self
错误:负参数(包括 )为 DomainError; 为 DivisionByZero 错误。
BinaryChecked::sin, BinaryChecked::sin_ctx
,委托给 Binary::try_sin_ctx。
pub fn BinaryChecked::sin(Self) -> Self
pub fn BinaryChecked::sin_ctx(Self, @binary.BinaryContext) -> Self
错误:无穷参数为 DomainError。
BinaryChecked::cos, BinaryChecked::cos_ctx
,委托给 Binary::try_cos_ctx。
pub fn BinaryChecked::cos(Self) -> Self
pub fn BinaryChecked::cos_ctx(Self, @binary.BinaryContext) -> Self
错误:无穷参数为 DomainError。
BinaryChecked::tan, BinaryChecked::tan_ctx
,委托给 Binary::try_tan_ctx。
pub fn BinaryChecked::tan(Self) -> Self
pub fn BinaryChecked::tan_ctx(Self, @binary.BinaryContext) -> Self
错误:无穷参数为 DomainError。
BinaryChecked::sinpi, BinaryChecked::sinpi_ctx
,委托给 Binary::try_sinpi_ctx。
pub fn BinaryChecked::sinpi(Self) -> Self
pub fn BinaryChecked::sinpi_ctx(Self, @binary.BinaryContext) -> Self
错误:无穷参数为 DomainError。
BinaryChecked::cospi, BinaryChecked::cospi_ctx
,委托给 Binary::try_cospi_ctx。
pub fn BinaryChecked::cospi(Self) -> Self
pub fn BinaryChecked::cospi_ctx(Self, @binary.BinaryContext) -> Self
错误:无穷参数为 DomainError。
BinaryChecked::tanpi, BinaryChecked::tanpi_ctx
,委托给 Binary::try_tanpi_ctx。
pub fn BinaryChecked::tanpi(Self) -> Self
pub fn BinaryChecked::tanpi_ctx(Self, @binary.BinaryContext) -> Self
错误:无穷参数为 DomainError;在半整数处(§9.2.1 的极点)为 DivisionByZero 错误。
BinaryChecked::asin, BinaryChecked::asin_ctx
,委托给 Binary::try_asin_ctx。
pub fn BinaryChecked::asin(Self) -> Self
pub fn BinaryChecked::asin_ctx(Self, @binary.BinaryContext) -> Self
错误:在 之外(包括无穷)为 DomainError。
BinaryChecked::acos, BinaryChecked::acos_ctx
,委托给 Binary::try_acos_ctx。
pub fn BinaryChecked::acos(Self) -> Self
pub fn BinaryChecked::acos_ctx(Self, @binary.BinaryContext) -> Self
错误:在 之外(包括无穷)为 DomainError。
BinaryChecked::atan, BinaryChecked::atan_ctx
,委托给 Binary::try_atan_ctx。
pub fn BinaryChecked::atan(Self) -> Self
pub fn BinaryChecked::atan_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::sinh, BinaryChecked::sinh_ctx
,委托给 Binary::try_sinh_ctx。
pub fn BinaryChecked::sinh(Self) -> Self
pub fn BinaryChecked::sinh_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::cosh, BinaryChecked::cosh_ctx
,委托给 Binary::try_cosh_ctx。
pub fn BinaryChecked::cosh(Self) -> Self
pub fn BinaryChecked::cosh_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::tanh, BinaryChecked::tanh_ctx
,委托给 Binary::try_tanh_ctx。
pub fn BinaryChecked::tanh(Self) -> Self
pub fn BinaryChecked::tanh_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::asinh, BinaryChecked::asinh_ctx
,委托给 Binary::try_asinh_ctx。
pub fn BinaryChecked::asinh(Self) -> Self
pub fn BinaryChecked::asinh_ctx(Self, @binary.BinaryContext) -> Self
BinaryChecked::acosh, BinaryChecked::acosh_ctx
,委托给 Binary::try_acosh_ctx。
pub fn BinaryChecked::acosh(Self) -> Self
pub fn BinaryChecked::acosh_ctx(Self, @binary.BinaryContext) -> Self
错误:小于 时为 DomainError。
BinaryChecked::atanh, BinaryChecked::atanh_ctx
,委托给 Binary::try_atanh_ctx。
pub fn BinaryChecked::atanh(Self) -> Self
pub fn BinaryChecked::atanh_ctx(Self, @binary.BinaryContext) -> Self
错误: 时为 DomainError; 为 DivisionByZero 错误。
BinaryChecked::atan2, BinaryChecked::atan2_ctx
点 的辐角,委托给 Binary::try_atan2_ctx。
pub fn BinaryChecked::atan2(Self, Self) -> Self
pub fn BinaryChecked::atan2_ctx(Self, Self, @binary.BinaryContext) -> Self
错误按纵坐标、横坐标、运算的顺序取用。atan2 使用较大操作数精度下的 BinaryContext::new(precision=p)。带符号零遵循 IEEE 754: 的 atan2 为 , 的则为 。信号 NaN 操作数为 DomainError;atan2 不触发其他异常。
///|
test "elementary functions" {
let r = fn(n : Int) { @binary_checked.BinaryChecked::from_int(n) }
inspect(show(r(2).ln()), content="6243314768165359p-53")
inspect(
show(r(2).ln_ctx(@binary.BinaryContext::new(precision=24))),
content="1453635p-21",
)
inspect(show(r(0).ln()), content="error: ln: division by zero")
inspect(show(r(-4).ln()), content="error: ln: invalid operation")
inspect(show(r(2).asin()), content="error: asin: invalid operation")
inspect(show(r(0).exp().ln()), content="0")
}
trait 实现
BinaryChecked 的 Add、Sub、Mul、Div 和 Neg
运算符 +、-、*、/ 和一元 - 调用 add、sub、mul、div 和 neg。
pub impl Add for BinaryChecked
pub impl Sub for BinaryChecked
pub impl Mul for BinaryChecked
pub impl Div for BinaryChecked
pub impl Neg for BinaryChecked
因此,包装值上的 / 会报告除以零,而普通 Binary 值上的 / 返回无穷。
已弃用
完整公共接口
以下快照是该包完整的生成接口。
// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating/binary_checked"
import {
"Luna-Flow/arithmetic",
"Luna-Flow/floating/binary",
}
// Values
// Errors
// Types and methods
pub struct BinaryChecked {
// private fields
}
pub fn BinaryChecked::abs(Self) -> Self
pub fn BinaryChecked::acos(Self) -> Self
pub fn BinaryChecked::acos_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::acosh(Self) -> Self
pub fn BinaryChecked::acosh_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::acospi(Self) -> Self
pub fn BinaryChecked::acospi_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::add(Self, Self) -> Self
pub fn BinaryChecked::add_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::asin(Self) -> Self
pub fn BinaryChecked::asin_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::asinh(Self) -> Self
pub fn BinaryChecked::asinh_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::asinpi(Self) -> Self
pub fn BinaryChecked::asinpi_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::atan(Self) -> Self
pub fn BinaryChecked::atan2(Self, Self) -> Self
pub fn BinaryChecked::atan2_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::atan2pi(Self, Self) -> Self
pub fn BinaryChecked::atan2pi_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::atan_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::atanh(Self) -> Self
pub fn BinaryChecked::atanh_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::atanpi(Self) -> Self
pub fn BinaryChecked::atanpi_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::bind(Self, (@binary.Binary) -> Self) -> Self
pub fn BinaryChecked::clamp(Self, min~ : Self, max~ : Self) -> Self
pub fn BinaryChecked::compound(Self, Int32) -> Self
pub fn BinaryChecked::compound_ctx(Self, Int32, @binary.BinaryContext) -> Self
pub fn BinaryChecked::cos(Self) -> Self
pub fn BinaryChecked::cos_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::cosh(Self) -> Self
pub fn BinaryChecked::cosh_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::cospi(Self) -> Self
pub fn BinaryChecked::cospi_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::div(Self, Self) -> Self
pub fn BinaryChecked::div_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::err(@arithmetic.ArithmeticError) -> Self
pub fn BinaryChecked::error(Self) -> @arithmetic.ArithmeticError?
pub fn BinaryChecked::exp(Self) -> Self
pub fn BinaryChecked::exp10(Self) -> Self
pub fn BinaryChecked::exp10_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::exp10m1(Self) -> Self
pub fn BinaryChecked::exp10m1_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::exp2(Self) -> Self
pub fn BinaryChecked::exp2_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::exp2m1(Self) -> Self
pub fn BinaryChecked::exp2m1_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::exp_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::expm1(Self) -> Self
pub fn BinaryChecked::expm1_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::from_coefficient(@binary.BinCoeff, precision? : Int32, negative? : Bool) -> Self
pub fn BinaryChecked::from_double(Double, precision? : Int32) -> Self
pub fn BinaryChecked::from_float(Float, precision? : Int32) -> Self
pub fn BinaryChecked::from_int(Int32, precision? : Int32) -> Self
pub fn BinaryChecked::from_result(Result[@binary.Binary, @arithmetic.ArithmeticError]) -> Self
pub fn BinaryChecked::hypot(Self, Self) -> Self
pub fn BinaryChecked::hypot_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::is_err(Self) -> Bool
pub fn BinaryChecked::is_ok(Self) -> Bool
pub fn BinaryChecked::ln(Self) -> Self
pub fn BinaryChecked::ln_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::log10(Self) -> Self
pub fn BinaryChecked::log10_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::log10p1(Self) -> Self
pub fn BinaryChecked::log10p1_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::log1p(Self) -> Self
pub fn BinaryChecked::log1p_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::log2(Self) -> Self
pub fn BinaryChecked::log2_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::log2p1(Self) -> Self
pub fn BinaryChecked::log2p1_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::map(Self, (@binary.Binary) -> @binary.Binary) -> Self
pub fn BinaryChecked::max_num(Self, Self) -> Self
pub fn BinaryChecked::min_num(Self, Self) -> Self
pub fn BinaryChecked::mul(Self, Self) -> Self
pub fn BinaryChecked::mul_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::neg(Self) -> Self
pub fn BinaryChecked::normalized(Self) -> Self
pub fn BinaryChecked::ok(@binary.Binary) -> Self
pub fn BinaryChecked::pow(Self, Self) -> Self
pub fn BinaryChecked::pow_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::pow_nat(Self, UInt32) -> Self
pub fn BinaryChecked::pown(Self, Int32) -> Self
pub fn BinaryChecked::pown_ctx(Self, Int32, @binary.BinaryContext) -> Self
pub fn BinaryChecked::powr(Self, Self) -> Self
pub fn BinaryChecked::powr_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::result(Self) -> Result[@binary.Binary, @arithmetic.ArithmeticError]
pub fn BinaryChecked::rootn(Self, Int32) -> Self
pub fn BinaryChecked::rootn_ctx(Self, Int32, @binary.BinaryContext) -> Self
pub fn BinaryChecked::rsqrt(Self) -> Self
pub fn BinaryChecked::rsqrt_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::sin(Self) -> Self
pub fn BinaryChecked::sin_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::sinh(Self) -> Self
pub fn BinaryChecked::sinh_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::sinpi(Self) -> Self
pub fn BinaryChecked::sinpi_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::sqrt(Self) -> Self
pub fn BinaryChecked::sqrt_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::sub(Self, Self) -> Self
pub fn BinaryChecked::sub_ctx(Self, Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::tan(Self) -> Self
pub fn BinaryChecked::tan_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::tanh(Self) -> Self
pub fn BinaryChecked::tanh_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::tanpi(Self) -> Self
pub fn BinaryChecked::tanpi_ctx(Self, @binary.BinaryContext) -> Self
pub fn BinaryChecked::ulp(Self) -> Self
pub fn BinaryChecked::with_precision(Self, Int32, @binary.BinaryRoundingMode) -> Self
pub impl Add for BinaryChecked
pub impl Div for BinaryChecked
pub impl Mul for BinaryChecked
pub impl Neg for BinaryChecked
pub impl Sub for BinaryChecked
// Type aliases
// Traits