def 教程
本教程的目标是编写能同时适用于 floating 所有数值类型的代码:通过 Floating trait 检视值,用泛型谓词检验其类别,把 IEEE 比较结果读作 PartialOrder,并复用 def 重新导出的算术上下文与错误类型。def 本身不做任何算术;算术由具体的包(binary、decimal、decimal_gda)完成。区间(interval)不是 Floating。该 trait 背后的定律见设计页面。
| 我想要 | 使用 |
|---|---|
| 查询任意值的类别、符号或精度 | Floating |
| 以泛型方式检测 NaN、无穷或零 | is_nan, is_infinite, is_finite, is_zero |
| 把值舍入到其自身基数下更少的位数 | Floating::with_precision |
| 读取 IEEE 比较的结果 | PartialOrder |
| 按打印形式比较十进制同值类 | Floating::normalized |
不导入 arithmetic 即可处理 checked 错误 | ArithmeticError |
快速入门
添加模块,并在你所用的表示之外再导入 def:
moon add Luna-Flow/floating@0.11.0
import {
"Luna-Flow/floating/def",
"Luna-Flow/floating/binary",
}
最小的实用程序询问一个值是什么:
///|
test "quick start: observe a value" {
let x = @binary.Binary::from_double(-2.5)
inspect(@def.is_finite(x), content="true")
inspect(@def.Floating::signum(x) == @def.Sign::Negative, content="true")
inspect(@def.Floating::precision(x), content="53")
}
日常任务
为所有表示编写同一个函数
以 F : @def.Floating 为约束的函数可以接受二进制、IEEE 十进制和 GDA 十进制值。请以限定形式 @def.Floating::classify(x) 调用 trait 方法,因为 MoonBit 不再把 trait 方法变成类型参数的点方法。
///|
fn[F : @def.Floating] summary(x : F) -> String {
if @def.is_nan(x) {
return "nan"
}
let sign = match @def.Floating::signum(x) {
Negative => "negative"
Zero => "zero"
Positive => "positive"
}
let kind = if @def.is_infinite(x) { "infinite" } else { "finite" }
"\{kind} \{sign} at precision \{@def.Floating::precision(x)}"
}
///|
test "one summary for four representations" {
inspect(
summary(@binary.Binary::from_int(7)),
content="finite positive at precision 53",
)
inspect(
summary(@decimal.Decimal::from_string("-0.00").unwrap()),
content="finite zero at precision 34",
)
inspect(
summary(@decimal_gda.Decimal::from_string("-Infinity").unwrap()),
content="infinite negative at precision 34",
)
}
泛型地改变精度
with_precision 舍入到所要求的有效数字位数,位数以值自身的基数计:二进制为位,十进制为十进制数字,就近舍入、偶数优先。trait 方法不带舍入模式;具体类型有自己的、带舍入模式的 with_precision 方法:
///|
fn[F : @def.Floating] three_digits(x : F) -> F {
@def.Floating::with_precision(x, 3)
}
///|
test "round to three significant digits of the radix" {
let d = three_digits(@decimal.Decimal::from_string("3.14159").unwrap())
inspect(d.to_string(), content="3.14")
let b = three_digits(@binary.Binary::from_int(11))
inspect(b.to_string(), content="3p2")
}
十一的二进制是 1011;三位有效位把它舍入为 1100,to_string 将其打印为 。
读取 IEEE 比较结果
遵循 IEEE 754 的比较返回一个 PartialOrder,其第四个值 Unordered 表示有 NaN 操作数:
///|
fn relation(a : @binary.Binary, b : @binary.Binary) -> String {
match a.compare_quiet(b).0 {
Less => "less"
Equal => "equal"
Greater => "greater"
Unordered => "unordered"
}
}
///|
test "four-way comparison" {
let one = @binary.Binary::from_int(1)
let two = @binary.Binary::from_int(2)
inspect(relation(one, two), content="less")
inspect(relation(@binary.Binary::nan(), @binary.Binary::nan()), content="unordered")
inspect(
relation(@binary.Binary::from_double(-0.0), @binary.Binary::zero()),
content="equal",
)
}
比较表示之前先规范化
值相同的十进制数,其存储的指数可能不同(1.5 和 1.500 属于同一个同值类)。normalized 选取规范成员,从而使打印形式一致:
///|
test "normalize a decimal cohort" {
let a = @decimal.Decimal::from_string("1.500").unwrap()
let b = @decimal.Decimal::from_string("1.5").unwrap()
inspect(a.to_string(), content="1.500")
inspect(
@def.Floating::normalized(a).to_string() ==
@def.Floating::normalized(b).to_string(),
content="true",
)
}
深入了解
使用重新导出的算术类型
@def.ArithmeticContext、@def.ArithmeticError、@def.RoundingMode 以及其他别名都是 Luna-Flow/arithmetic 的类型。只需要这套词汇的代码可以导入 def 而不是 arithmetic:
///|
fn describe_error(e : @def.ArithmeticError) -> String {
if e.is_division_by_zero() {
"division by zero: " + e.message
} else if e.is_domain_error() {
"domain error: " + e.message
} else {
"other: " + e.message
}
}
///|
test "handle a checked result through def" {
let result = @lf_arith.DivChecked::div_checked(
@binary.Binary::from_int(1),
@binary.Binary::zero(),
@lf_arith.ArithmeticContext::new(53),
)
match result {
Ok(_) => fail("expected an error")
Err(e) => inspect(describe_error(e), content="division by zero: division by zero")
}
}
常见陷阱
Sign::Zero并不意味着“值为零”。标量类型对 NaN 也返回它。请先检验@def.is_nan。Interval不是Floating:对区间请使用is_empty、is_common_interval或contains。Sign无法区分 和 。当符号位重要时,请使用具体的包(Binary::is_negative_zero、Decimal::is_sign_minus)。PartialOrder不是可以用来排序的序:Unordered破坏了三分律。排序请使用具体包提供的全序(Binary::total_order_compare、Decimal::total_order_compare)。with_precision不报告标志,也忽略上下文的指数界限(Binary仍会在其约为 的实现范围处上溢)。当舍入必须可观测时,请使用具体包的*_ctx运算。with_precision(x, 0)不是错误:精度会被钳制为 1。
后续步骤
def设计:trait 定律以及PartialOrder的序理论。defAPI:每个条目及其签名。semantic教程:精确比较不同表示的值。- 本仓库的共享词汇见数值语义指南。