decimal_gda API

用途

decimal_gda 实现了 M. F. Cowlishaw 的《通用十进制算术规范》1.70(GDA 1.70)。它分为三部分:

  • Decimal 由符号、任意长度的十进制系数和十进制指数构成。
  • GdaContext 携带精度、舍入、指数限制、钳制、扩展算术与子集算术之间的切换、标志以及启用的陷阱。
  • 每个 GDA 运算都是返回 GdaOutcome 的纯函数:它给出已定义的结果、下一个上下文,以及该运算引发的条件。

教程展示该包的用法;设计页推导此处所述的规则。

该包不依赖 IEEE 754 包 decimal。它只公开 GDA 层(Decimal、GdaContext、Gda* 类型以及以 GDA 运算命名的函数),以及面向 Luna-Flow/arithmetic 和 Luna-Flow/luna-generic trait 的适配器。这些函数所基于的引擎不保留任何标志,是包内私有的。

全文中,有限值记作 (−1)s⋅c⋅10q(-1)^s \cdot c \cdot 10^{q},其中系数 c≥0c \ge 0,指数为 qq。其调整后指数为 q^=q+digits⁡(c)−1\hat q = q + \operatorname{digits}(c) - 1,即其首位数字的指数。对于精度为 pp 的上下文,Etiny=Emin⁡−p+1E_{\mathrm{tiny}} = E_{\min} - p + 1 是结果可以具有的最小指数;在钳制时,Etop=Emax⁡−p+1E_{\mathrm{top}} = E_{\max} - p + 1 是最大指数。

导入

将该包添加到你的 moon.pkg 中:

import {
  "Luna-Flow/floating/decimal_gda",
}

本页示例通过别名 @decimal_gda. 调用该包,并通过 @lf_arith.(对应 Luna-Flow/arithmetic)调用 trait 适配器。若干示例定义了局部辅助函数 d(s),它用 Decimal::from_string 解析字面量并保留其指数。

Decimal 值

Decimal

Decimal 是不可变的 GDA 数:有限数或特殊值(无穷、静默 NaN 或信号 NaN)。

pub struct Decimal {
  // private fields
} derive(@debug.Debug)

有限数保留其指数,所以 2.50 与 2.5 是同一*同值类(cohort)*的不同成员:它们在数值上比较相等,但在 compare_total、same_quantum 和打印输出上不同。零带符号。NaN 带有符号和非负整数载荷。载荷为 0 表示不带诊断信息的 NaN,打印为 NaN;GDA 1.70,第 11 页(Special values)把 NaN 带有的载荷描述为正整数,第 18 和 19 页给出的字符串形式是 NaN 后跟可选的、没有前导零的数字。

每个值都存储一个精度,即值的精度(precision())。它是产生该值的上下文或构造函数的精度。GDA 运算忽略值的精度,改用上下文的精度。

系数以持久的十进制肢数组存储,因此值可以自由共享。

Decimal::zero, Decimal::negative_zero, Decimal::one

返回 +0+0、−0-0 和 11,指数均为 0。

pub fn Decimal::zero(precision? : Int32) -> Self
pub fn Decimal::negative_zero(precision? : Int32) -> Self
pub fn Decimal::one(precision? : Int32) -> Self

precision(默认 34)是值的精度:系数可以有的位数,范围从 1 到 999,999,999(上下文精度的实现限制)。超出该范围的精度会中止,消息指明函数和参数;没有任何构造函数会把它替换为 1。所有接受 precision 的构造函数都做同样的检查。

Decimal::inf, Decimal::nan, Decimal::quiet_nan, Decimal::signaling_nan

构建特殊值。

pub fn Decimal::inf(negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::nan(precision? : Int32) -> Self
pub fn Decimal::quiet_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::signaling_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int32) -> Self
调用结果
inf()+∞+\infty
inf(negative=true)−∞-\infty
nan()载荷为 0 的正静默 NaN,表示不带诊断信息的 NaN

载荷至多有 precision - 1 位,因为 GDA 1.70 第 11 页(Special values)说明载荷不大于系数所能表示的值少一位。负载荷,或对给定的 precision 而言过宽的载荷,会中止;精度为 1 时只有载荷 0。

消耗信号 NaN 的算术运算引发 InvalidOperation,并将其替换为符号和载荷相同的静默 NaN。

gda_value_e_max, gda_value_e_tiny

gda_value_e_max 和 gda_value_e_tiny 是有限 Decimal 所能持有的指数的限制。

pub let gda_value_e_max : Int32
pub let gda_value_e_tiny : Int32
常量值限制对象
gda_value_e_max231−2=2,147,483,6462^{31}-2 = 2{,}147{,}483{,}646调整后指数
gda_value_e_tiny−231+3=−2,147,483,645-2^{31}+3 = -2{,}147{,}483{,}645最低位数字的指数;它是普通运算符的工作上下文的 EtinyE_{\mathrm{tiny}},与精度无关

有限值(包括零,零有一位数字)只有在 q≥q \ge gda_value_e_tiny 且 q^≤\hat q \le gda_value_e_max 时才会被存储,其中 qq 是其指数,cc 是其系数,q^=q+digits⁡(c)−1\hat q = q + \operatorname{digits}(c) - 1 是其调整后指数。

构造函数 make、try_make 和 from_string 拒绝限制之外的值。运算通过舍入、下溢或上溢使其产生的值保持在限制之内,所以你能得到的每个 Decimal 都满足该条件。这是对每个已存储的有限值 x + 0 == x 和 x * 1 == x 成立的不变式(见普通运算符)。

限制之外的字符串在需要舍入后的值时用上下文读取:to_number 按上下文舍入、上溢和下溢。

Decimal::make, Decimal::try_make

make(c, e, p) 返回 c⋅10qc \cdot 10^{q},其中 qq 为 e,作为系数为 p 位的值,方式与 GDA 1.70 第 21 页(to-number)的 to-number 转换相同。

pub fn Decimal::make(@bigint.BigInt, Int32, Int32, mode? : GdaRoundingMode) -> Self
pub fn Decimal::try_make(@bigint.BigInt, Int32, Int32, mode? : GdaRoundingMode) -> Result[Self, @arithmetic.ArithmeticError]

符号取自 c 的符号。当 c 至多有 p 位时保留指数。更长的系数按 mode(默认 HalfEven,上下文的八种舍入算法之一)一次性舍入到 p 位,且舍入后的系数不被约简。

调用结果
make(1200, 0, 34)1200
make(1200, -2, 34)12.00
make(0, 5, 5)0E+5
make(1000000, 0, 3)1.00E+6

要去掉尾随零,请用 Decimal::normalized 或 Decimal::trim。不报告任何标志。

舍入之后,值必须位于已存储值的指数的实现限制之内(gda_value_e_max、gda_value_e_tiny)。指数低于 gda_value_e_tiny,或调整后指数高于 gda_value_e_max(包括舍入进位到下一个 10 的幂),都绝不会被零或无穷替换。

条件maketry_make
指数或调整后指数在限制之外中止,消息指明两个限制和该指数返回域错误一类的 Err,消息相同
精度超出 1..=999999999中止返回域错误一类的 Err

由整数和二进制值构造的构造函数

将整数和二进制值转换为十进制。

pub fn Decimal::from_int(Int32, precision? : Int32) -> Self
pub fn Decimal::from_bigint(@bigint.BigInt, precision? : Int32) -> Self
pub fn Decimal::from_double(Double, precision? : Int32) -> Self
pub fn Decimal::from_float(Float, precision? : Int32) -> Self
pub fn Decimal::from_binary(@binary.Binary, precision~ : Int32, mode? : GdaRoundingMode) -> Self
参数结果
至多 precision 位的整数指数 0
精确的二进制小数最接近 0 的指数(尾随零只去除到指数 0 为止)
更长或不精确的值舍入到恰好 precision 位,半值取偶,除非 mode(对 from_binary)另有指定

precision 默认为 34 位;对 from_binary 而言它是必需的,并按十进制位数计,而不是参数的位数。

二进制浮点数 m⋅2km \cdot 2^{k}(k<0k < 0)首先被精确写成 m5−k⋅10km 5^{-k} \cdot 10^{k},因此只要结果能容纳于 precision 位,转换就是精确的。例如,from_double(0.1) 是最接近 0.10.1 的 binary64 的精确值,按半值取偶舍入到 34 位的结果。

参数结果
NaN静默 NaN(from_double 保留符号)
无穷符号相同的无穷
零符号相同的零(from_binary 返回 +0+0)
调用结果
from_int(100)100(指数 0)
from_int(99999, precision=3)1.00E+5
from_double(0.5)0.5

Decimal::to_binary

to_binary 将值舍入为具有 precision 位有效二进制位的 Binary。

pub fn Decimal::to_binary(Self, precision~ : Int32, mode? : @binary.BinaryRoundingMode) -> @binary.Binary

precision 是必需的,按位数计,范围从 1 到 @binary.binary_precision_max(超出该范围则调用中止)。mode 是结果的基数的舍入方向属性(IEEE 754-2019 4.3.1 和 4.3.2),默认 RoundTiesToEven。每一种舍入,包括 ties-to-away 在内,都把该十进制数从其精确值一次性舍入到 precision 位,中间不做任何舍入。

用 RoundTowardNegative 和 RoundTowardPositive 得到的两个结果包含该十进制值;初等函数以这种方式构建其经过认证的输入区间。零映射为 +0+0,NaN 映射为二进制 NaN。

Decimal::from_string

from_string 在没有 GDA 上下文的情况下读取 GDA 数值字符串。

pub fn Decimal::from_string(String, precision? : Int32) -> Result[Self, @arithmetic.ArithmeticError]

可接受的语法是 GDA 数值字符串语法:可选符号、带可选小数点的数字、可选的指数 E±n;或者 Infinity、Inf、NaN、sNaN 之一(不区分大小写),后面可跟可选的十进制 NaN 载荷。

指数被精确保留,因此 from_string("2.50") 的指数为 −2-2。有效数字多于 precision(默认 34)位的字面量按半值取偶舍入,并去掉其尾随零。

输入结果
格式错误的字面量Err(parse_error)
超出 1..=999999999 的精度域错误一类的 Err
载荷宽度超过 precision - 1 位数字的 NaN(GDA 1.70,第 11 页(Special values))Err(parse_error)
值(包括零)在舍入之后的指数低于 gda_value_e_tiny,或调整后指数高于 gda_value_e_max 的字面量同时指出两个限制和该指数的域错误一类的 Err

不适用任何上下文指数范围,只有对所存储值的指数的实现限制(gda_value_e_max、gda_value_e_tiny)。限制之外的字面量不是格式错误的字面量,也不会被折叠为无穷或零:"1e3000000000" 和 "1e-3000000000" 都是错误。需要舍入后的值,或必须应用上下文的指数限制、标志或陷阱时,请使用包函数 to_number。

Decimal::to_string, Decimal::output

以 GDA 科学记数法打印值,特殊值使用小写。

pub fn Decimal::to_string(Self) -> String
pub fn Decimal::output(Self, &Logger) -> Unit
pub impl Show for Decimal
值打印为
满足 q≤0q \le 0 且 q^≥−6\hat q \ge -6 的有限数不带指数:0.000123、7.50
其他任何有限数一位数字,其余数字写在小数点之后,再加 E±q^\hat q:1.23E+7、1E-7、0E+2
无穷inf 或 -inf
NaNnan、snan 或 -nan,载荷不为零时后面跟载荷

需要 GDA 的写法 Infinity、NaN 和 sNaN 时,请使用 Decimal::to_sci_string。

Decimal::to_sci_string, Decimal::to_eng_string

用 GDA 的 to-scientific-string 或 to-engineering-string 打印一个值。两种转换都不使用上下文。

pub fn Decimal::to_sci_string(Self) -> String
pub fn Decimal::to_eng_string(Self) -> String

工程记数法使用 3 的倍数作为指数(123E+5 打印为 12.3E+6)。特殊值打印为 Infinity、-Infinity、NaN、sNaN,并带载荷。若要先在某个上下文下转换数值字符串,请先调用 to_number,再打印其值。

观察值

Decimal::classify, Decimal::signum, Decimal::precision

报告值的类别、数值符号和精度。

pub fn Decimal::classify(Self) -> @arithmetic.FpClass
pub fn Decimal::signum(Self) -> @def.Sign
pub fn Decimal::precision(Self) -> Int32

classify 返回 Finite、Infinity 或 NaN。precision 返回值的精度,其定义见 Decimal。

值signum 返回
任一符号的零Zero
NaNZero
其他任何值Positive 或 Negative

要读取符号位,请使用 is_signed。

Decimal::coefficient, Decimal::exponent10

返回值所存储的表示 (c,q)(c, q)。

pub fn Decimal::coefficient(Self) -> @bigint.BigInt
pub fn Decimal::exponent10(Self) -> Int32

coefficient 返回 c≥0c \ge 0:对 NaN 为载荷,对无穷为 0。exponent10 返回 qq,对特殊值为 0。GDA 1.70 没有名为 quantum 的操作:它的操作是 quantize(GDA 1.70,第 36 页(quantize))和 same-quantum(GDA 1.70,第 48 页(same-quantum))。

类别与符号谓词

判断值的类别和符号。

pub fn Decimal::is_finite(Self) -> Bool
pub fn Decimal::is_infinite(Self) -> Bool
pub fn Decimal::is_nan(Self) -> Bool
pub fn Decimal::is_zero(Self) -> Bool
pub fn Decimal::is_signed(Self) -> Bool
pub fn Decimal::is_negative_zero(Self) -> Bool

is_signed 读取符号位,因此对 −0-0、−∞-\infty 和负 NaN 为真。is_zero 对任意指数的有限零为真。

Decimal::is_qnan, Decimal::is_snan

判断值是静默 NaN 还是信号 NaN。

pub fn Decimal::is_qnan(Self) -> Bool
pub fn Decimal::is_snan(Self) -> Bool

Decimal::is_canonical

is_canonical 总是返回 true,因为每个 Decimal 值都是规范的。只有交换编码可能是非规范的:参见 GdaInterchange::is_canonical。

pub fn Decimal::is_canonical(Self) -> Bool

Decimal::nan_payload

nan_payload 返回 NaN 的载荷,对其他任何值返回 0。

pub fn Decimal::nan_payload(Self) -> @bigint.BigInt

IEEE 754-2019 9.7 的载荷运算(NaN 载荷运算:getPayload、setPayload、setPayloadSignaling)在 decimal 中,不在这里。

符号、同值类与精度变换

符号运算

这里的每个运算都只改变值的符号位。它们都不舍入,也不发出信号,即使对信号 NaN 也是如此。

pub fn Decimal::neg(Self) -> Self
pub fn Decimal::abs(Self) -> Self
pub fn Decimal::copy(Self) -> Self
pub fn Decimal::copy_abs(Self) -> Self
pub fn Decimal::copy_negate(Self) -> Self
pub fn Decimal::copy_sign(Self, Self) -> Self
操作结果
neg, copy_negate符号翻转后的值
abs, copy_abs符号清除后的值
copy该值本身
copy_sign(x, y)带有 y 的符号位的 x

copy、copy_abs、copy_negate 和 copy_sign 是 GDA 的 copy 运算(GDA 1.70,第 43 页和第 44 页(Miscellaneous operations));neg 和 abs 的行为分别同 copy_negate 和 copy_abs。会舍入的版本是包函数 minus、plus、abs。

Decimal::normalized, Decimal::trim, Decimal::with_precision

normalized 和 trim 在同值类内移动值。with_precision 把值舍入到新的精度。

pub fn Decimal::normalized(Self) -> Self
pub fn Decimal::trim(Self) -> Self
pub fn Decimal::with_precision(Self, Int32, GdaRoundingMode) -> Self

normalized 按半值取偶舍入到值的精度(见 Decimal),并约简结果,去除所有尾随零:7.50 变为 7.5,1200 变为 1.2E+3。

trim 只去除小数部分的尾随零,且从不使指数变为正:7.50 变为 7.5,1200 保持 1200。零变为 0,指数为 0。

with_precision(p, mode) 以舍入算法 mode 舍入到 p 位,去除尾随零,并设置值的精度。特殊值只获得新的精度。

这三者都不报告标志。

舍入产生的进位若越过所存储值的指数的实现限制的上端,则遵循 Overflow 规则(GDA 1.70,第 53 页(Overflow)),并以该限制作为最大调整后指数,因此结果绝不会是超过该限制的有限值。例如,9999E+2147483643 舍入到 2 位时,在远离零舍入和就近舍入的舍入算法下是 inf;在向零舍入的舍入算法下是 9.9E+2147483646,即该宽度下最大的有限值。

Decimal::identical

identical 判断两个值是否为同一个数据。

pub fn Decimal::identical(Self, Self) -> Bool

当两个值的类别、符号、系数和指数都相同、精度相同,且对 NaN 而言载荷和种类(静默或信号)也相同时,结果为真。它等价于 compare_total 为 0 且精度相等;GDA 定义的 compare_total 不含精度。

2.5 与 2.50 按 == 相等,但不 identical。0 与 -0 相等,但不 identical。载荷相同的两个 NaN 是 identical 的,尽管 == 和 compare 对所有 NaN 一视同仁。

值的精度属于该关系的一部分,因为普通运算符按操作数中较大的精度舍入。精度为 3 的 1 与精度为 34 的 1 相等,且 compare_total 为 0,但除以 3 后分别得到 0.333 和 0.3333333333333333333333333333333333。两个 identical 的值在每个函数中给出相同的结果。

Decimal::same_quantum

same_quantum 检测两个值是否具有相同的指数。

pub fn Decimal::same_quantum(Self, Self) -> Bool
操作数结果
两个有限值当它们的指数相等时为真
两个无穷真
两个 NaN真
其他任何一对假

GDA 上下文

GdaContext

GdaContext 是不可变的 GDA 上下文。它包含算术策略、上下文的标志和已启用的陷阱。

pub struct GdaContext {
  // private fields
}

策略由精度 p≥1p \ge 1、舍入(一个 GdaRoundingMode)、Emin⁡≤Emax⁡E_{\min} \le E_{\max}、clamp 以及 extended 组成。标志是一个 GdaFlags 值,运算只会使它增大;陷阱是一个 GdaTrapSet。

标志和陷阱都不影响运算的数值结果。如陷阱的选择所述,它们只决定下一个上下文,以及结果是 Completed 还是 Trapped。

GdaContext::new, GdaContext::try_new

构造标志集为空的上下文。参数无效时 new 中止;try_new 接受相同的参数并返回 Result。

pub fn GdaContext::new(precision? : Int32, rounding? : GdaRoundingMode, e_min? : Int32, e_max? : Int32, clamp? : Bool, extended? : Bool, traps? : GdaTrapSet) -> Self
pub fn GdaContext::try_new(precision? : Int32, rounding? : GdaRoundingMode, e_min? : Int32, e_max? : Int32, clamp? : Bool, extended? : Bool, traps? : GdaTrapSet) -> Result[Self, @arithmetic.ArithmeticError]

默认值为:

参数默认值
precision34
roundingHalfEven
e_min-999_999_999
e_max999_999_999
clampfalse
extendedtrue
traps无

参数必须满足下列规则。try_new 返回 Err(domain_error) 并指明无效的参数,new 则以同样的消息中止。

  • precision 在 1..=999_999_999 内,e_max 在 0..=999_999_999 内,e_min 在 -999_999_999..=0 内。这些实现限制取自 decNumber 的 decContext.h;它们使 EtinyE_{\mathrm{tiny}}、EtopE_{\mathrm{top}} 和指数运算保持在 Int 范围内。GDA 1.70 本身没有规定任何限制。
  • e_min 是 -e_max、1 - e_max(IEEE 754 的规则)或 -e_max - 1。GDA 1.70 第 10 页(Abstract representation of numbers)要求 Emin⁡E_{\min} 与 Emax⁡E_{\max} 的绝对值至多相差一,使结果范围保持平衡。像 e_min=-50, e_max=60 这样的范围会被拒绝。同一段还要求,存在最大系数长度时,Elimit 须大于其五倍。上下文没有最大系数长度,且官方 decTest 语料中有 913 行在 e_max 小于精度四倍的情况下运行,因此库不强制执行这一要求。

在 GDA 文本中,clamp 和 extended 不是上下文的参数。它们是库保留的 decNumber 扩展,官方 decTest 文件两者都用到。

  • clamp=true 是 Clamped 条件的折叠(GDA 1.70,第 51 页(Exceptional conditions))。它把指数限制在 Emax⁡−p+1E_{\max} - p + 1 以内,与交换格式一样。
  • extended=false 选择附录 A 的 GDA 子集算术(GDA 1.70,第 55 页(Appendix A, The X3.274 subset);脚注 43:上下文的某个参数“可能”选择它)。在子集算术中,每个接受上下文的操作都先把长于 pp 位的操作数舍入(GDA 1.70,第 56 页(Appendix A, The X3.274 subset)),不精确时引发 LostDigits;因此精度 9 时 reduce(1234567891) 引发 LostDigits,next_toward(1234567891, 0) 是 1.23456788E+9。逻辑操作、全序和分类不舍入其操作数。特殊值不能被解析,divide 和 power 去掉小数部分的尾随零,fma 无效。
    • 子集上下文中每个接受上下文的运算,其结果都遵守三条规则(GDA 1.70,第 55 页(Appendix A, The X3.274 subset)):零是指数为 0 的 +0+0;低于 Emin⁡E_{\min} 的结果(次正规结果)是指数为 0 的 +0+0,并带有标志 Underflow、Subnormal、Inexact 和 Rounded(该运算此前已引发该标志的,仍带 Clamped,如 exp);指数为正的结果在其位数不超过 pp 时得到指数 0。因此 1200 的 reduce 是 1200,scaleb(12, 2) 是 1200,remainder(1.4E+3, 5E+2) 是 400,round_to_integral_value(1.2E+3) 是 1200,sqrt(1E+2) 是 10,power(-0, 3) 是 +0+0,next_toward(0, 1) 是带 Underflow 的 +0+0。
    • 复制运算(copy、copy_abs、copy_negate、copy_sign)不接受上下文,不在这些规则之内:copy_negate(0) 是 −0-0。
    • quantize 和 rescale 保留零的指数和正指数(quantize 是第 55 页的例外,rescale 是它的旧名):quantize(-0, 1E+2) 是 0E+2。to_number 保留正指数,并把零读作指数为 0 的 +0+0。
    • 附录 A 没有列出 reduce、rescale、scaleb、shift、rotate、remainder、remainder_near、那两个取整运算或 next_toward;这些规则像适用于每个运算一样适用于它们。decNumber 3.68 对其中一些给出不同的结果(见符合性)。

默认上下文

返回默认上下文(GDA 1.70,第 16 页(Default contexts)):basic_default,以及扩展默认上下文 decimal32、decimal64 和 decimal128。

pub fn GdaContext::basic_default() -> Self
pub fn GdaContext::decimal32(rounding? : GdaRoundingMode) -> Self
pub fn GdaContext::decimal64(rounding? : GdaRoundingMode) -> Self
pub fn GdaContext::decimal128(rounding? : GdaRoundingMode) -> Self
上下文pp舍入Emin⁡E_{\min}Emax⁡E_{\max}clamp扩展陷阱
basic_default9HalfUp−999 999 999-999\,999\,999999 999 999999\,999\,999否否DivisionByZero、InvalidOperation、Overflow、Underflow、Clamped
decimal327HalfEven−95-959696是是无
decimal6416HalfEven−383-383384384是是无
decimal12834HalfEven−6143-614361446144是是无

basic_default 是规范的基本默认上下文:精度 9、半值向上舍入,除 inexact、rounded 和 subnormal 外的所有陷阱都启用(GDA 1.70,第 16 页(Default contexts))。库还让 lost-digits 陷阱保持关闭,因为附录 A 指出在默认上下文中 Lost digits 条件应设为 0(GDA 1.70,第 56 页(Appendix A, The X3.274 subset))。库为该上下文设置 extended=false(附录 A 的 X3.274 子集),而 GDA 1.70 中基本默认上下文的定义并未设置它。

decimal32、decimal64 和 decimal128 是 IEEE 754 格式的扩展默认上下文:半值取偶舍入,不设陷阱。给出 rounding 时,它替换舍入算法。

GdaContext::new() 不是 basic_default():它的精度为 34,半值取偶,不设陷阱。这些值只创建一次并共享。

GdaContext::with_precision, GdaContext::with_rounding, GdaContext::with_exponent_range

返回改变了一项设置的上下文副本。

pub fn GdaContext::with_precision(Self, Int32) -> Self
pub fn GdaContext::with_rounding(Self, GdaRoundingMode) -> Self
pub fn GdaContext::with_exponent_range(Self, e_min~ : Int32, e_max~ : Int32) -> Self

结果与 new 一样受到检查:精度或指数范围若不符合 new 的规则则中止。标志、陷阱、clamp、extended 和细化限制都会保留。

上下文的参数

读取上下文的算术策略。

pub fn GdaContext::precision(Self) -> Int32
pub fn GdaContext::rounding(Self) -> GdaRoundingMode
pub fn GdaContext::e_min(Self) -> Int32
pub fn GdaContext::e_max(Self) -> Int32
pub fn GdaContext::clamp(Self) -> Bool
pub fn GdaContext::extended(Self) -> Bool
pub fn GdaContext::radix(Self) -> Int32

radix 总是返回 10。

GdaContext::flags, GdaContext::traps

读取上下文的标志和已启用的陷阱。

pub fn GdaContext::flags(Self) -> GdaFlags
pub fn GdaContext::traps(Self) -> GdaTrapSet

GdaContext::refinement_limit, GdaContext::with_refinement_limit

读取和设置认证初等函数 exp、ln、log10 和 power 的细化步数。

pub fn GdaContext::refinement_limit(Self) -> Int32?
pub fn GdaContext::with_refinement_limit(Self, Int32?) -> Self

exp、ln、log10 和 power 默认至多对结果的包含细化 Some(12) 次。极少数非常接近舍入边界的参数可能需要更多:with_refinement_limit(Some(24)) 允许 24 步,None 取消步数限制,此时工作精度在某个资源上限处停止。小于 1 的限制按一步计。

达到限制时,普通函数返回带 invalid_operation 的 NaN,而 try_exp、try_ln、try_log10、try_power 返回 Err。

GdaContext::trap, GdaContext::with_traps, GdaContext::clear_flags, GdaContext::reset

返回陷阱或标志已更改的新上下文;接收者本身不被修改。

pub fn GdaContext::trap(Self, GdaSignal, enabled? : Bool) -> Self
pub fn GdaContext::with_traps(Self, GdaTrapSet) -> Self
pub fn GdaContext::clear_flags(Self) -> Self
pub fn GdaContext::reset(Self) -> Self
方法效果
trap(s)启用一个陷阱,或用 enabled=false 将其禁用
with_traps替换整个集合
clear_flags清空标志,保留陷阱
reset两者都清空

GdaRoundingMode

GdaRoundingMode 是上下文的舍入的类型。它的八个值是舍入算法(GDA 1.70,第 13 页和第 14 页(Abstract representation of context))。

pub(all) enum GdaRoundingMode {
  HalfEven
  HalfUp
  HalfDown
  Down
  Ceiling
  Floor
  Up
  ZeroFiveUp
}

当精确结果严格位于两个可表示的相邻值之间时,各舍入算法的选择如下。

舍入结果
Down更接近零的相邻值
Up远离零的那个邻值
Ceiling较大的邻值
Floor较小的邻值
HalfEven较近的邻值;恰好居中时取末位数字为偶数的那个
HalfUp较近的邻值;恰好居中时远离零
HalfDown较近的邻值;恰好居中时朝向零
ZeroFiveUp向零舍入,除非这样会使末位数字为 0 或 5,此时则远离零舍入

设计文档以公式给出每种算法。

信号、标志与陷阱

GdaSignal

GdaSignal 列出 GDA 1.70 的八种信号以及其附录 A 的 Lost digits 信号。每种信号在上下文的标志中有一个标志,并有一个陷阱使能。

pub(all) enum GdaSignal {
  Clamped
  DivisionByZero
  Inexact
  InvalidOperation
  LostDigits
  Overflow
  Rounded
  Subnormal
  Underflow
}

规范有八种信号(GDA 1.70,第 15 页(flags and trap-enablers)):Clamped、DivisionByZero、Inexact、InvalidOperation、Overflow、Rounded、Subnormal 和 Underflow。LostDigits 是第九种,仅在子集算术中引发(GDA 1.70,第 56 页(Appendix A, The X3.274 subset))。

规范称会发出 invalid-operation 的四个条件不是信号:转换语法错误、除法不可行、除法未定义和无效上下文(GDA 1.70,第 51 和 52 页(Exceptional conditions))。它们是 GdaFlags 的条件,引发其中任何一个都会引发信号 InvalidOperation;GdaFlags::contains 说明了具体方式。

GdaFlags

GdaFlags 是条件的集合。它既记录单个运算引发的条件,也记录上下文的标志。它包含库引发的十三种条件,即 Exceptional conditions 一节(GDA 1.70,第 51 页至第 53 页)中除 Insufficient storage 外的十二种条件,加上附录 A 的 Lost digits。为何把这些条件区分开,见设计文档。

pub struct GdaFlags {
  // private fields
} derive(Eq)

字段为私有。13 个条件中的每一个都用同名方法读取,例如 raised().inexact() 或 flags().division_by_zero()。唯一的公开构造函数是 new,因此集合只能通过 new 和 combine 构造。GdaTrapSet 的读取方式相同,九种信号各有一个方法。

GdaFlags::new, GdaFlags::contains, GdaFlags::combine

new 是空集,combine 是逐字段并集,contains 是对信号的成员检测,采用 GDA 的无效运算分组。访问器方法每个读取一个条件。

pub fn GdaFlags::new() -> Self
pub fn GdaFlags::contains(Self, GdaSignal) -> Bool
pub fn GdaFlags::combine(Self, Self) -> Self

combine 满足结合律、交换律和幂等律,并以 new() 为单位元。

contains(s) 读取 s 对应的字段,但 contains(InvalidOperation) 例外:只要 invalid_operation、conversion_syntax、division_impossible、division_undefined 或 invalid_context 中任一被设置,它即为真。要询问信号是否被引发,请使用 contains(InvalidOperation)。

访问器 invalid_operation() 只读取普通的无效运算条件,而这四个条件并非每一个都会引发普通条件。除法未定义或不可行会引发它;转换语法错误或无效上下文则不会。这与 decNumber 一致,decTest 的各行描述的正是它的状态字。

GdaTrapSet

GdaTrapSet 是已启用的陷阱使能的集合,九种信号各有一个。

pub struct GdaTrapSet {
  // private fields
} derive(Eq)

GdaTrapSet::new, GdaTrapSet::with_signal, GdaTrapSet::contains

构建并查询陷阱集。

pub fn GdaTrapSet::new() -> Self
pub fn GdaTrapSet::with_signal(Self, GdaSignal, enabled? : Bool) -> Self
pub fn GdaTrapSet::contains(Self, GdaSignal) -> Bool

new 不启用任何陷阱。with_signal(s) 启用一个陷阱,或用 enabled=false 将其禁用。contains(s) 读取 s 的陷阱使能。InvalidOperation 的陷阱对每一个发出该信号的条件都会触发,详见陷阱选择。

GdaOutcome

GdaOutcome[T] 是每个 GDA 运算的结果。

pub enum GdaOutcome[T] {
  Completed(T, GdaContext, GdaFlags)
  Trapped(GdaSignal, T, GdaContext, GdaFlags)
}

两个变体都携带 GDA 定义的结果、下一个上下文以及本次运算引发的条件。Trapped 还指明所触发的陷阱。

在包外,调用方可以对该枚举 match 并使用访问器,但只有 GDA 运算、GdaOutcome::completed 和 GdaOutcome::absorb 才能构造 outcome。GdaOutcome::resume_defined 把被陷阱拦截的 outcome 变成其定义结果。

GdaOutcome::value, GdaOutcome::next_context, GdaOutcome::raised

读取两个变体共有的字段,无需 match。

pub fn[T] GdaOutcome::value(Self[T]) -> T
pub fn[T] GdaOutcome::next_context(Self[T]) -> GdaContext
pub fn[T] GdaOutcome::raised(Self[T]) -> GdaFlags

value 是定义的结果,对 Trapped 亦然。next_context 是标志已更新的上下文。raised 只包含本次运算的条件。

GdaOutcome::completed, GdaOutcome::absorb

构造流水线所需的 outcome。

pub fn[T] GdaOutcome::completed(T, GdaContext) -> Self[T]
pub fn[T, U] GdaOutcome::absorb(Self[T], Self[U]) -> Self[T]

completed(v, c) 是什么都没有引发的运算的 outcome:v 和 c 不变,raised 为空。

a.absorb(b) 保留 a 的值、上下文(包括陷阱)和陷阱。b 引发的条件并入 raised 和下一个上下文的标志。在标志中,其中含四个细分的无效条件之一时,设置 invalid_operation。当 a 已完成而 b 陷入陷阱时,结果是带 b 的信号的 Trapped。a 的陷阱不会对 b 的条件进行检测。GdaDecimalChecked 用 absorb 处理二元运算的右操作数。

GdaOutcome::resume_defined

把 Trapped outcome 变为 Completed outcome;这是唯一的方式。

pub fn[T] GdaOutcome::resume_defined(Self[T]) -> Self[T]

Completed 原样返回。Trapped(s, v, C, R) 变为 Completed(v, C, none),其中 C 是该结果所携带的下一个上下文(不是陷阱选择的输入上下文),none 是空的 raised;这是 GDA 规定的“陷阱后以定义结果继续”的规则。GdaDecimalChecked::resume_defined 调用它。

陷阱选择

GDA 函数都以相同的三步结束。设 RR 为运算引发的条件,CC 为输入上下文。

  1. 若 RR 为空,结果为 Completed(v, C, none):返回同一个上下文。
  2. 否则,下一个上下文 C′C' 是标志为 C.flags∪RC.\mathrm{flags} \cup R 的 CC;只要 RR 包含四种细分无效条件之一,invalid_operation 标志也会被设置。
  3. 被捕获的信号是按以下顺序中第一个同时满足 raised.contains(s) 与 traps.contains(s) 的 ss:InvalidOperation、DivisionByZero、Overflow、Underflow、Subnormal、Inexact、Rounded、Clamped、LostDigits。若存在这样的信号,结果为 Trapped(s, v, C', R),否则为 Completed(v, C', R)。

由于 contains(InvalidOperation) 涵盖了各细分无效条件,唯一的 InvalidOperation 陷阱会捕获所有这些条件,并且无论由哪个条件引发,被捕获的信号都是 InvalidOperation。

///|
test "GDA context, flags and traps" {
  let ctx = @decimal_gda.GdaContext::decimal64().trap(InvalidOperation)
  let zero = @decimal_gda.Decimal::zero()
  let out = @decimal_gda.divide(zero, zero, ctx) // 0/0
  inspect(out.value(), content="nan")
  inspect(out.raised().division_undefined(), content="true")
  inspect(out.raised().contains(InvalidOperation), content="true")
  match out {
    @decimal_gda.GdaOutcome::Trapped(signal, _, _, _) =>
      inspect(signal == InvalidOperation, content="true")
    @decimal_gda.GdaOutcome::Completed(_, _, _) => fail("expected a trap")
  }
  inspect(out.next_context().flags().invalid_operation(), content="true")
  inspect(ctx.flags() == @decimal_gda.GdaFlags::new(), content="true")
  inspect(ctx.reset().traps() == @decimal_gda.GdaTrapSet::new(), content="true")
}

GDA 运算

本节中的每个函数都接受其操作数(to_number 接受字符串)和一个 GdaContext,并返回按陷阱选择规则构建的 GdaOutcome;四个 try_ 函数例外,它们把该结果放在 Result 中返回。信号 NaN 操作数引发 InvalidOperation 并产生对应的静默 NaN;静默 NaN 操作数直接传播,不引发任何条件(第一个 NaN 操作数优先)。在子集上下文(extended=false)中,长于精度的有限操作数会先被舍入(见 GdaContext::new)。除非另有说明,结果是精确结果经正确舍入所得;当结果精确时,其指数为该运算所列的理想指数。

to_number

to_number 按 GDA 的 to-number 转换把字符串转换为 Decimal。

pub fn to_number(String, GdaContext) -> GdaOutcome[Decimal]

字面量保留其指数,除非必须舍入到精度或钳制到指数范围。上溢与下溢依据文本的精确指数判定,无论其多大。

格式错误的文本得到带 ConversionSyntax 的静默 NaN。在子集上下文中,无穷与 NaN 也属于转换语法错误。

apply, plus, minus, abs

将单个操作数舍入到上下文:plus 即 0+x0 + x,minus 即 0−x0 - x,abs 即 ∣x∣|x|,apply 则是将值直接转换到上下文。

pub fn apply(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn plus(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn minus(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn abs(Decimal, GdaContext) -> GdaOutcome[Decimal]

理想指数是操作数的指数。

零结果的符号遵循 GDA 关于零和的规则。plus(-0) 和 minus(0) 在 Floor 下为 −0-0,在其他所有舍入算法下为 +0+0,而 plus(0)、minus(-0) 以及任一零的 abs 都是 +0+0。在子集上下文中,所有零结果都是 +0+0。

在扩展上下文中,apply 保留零的符号;它不会先舍入子集操作数。

add, subtract, multiply, divide, fma

执行基本算术运算;fma(a, b, c) 是只舍入一次的 a×b+ca \times b + c。

pub fn add(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn subtract(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn multiply(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn divide(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn fma(Decimal, Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

各运算的理想指数,其中 q1q_1 和 q2q_2 分别是第一个和第二个操作数的指数:

操作理想指数
add, subtractmin⁡(q1,q2)\min(q_1, q_2)
multiplyq1+q2q_1 + q_2
divideq1−q2q_1 - q_2(不精确的商具有完整的 pp 位系数)
fma对精确乘积和加数应用 add 的规则

精确的零和:两个操作数均为负时为 −0-0,均为正时为 +0+0,其他情况为 +0+0,在 Floor 下则为 −0-0。对 subtract,操作数是第一个操作数和取负后的第二个操作数;在子集上下文中,长于 pp 位的第二个操作数先按其自身符号舍入,再取负(#761),因此精度为 1、Floor 下的 subtract(0, 12) 为 -1E+1。因此 add(0, -0) 在 HalfEven 下为 0,在 Floor 下为 -0,而 add(0, 0) 在任何舍入算法下都是 0。

特殊情形:

操作数结果
∞−∞\infty - \infty, 0×∞0 \times \infty, ∞/∞\infty / \infty无效
x/0x / 0带 DivisionByZero 的 ±∞\pm\infty
0/00 / 0带 DivisionUndefined 的 NaN
x/∞x / \infty指数为 EtinyE_{\mathrm{tiny}} 且带 Clamped 的零

无效的结果引发 InvalidOperation;算作该信号的条件列于 GdaFlags::new。fma 在子集上下文中无效。

远小得多的加数会被吸收为低于和的每个舍入边界的粘滞单位,并且只有当结果的精确结果低于 10Emin⁡10^{E_{\min}} 时才会被舍入到次正规网格,因此这些运算都只舍入一次。精度为 7 时,add(1598618.5, 1E-20) 为 1598619。在 decimal32 中,multiply(3.000001E-45, 1.500001E-45) 为 4.500005E-90,divide(1, 1.9999999999998E+101) 为 1E-101。

divide_integer, remainder, remainder_near

做除法得到整数商。

pub fn divide_integer(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn remainder(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn remainder_near(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
函数返回理想指数
divide_integerQ=trunc⁡(x/y)Q = \operatorname{trunc}(x / y)0
remainderx−Qyx - Q y(符号取 xx 的符号)min⁡(qx,qy)\min(q_x, q_y)
remainder_nearx−nyx - n y,其中 nn 是将 x/yx / y 舍入到最接近的整数(平局时取偶数)min⁡(qx,qy)\min(q_x, q_y)

当 QQ(或 nn)需要多于 pp 位时,结果为带 DivisionImpossible 的 NaN。除数为零时给出 DivisionByZero(非零数的 divide_integer)或 DivisionUndefined(0/00 / 0)。除数为零或被除数为无穷时,两个余数都无效。有限被除数除以无穷除数时,余数就是被除数本身。

在扩展的钳制上下文中(见 GdaContext::new),零商保留其精确的符号,必要时被折叠到 Emax⁡−p+1E_{\max}-p+1,并引发 Clamped,而不引发 Rounded 或 Inexact。

quantize, rescale

将值舍入到指定的指数。

pub fn quantize(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn rescale(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

quantize(x, y) 返回指数为 y 的指数 qyq_y 的 xx 的值,以上下文的舍入进行舍入(引发 Rounded,丢失数字时还引发 Inexact)。rescale(x, n) 对指数 nn 做同样的事,其中 nn 必须是整数值。

在子集上下文中,位数多于 pp 的第二个操作数先被舍入到 pp 位(丢弃非零数字时引发 LostDigits),量子取舍入后的操作数:精度为 1 时,quantize(0, 12) 是 0E+1,rescale(0, 12) 是 0E+10。

在以下情况下结果无效:

  • 目标指数位于 [Etiny,Emax⁡][E_{\mathrm{tiny}}, E_{\max}] 之外(在子集上下文中为 [Emin⁡,Emax⁡][E_{\min}, E_{\max}]),
  • 结果的系数需要多于 pp 位,或
  • 恰有一个操作数为无穷。

两个无穷给出该无穷。

次正规结果会引发 Subnormal(丢失数字时还引发 Inexact 和 Rounded),但从不引发 Underflow:在 decimal32 中,quantize(1.55E-100, 1E-101) 为 1.6E-100,并带有 Subnormal、Inexact 和 Rounded。

round_to_integral_exact, round_to_integral_value

以上下文的舍入舍入到整数。

pub fn round_to_integral_exact(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn round_to_integral_value(Decimal, GdaContext) -> GdaOutcome[Decimal]

指数 ≥0\ge 0 的值本身已是整数,按原样返回,即使它长于 pp 位(clamp 上下文仍会把它的指数折叠下来)。

指数为负的值以 max⁡(p,its digit count)\max(p, \text{its digit count}) 的工作精度量化到指数 0,与 decNumber 相同,因此整数部分从不被舍入到 pp 位:精度为 3 时,12345.6 得到 12346。

丢弃数字时,round_to_integral_exact 会引发 Inexact 和 Rounded,而 round_to_integral_value 从不引发。在子集上下文中,操作数先被舍入到 pp 位,并引发 LostDigits。

///|
test "round_to_integral keeps long integral parts" {
  let d = (s : String) => @decimal_gda.Decimal::from_string(s).unwrap()
  let p3 = @decimal_gda.GdaContext::new(precision=3)
  let exact = @decimal_gda.round_to_integral_exact(d("12345.6"), p3)
  inspect(exact.value(), content="12346")
  inspect(exact.raised().inexact(), content="true")
  let value = @decimal_gda.round_to_integral_value(d("12345.6"), p3)
  inspect(value.raised().inexact(), content="false")
  inspect(@decimal_gda.round_to_integral_exact(d("12345"), p3).value(), content="12345")
}

exp、ln、log10 和 power 的实现限制

exp、ln、log10 以及非整数指数的 power 只接受满足 p≤999 999p \le 999\,999、Emax⁡≤999 999E_{\max} \le 999\,999 且 Emin⁡≥−999 999E_{\min} \ge -999\,999 的上下文。超出某项限制时,它们引发 InvalidContext;在默认指数范围下,它们返回 NaN 并引发 InvalidContext。

限制适用于
p≤999 999p \le 999\,999, Emax⁡≤999 999E_{\max} \le 999\,999, −Emin⁡≤999 999-E_{\min} \le 999\,999exp、ln、log10,以及非整数指数的 power
非零操作数至多有 999,999 位数字exp, ln, log10
非零操作数的调整后指数至多为 999,999,且至少为 −1 999 997-1\,999\,997exp、ln、log10,以及指数为非整数的 power(其底数,以及位于下限处的指数)

这是一项实现限制。GDA 1.70 的运算正文没有规定这样的界,该限制由它的 Invalid context 条件所允许,该条件适用于精度“超出底层具体表示的能力”的情形(GDA 1.70,第 52 页(Exceptional conditions))。数值 999,999 是 decNumber 的 DEC_MAX_MATH。原因见设计页面。

sqrt, exp, ln, log10

返回值的正确舍入的平方根、指数函数、自然对数和以 10 为底的对数。

pub fn sqrt(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn exp(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn ln(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn log10(Decimal, GdaContext) -> GdaOutcome[Decimal]

无论上下文的舍入如何,它们对结果都使用半偶舍入算法。在子集上下文中,长于 pp 位的操作数先按上下文的舍入算法舍入,而不是按半偶舍入(#762):精度为 1、Down 下的 sqrt(0.999) 为 0.9。每个未列为精确的有限结果,在 pp 位下都是不精确的。

函数操作数结果
sqrt完全平方数精确,理想指数为 ⌊q/2⌋\lfloor q/2 \rfloor,其中 qq 是操作数的指数
exp0011
ln1100
log1010 的幂 10k10^k整数 kk,若 kk 多于 pp 位则舍入
函数操作数结果
sqrt, ln, log10负数无效
ln, log1000−∞-\infty;在子集上下文中无效
exp−∞-\infty00
全部四个+∞+\infty+∞+\infty

无效结果会引发 InvalidOperation;计为该信号的条件列于 GdaFlags::new 之下。

exp、ln 和 log10 受实现限制约束。如果计算无法在细化限制内判定舍入,结果为带 InvalidOperation 的 NaN。

GDA 1.70 允许 exp、ln 和 log10 的不精确结果与正确舍入的结果相差至多一个末位单位(GDA 1.70,第 30 页(exp);第 31 页(ln, log10))。本库返回正确舍入的结果,在子集上下文中与在扩展算术中一样(见符合性页面)。

power

power(x, y) 返回 xyx^y。

pub fn power(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

对整数 yy,结果用二进制求幂并以 p+digits⁡(∣y∣)+2p + \operatorname{digits}(|y|) + 2 位工作位数计算(子集上下文中少一位),然后以上下文的舍入进行舍入。能容纳于 pp 位的精确幂会被精确返回,其指数为 y⋅qxy \cdot q_x。

对非整数 yy,xx 必须为正(负底数无效),且结果经认证是按上下文的舍入正确舍入的。y=0.5y = 0.5 的情形在上下文的舍入下按平方根计算。实现限制适用于非整数指数。对有限底数,若指数为非整数而底数的调整后指数大于 999,999 或小于 −1 999 997-1\,999\,997,或指数的调整后指数小于 −1 999 997-1\,999\,997,或指数为整数而其调整后指数大于 999,999,则给出带 InvalidOperation 的 NaN,而不是 InvalidContext。

特殊情形遵循 GDA:

用例结果
x0x^011;000^0 无效
±∞\pm\infty 或 ±0\pm 0 的幂其符号取决于整数指数的奇偶性
1y1^y,yy 不是整数或为无穷11,带 Inexact 和 Rounded
子集上下文中的 000^01
子集上下文中的 0−n0^{-n}无效,而不是 ∞\infty

GDA 把所有非整数次幂都视为不精确,即使实数 xyx^y 可以表示:精度为 5 时,power(4, 1.5) 在每种舍入算法下都是 8.0000,并带有 Inexact 和 Rounded。这类情形,即指数为既约分数 a/ba/b 且 xx 是完全 bb 次幂,在细化循环之前判定,因为它们落在定向舍入单元的边界上或中点上,循环无法认证。

GDA 1.70 允许不精确的 power 结果有至多一个末位单位的误差(GDA 1.70,第 35 页(power))。本库始终返回正确舍入的结果,因此 Down、Floor 和 ZeroFiveUp 给出 8.0000,而 libmpdec 给出 7.9999。

try_exp, try_ln, try_log10, try_power

返回 exp、ln、log10 或 power 的结果;当本库无法认证结果时返回 Err。

pub fn try_exp(Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]
pub fn try_ln(Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]
pub fn try_log10(Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]
pub fn try_power(Decimal, Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]

规范定义的每种情形都是与普通函数完全相同的 Ok 结果。这些情形包括定义域错误、信号 NaN,以及超出所支持指数范围的上下文,exp 及其同类函数将后者报告为 invalid_context(即实现限制)。Err 表示细化限制或资源上限使计算中止;此时普通函数返回带 invalid_operation 的 NaN。

reduce

将值舍入到上下文并去除其尾随零。

pub fn reduce(Decimal, GdaContext) -> GdaOutcome[Decimal]

零变为指数为 0 的 00,在扩展上下文中保留其符号。在钳制上下文中(参数 clamp,见 GdaContext::new),指数不会被提高到 Emax⁡−p+1E_{\max} - p + 1 以上。

scaleb, logb

scaleb 按十的幂缩放一个值,logb 返回其调整后指数。

pub fn scaleb(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn logb(Decimal, GdaContext) -> GdaOutcome[Decimal]

scaleb(x, n) 通过把 nn 加到指数上返回 x⋅10nx \cdot 10^n。操作数 nn 必须是指数为 0 且满足 ∣n∣≤2(Emax⁡+p)|n| \le 2(E_{\max} + p) 的整数,否则结果无效。随后结果经过共用的最终化:

用例结果示例
系数长于 pp 位舍入
上溢按舍入算法,为 ±∞\pm\infty 或最大有限数在 decimal32 中,Down 下的 scaleb(9E+96, 1) 是 9.999999E+96
零结果钳制到指数范围内decimal64 中的 scaleb(0E+300, 400) 是 0E+369

logb(x) 以整数形式返回 q^\hat q。logb(0) 是 −∞-\infty,并带有 DivisionByZero;logb(±∞) 是 +∞+\infty。

next_plus, next_minus, next_toward

返回与值相邻的可表示数。

pub fn next_plus(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn next_minus(Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn next_toward(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

next_plus 返回上下文中大于 xx 的最小可表示数,next_minus 返回比它小的最大可表示数。从零出发时它们步进到 ±10Etiny\pm 10^{E_{\mathrm{tiny}}},越过最大有限数时到达 ±∞\pm\infty。对有限结果,二者都不引发标志(GDA 1.70,第 34 页);在子集上下文中,低于正规范围的一步给出 +0+0(不允许次正规结果),同样不引发标志(#770)。

next_toward(x, y) 从 xx 朝 yy 步进;当二者比较相等时返回 xx(对零则取 yy 的符号)。当这一步离开正规范围时,引发 Overflow,或 Underflow 和 Subnormal,并同时引发 Inexact 和 Rounded(第 35 页:与加上一个无穷小量之和的标志相同);在子集上下文中,低于正规范围的一步给出带 Underflow、Subnormal、Inexact 和 Rounded 的 +0+0。

logical_and, logical_or, logical_xor, logical_invert

计算逻辑操作数的逐位与、或、异或和取反(GDA 1.70,第 41 页(Miscellaneous operations))。逻辑操作数是指数为 0 的非负整数,其各位数字都是 0 或 1。

pub fn logical_and(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn logical_or(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn logical_xor(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn logical_invert(Decimal, GdaContext) -> GdaOutcome[Decimal]

每个操作数都恰好按 pp 位读取:较短的操作数以前导零填充,较长的只使用其低 pp 位。logical_invert 对全部 pp 位取反。任何其他操作数都会使结果无效(即 InvalidOperation 条件)。

shift, rotate

在 pp 位的窗口内将 xx 的系数数字移动 nn 位(GDA 1.70,第 49 页(shift);第 47 页(rotate))。

pub fn shift(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn rotate(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

nn 必须是指数为 0 且满足 ∣n∣≤p|n| \le p 的整数,否则结果无效。正的 nn 将数字向左移动。shift 丢弃移出窗口的数字并以零填充;rotate 将它们循环移到另一端。指数与符号不变,无穷的 xx 原样返回。

compare, compare_signal, compare_total, compare_total_magnitude

按值或按 GDA 全序比较两个值(GDA 1.70,第 42 页(compare-total))。

pub fn compare(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn compare_signal(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn compare_total(Decimal, Decimal, GdaContext) -> GdaOutcome[Int32]
pub fn compare_total_magnitude(Decimal, Decimal, GdaContext) -> GdaOutcome[Int32]
函数结果某个操作数是 NaN
compare十进制数 −1-1、00 或 11,按值比较(−0=+0-0 = +0,2.50=2.52.50 = 2.5)静默 NaN;仅对信号 NaN 引发 InvalidOperation
compare_signal与 compare 相同静默 NaN;对任何 NaN 都引发 InvalidOperation

compare_total 按 GDA 全序返回 −1-1、00 或 11。该顺序先比较符号位。对正值有 finite<∞<sNaN<NaN\text{finite} < \infty < \text{sNaN} < \text{NaN},有限值先按值、再按指数排序(2.50 < 2.5),NaN 按载荷排序;对负值则顺序相反。compare_total_magnitude 对绝对值应用该全序:它就是两个绝对值的 compare_total(GDA 1.70,第 43 页),因此在子集上下文中,符号先于操作数的舍入被去掉(#763)。在扩展上下文中,全序比较从不引发标志。

子集上下文(extended=false)与对待其他所有操作数一样,先把操作数舍入到该精度(见 GdaContext::new;GDA 1.70,第 56 页(Appendix A, The X3.274 subset)),这会引发 Rounded 和 LostDigits。同名的 Decimal 方法 则按原样比较操作数。

max, min, max_magnitude, min_magnitude

按值或按绝对值返回两个操作数中较大或较小的一个(GDA 1.70,第 32 页(max))。

pub fn max(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn min(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn max_magnitude(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn min_magnitude(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
操作数结果
一个是静默 NaN,另一个操作数不是 NaN不是 NaN 的那个操作数
两个静默 NaN第一个
信号 NaN静默 NaN,并带有 InvalidOperation
比较相等的值,扩展上下文全序所选中的操作数(因此 max(2.5, 2.50) 是 2.5)
比较相等的值,子集上下文第一个操作数

随后所选的操作数像 plus 一样被舍入到上下文,但在扩展上下文中零保留其符号:对每种舍入算法,max(-0, -0) 都是 -0,而 plus(-0) 是 0,除非舍入为 Floor。

class_name, is_normal, is_subnormal

在上下文下对值进行分类(GDA 1.70,第 42 页(class);第 45 页(is-normal);第 46 页(is-subnormal))。它们从不引发条件,因此总是返回带输入上下文的 Completed。

pub fn class_name(Decimal, GdaContext) -> GdaOutcome[String]
pub fn is_normal(Decimal, GdaContext) -> GdaOutcome[Bool]
pub fn is_subnormal(Decimal, GdaContext) -> GdaOutcome[Bool]

它们需要上下文的指数下限 Emin⁡E_{\min},所以是以上下文为参数的函数。same_quantum 不使用上下文,是 Decimal 的方法。

///|
test "GDA operation sampler" {
  let ctx = @decimal_gda.GdaContext::decimal64()
  let d = (s : String) => @decimal_gda.Decimal::from_string(s).unwrap()
  inspect(@decimal_gda.divide_integer(d("17"), d("5"), ctx).value(), content="3")
  inspect(@decimal_gda.remainder(d("-17"), d("5"), ctx).value(), content="-2")
  inspect(@decimal_gda.remainder_near(d("17"), d("5"), ctx).value(), content="2")
  inspect(@decimal_gda.fma(d("1.5"), d("2"), d("0.25"), ctx).value(), content="3.25")
  inspect(@decimal_gda.round_to_integral_exact(d("2.5"), ctx).value(), content="2")
  inspect(@decimal_gda.rescale(d("1.2345"), d("-2"), ctx).value(), content="1.23")
  inspect(@decimal_gda.reduce(d("120.00"), ctx).value(), content="1.2E+2")
  inspect(@decimal_gda.scaleb(d("1.5"), d("3"), ctx).value(), content="1.5E+3")
  inspect(@decimal_gda.logb(d("0.00123"), ctx).value(), content="-3")
  inspect(@decimal_gda.next_plus(d("1"), ctx).value(), content="1.000000000000001")
  inspect(@decimal_gda.logical_xor(d("1100"), d("1010"), ctx).value(), content="110")
  inspect(@decimal_gda.shift(d("12345"), d("2"), ctx).value(), content="1234500")
  inspect(@decimal_gda.rotate(d("12345"), d("-1"), @decimal_gda.GdaContext::new(precision=5)).value(), content="51234")
  inspect(@decimal_gda.power(d("2"), d("-3"), ctx).value(), content="0.125")
  inspect(@decimal_gda.max(d("2.5"), d("2.50"), ctx).value(), content="2.5")
  inspect(@decimal_gda.class_name(d("-0"), ctx).value(), content="-Zero")
  let wide = @decimal_gda.GdaContext::new() // exponent range ±999,999,999
  inspect(@decimal_gda.exp(d("1"), wide).raised().invalid_context(), content="true")
}

值上的排序与普通算术

本节的方法和运算符不接受上下文。它们从不发出信号,也从不触发陷阱;当标志重要时,请使用 GDA 函数。

Decimal::compare, Decimal::compare_checked

compare 返回两个值的三路数值比较结果,对所有值都完全;compare_checked 拒绝 NaN。

pub fn Decimal::compare(Self, Self) -> Int32
pub fn Decimal::compare_checked(Self, Self) -> Result[Int32, @arithmetic.ArithmeticError]
pub impl Compare for Decimal
pub impl @arithmetic.CompareChecked for Decimal

compare 按数值对有限值和无穷排序,其中 −0=+0-0 = +0,并使每个 NaN 与其他所有 NaN 相等,且大于所有不是 NaN 的值,因此它是全预序,排序永不中止。当任一操作数为 NaN 时,compare_checked 返回 Err(unordered_comparison)。

比较运算符

实现 Eq 和 Compare 的运算符。

pub fn Decimal::equal(Self, Self) -> Bool
pub fn Decimal::not_equal(Self, Self) -> Bool
pub fn Decimal::op_lt(Self, Self) -> Bool
pub fn Decimal::op_le(Self, Self) -> Bool
pub fn Decimal::op_gt(Self, Self) -> Bool
pub fn Decimal::op_ge(Self, Self) -> Bool
pub impl Eq for Decimal

== 的比较方式如下:

操作数==
有限值数值相等(2.50=2.52.50 = 2.5,−0=+0-0 = +0)
无穷符号相等
两个 NaN真

<、<=、> 和 >= 遵循 compare。

Decimal::compare_total, Decimal::compare_total_magnitude

按 GDA 全序返回 −1-1、00 或 11。

pub fn Decimal::compare_total(Self, Self) -> Int32
pub fn Decimal::compare_total_magnitude(Self, Self) -> Int32

每个方法都与本包中同名的包函数 compare_total, compare_total_magnitude 一致,区别在于:包函数会对子集算术的操作数做舍入(GdaContext::new),而方法不会。

Decimal::clamp, Decimal::clamp_checked

在不舍入的情况下把值限制在 [min⁡,max⁡][\min, \max] 之内。GDA 的运算 min 和 max 是本包的自由函数 min 和 max,它们接受 GdaContext 并返回 GdaOutcome;没有同名的方法。

pub fn Decimal::clamp(Self, min~ : Self, max~ : Self) -> Self
pub fn Decimal::clamp_checked(Self, min~ : Self, max~ : Self) -> Result[Self, @arithmetic.ArithmeticError]

当值位于 [min⁡,max⁡][\min, \max] 之外时,clamp 返回 min 或 max,否则返回该值(NaN 值原样返回)。当某个界为 NaN 或 min > max 时它会中止;此时 clamp_checked 返回 Err(domain_error)。

Decimal::add, Decimal::sub, Decimal::mul, Decimal::div, Decimal::neg

在无上下文的情况下实现 +、-、*、/ 以及一元 -。

pub fn Decimal::add(Self, Self) -> Self
pub fn Decimal::sub(Self, Self) -> Self
pub fn Decimal::mul(Self, Self) -> Self
pub fn Decimal::div(Self, Self) -> Self
pub impl Add for Decimal
pub impl Sub for Decimal
pub impl Mul for Decimal
pub impl Div for Decimal
pub impl Neg for Decimal

+、-、* 和 / 在私有的工作上下文中运行自由函数 add、subtract、multiply 和 divide,并返回其值。该工作上下文的参数如下:

参数值
精度PP,两个操作数的精度中较大者
舍入半值取偶
算术扩展
clamp无
陷阱无
指数范围下面的范围

因此,结果是同值类中具有 GDA 所规定理想指数的成员(见 GDA 运算):

操作结果的指数示例
+, -两个操作数中较小的指数1.10 + 3.50 为 4.60
*两个指数之和
/两个指数之差,并且只去除精确商所允许去除的尾随零1.5 / 0.5 为 3,1.50 / 0.5 为 3.0

四种运算中任何一种的结果若多于 PP 位数字,就舍入到 PP 位(因此 * 的积会被舍入,而不是精确的)。

x + 0 == x 和 x * 1 == x 成立,是因为相等比较的是值,而不是同值类的成员;并且对每个已存储的有限值,也是因为下面的工作指数范围是已存储值的指数的实现限制。

GdaContext::new 无法构造该工作指数范围。它把上下文的 Emax⁡E_{\max} 限制为至多 999,999,999999{,}999{,}999,把 Emin⁡E_{\min} 限制为至少 −999,999,999-999{,}999{,}999(GDA 1.70,第 10 页(Abstract representation of numbers)允许设置上限 Elimit,但没有给出数值),而工作范围占满整个 Int 范围:对每个 PP,最大调整后指数为 231−2=2,147,483,6462^{31}-2 = 2{,}147{,}483{,}646,最小指数为 Etiny=−231+3=−2,147,483,645E_{\mathrm{tiny}} = -2^{31}+3 = -2{,}147{,}483{,}645,因此 EtinyE_{\mathrm{tiny}} 总是合法的 Int 指数。高于该范围的结果为带符号的无穷(1E+1500000000 * 1E+1500000000 为 inf);低于该范围的结果按半值取偶舍入算法舍入到 EtinyE_{\mathrm{tiny}},可能得到带符号的零。

特殊值遵循 IEEE 754-2019 第 6.2 条(含 NaN 的运算)、第 7.2 条(无效运算)和第 7.3 条(除以零)的默认结果,且不发出信号:

操作数结果
NaN 操作数静默 NaN
∞−∞\infty - \infty, 0×∞0 \times \infty, 0/00/0, ∞/∞\infty/\inftyNaN
x/0x/0,xx 非零且不是 NaN带符号的无穷
x/∞x/\infty带符号的零

需要其他精度、舍入、范围或信号时,请使用自由函数并传入 GdaContext。

一元 - 即 Decimal::neg:它翻转符号位,不使用上下文。上文的工作上下文、精度 PP、同值类规则和工作范围只适用于 +、-、* 和 /。

Decimal::sqrt

sqrt 是在普通运算符的工作上下文中,以该值的精度求平方根。

pub fn Decimal::sqrt(Self) -> Self

负操作数给出 NaN,与自由函数 sqrt 相同。带检查的形式是 sqrt_checked 和 DivChecked::div_checked。Decimal::div_checked 不存在:要得到无穷请用 1 / 0,要得到错误请用带陷阱的 GdaContext 调用 divide。

///|
test "context-free ordering and operators" {
  let d = (s : String) => @decimal_gda.Decimal::from_string(s).unwrap()
  inspect(d("2.50") == d("2.5"), content="true")
  inspect(d("2.50").compare_total(d("2.5")), content="-1")
  inspect(@decimal_gda.Decimal::nan() == @decimal_gda.Decimal::nan(), content="true")
  inspect(d("1").compare(@decimal_gda.Decimal::nan()), content="-1")
  inspect(d("1").compare_checked(@decimal_gda.Decimal::nan()) is Err(_), content="true")
  inspect(d("1.10") + d("2.20"), content="3.30")
  inspect(d("1.10") * d("2.20"), content="2.4200")
  inspect(d("1") / d("3"), content="0.3333333333333333333333333333333333")
  inspect(d("5").clamp(min=d("0"), max=d("3")), content="3")
}

交换编码

GdaInterchangeFormat

GdaInterchangeFormat 选择 IEEE 754 三种十进制交换格式之一:decimal32、decimal64 或 decimal128。

pub(all) enum GdaInterchangeFormat {
  Decimal32
  Decimal64
  Decimal128
} derive(Eq)

GdaInterchangeFormat::context

context 返回该格式的扩展默认 GdaContext:精度为 7、16 或 34,半值取偶,启用钳制。

pub fn GdaInterchangeFormat::context(Self) -> GdaContext

这些上下文即预设 GdaContext::decimal32()、decimal64() 和 decimal128()。

GdaInterchange

GdaInterchange 保存采用密集压缩十进制(DPD)编码的 decimal32、decimal64 或 decimal128 比特模式。

pub struct GdaInterchange {
  // private fields
}

GdaInterchange::format

format 返回该比特模式的格式。

pub fn GdaInterchange::format(Self) -> GdaInterchangeFormat

GdaInterchange::from_decimal, GdaInterchange::to_decimal

from_decimal 把 Decimal 编码为某种格式;to_decimal 对比特模式解码。

pub fn GdaInterchange::from_decimal(Decimal, GdaInterchangeFormat) -> (Self, GdaFlags)
pub fn GdaInterchange::to_decimal(Self) -> Decimal

from_decimal 将值舍入到该格式并对其编码;返回的 GdaFlags 报告舍入、上溢、下溢和钳制。to_decimal 精确解码,保留指数,且不引发任何标志;需要次正规条件时,请用 is_subnormal 检验结果。

NaN 以其符号、信号位和载荷编码。decimal32、decimal64 或 decimal128 比特模式的载荷分别有 6、15 或 33 位数字(尾随有效数字段,IEEE 754-2019 3.5.2)。更宽的载荷保留其低位数字,且不引发标志。IEEE 754-2019 6.2.3 要求,无法保留载荷的转换应返回一个带有某种语言自定诊断信息的静默 NaN,并未指定任何信号。

GdaInterchange::from_hex, GdaInterchange::to_hex

from_hex 从十六进制文本解析比特模式;to_hex 将其打印出来。

pub fn GdaInterchange::from_hex(String, GdaInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn GdaInterchange::to_hex(Self) -> String

文本为 #(输入时可选)后跟恰好 8、16 或 32 个十六进制数字;两侧的空格被忽略。其他任何输入都得到 Err(parse_error)。to_hex 打印 # 和大写数字。

GdaInterchange::canonical, GdaInterchange::is_canonical

canonical 返回比特模式的规范编码;is_canonical 检测比特模式是否规范。

pub fn GdaInterchange::canonical(Self) -> Self
pub fn GdaInterchange::is_canonical(Self) -> Bool

canonical 对比特模式解码后重新编码,从而将非规范的 declet 和载荷替换为其规范形式。若这样做不改变比特模式,is_canonical 为真。

对编码的符号运算

不经解码直接操作编码的符号位。

pub fn GdaInterchange::copy(Self) -> Self
pub fn GdaInterchange::copy_abs(Self) -> Self
pub fn GdaInterchange::copy_negate(Self) -> Self
pub fn GdaInterchange::copy_sign(Self, Self) -> Self

当两个比特模式的格式不同时,copy_sign 会中止。

///|
test "DPD interchange" {
  let d = (s : String) => @decimal_gda.Decimal::from_string(s).unwrap()
  let (bits, flags) = @decimal_gda.GdaInterchange::from_decimal(d("1.234567890"), Decimal32)
  inspect(bits.to_hex(), content="#25F4D2E8")
  inspect(bits.to_decimal(), content="1.234568")
  inspect(flags.inexact(), content="true")
  let back = @decimal_gda.GdaInterchange::from_hex("#A2300000000003D0", Decimal64).unwrap()
  inspect(back.to_decimal(), content="-7.50")
  inspect(bits.copy_negate().to_decimal(), content="-1.234568")
}

trait 实现

枚举和标志集的派生 Eq

GdaRoundingMode、GdaSignal、GdaFlags、GdaTrapSet 和 GdaInterchangeFormat 具有派生的 Eq 方法,用于检验两个值是否相等。

pub fn GdaRoundingMode::equal(Self, Self) -> Bool
pub fn GdaRoundingMode::not_equal(Self, Self) -> Bool
pub fn GdaSignal::equal(Self, Self) -> Bool
pub fn GdaSignal::not_equal(Self, Self) -> Bool
pub fn GdaFlags::equal(Self, Self) -> Bool
pub fn GdaFlags::not_equal(Self, Self) -> Bool
pub fn GdaTrapSet::equal(Self, Self) -> Bool
pub fn GdaTrapSet::not_equal(Self, Self) -> Bool
pub fn GdaInterchangeFormat::equal(Self, Self) -> Bool
pub fn GdaInterchangeFormat::not_equal(Self, Self) -> Bool

它们逐字段(或逐变体)比较。GdaContext 没有 Eq。Decimal 有自己的数值 Eq,见比较运算符。

Luna-Flow/arithmetic 的上下文式 trait

Decimal 在本包私有引擎之上实现 Luna-Flow/arithmetic 的上下文式 trait:每个运算在一个 ArithmeticContext 下运行,返回结果或错误。

pub fn Decimal::add_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::sub_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::mul_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::div_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::abs_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::sqrt_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::exp_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub impl @arithmetic.AddContextual for Decimal
pub impl @arithmetic.SubContextual for Decimal
pub impl @arithmetic.MulContextual for Decimal
pub impl @arithmetic.DivContextual for Decimal
pub impl @arithmetic.AbsContextual for Decimal
pub impl @arithmetic.SqrtContextual for Decimal
pub impl @arithmetic.ExpContextual for Decimal

每个方法把算术上下文转换为引擎上下文,运行运算,并把运算引发的条件映射到返回结果:

引发的条件结果
invalid_contextErr(unsupported)
division_by_zeroErr(division_by_zero)
任何其他错误标志Err(domain_error)
以上皆无Ok,带有值以及诊断信息 inexact、rounded、overflow、underflow、subnormal 和 clamped

舍入为 ArithmeticContext 的五种舍入算法之一。sqrt_contextual 和 exp_contextual 按它舍入,而不像 GDA 函数那样按偶数优先舍入。

exp_contextual 需要一个设置了指数边界的 ArithmeticContext。与包函数 exp 一样,它受实现限制约束:精度、Emax⁡E_{\max} 和 −Emin⁡-E_{\min} 都不超过 999 999999\,999。未设置 e_min 或 e_max 的 ArithmeticContext 不会得到默认边界,而是转换为完整范围 ±999 999 999\pm 999\,999\,999,这对 exp 是无效上下文。

这样的上下文的结果为 Err(unsupported),与操作数无关(包括 -Infinity)。其消息以 invalid context: 开头,说明该限制,并指出 ArithmeticContext 未设置的边界。因此 ArithmeticContext::new(16) 会失败,而 ArithmeticContext::new(16, e_min=-999_999, e_max=999_999) 和 ArithmeticContext::decimal64() 可以正常工作。

///|
test "exp_contextual needs exponent bounds" {
  let one = @decimal_gda.Decimal::one()
  let unbounded = @lf_arith.ArithmeticContext::new(16)
  let error = @lf_arith.ExpContextual::exp_contextual(one, unbounded).unwrap_err()
  inspect(error.is_unsupported(), content="true")
  inspect(
    error.message,
    content="invalid context: exp requires precision, e_max and -e_min of at most 999999; the ArithmeticContext sets no e_min and e_max",
  )
  let bounded = @lf_arith.ArithmeticContext::new(16, e_min=-999_999, e_max=999_999)
  inspect(
    @lf_arith.ExpContextual::exp_contextual(one, bounded).unwrap().value,
    content="2.718281828459045",
  )
}

NumericFormatContextual

Decimal 实现 NumericFormatContextual,它描述 ArithmeticContext 的数值格式。zero_contextual 和 one_contextual 分别返回 0 和 1,二者都带有该上下文的精度,且不检查上下文。

pub fn Decimal::zero_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::one_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::epsilon_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::min_normal_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::max_finite_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::classify_contextual(Self) -> @arithmetic.FpClass
pub impl @arithmetic.NumericFormatContextual for Decimal

设 pp 为上下文的精度,Emin⁡E_{\min}、Emax⁡E_{\max} 为其指数限制:

方法返回
epsilon_contextualnext_plus⁡(1)−1=101−p\operatorname{next\_plus}(1) - 1 = 10^{1-p}
min_normal_contextual10Emin⁡10^{E_{\min}}
max_finite_contextual(10p−1)⋅10Emax⁡−p+1(10^p - 1) \cdot 10^{E_{\max} - p + 1}

若 ArithmeticContext 无效,即无法转换为引擎上下文(例如精度高于 999,999,999),这三个方法返回一个零,其精度取该上下文的精度并限制在 1 到 999,999,999 的范围内。

Luna-Flow/arithmetic 的 checked trait

Decimal 实现 Luna-Flow/arithmetic 的 checked trait,它们返回 Result 而不是标志。

pub fn Decimal::parse_checked(String, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::sqrt_checked(Self, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::pow_int_checked(Self, Int32, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::pow_nat_checked(Self, UInt32, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub impl @arithmetic.ParseChecked for Decimal
pub impl @arithmetic.SqrtChecked for Decimal
pub impl @arithmetic.PowIntChecked for Decimal
pub impl @arithmetic.PowNatChecked for Decimal
pub impl @arithmetic.DivChecked for Decimal

parse_checked 把算术上下文转换为引擎上下文,并在其下转换字符串:上下文的精度、舍入、指数边界和 clamp 设置均会生效。若上下文无法转换(例如精度高于 999,999,999),它在读取字符串之前就因该转换返回 Err(domain_error)。它返回转换后的值;溢出等条件可从 to_number 获取。语法无效时返回 Err(parse_error)。

sqrt_checked 对负操作数返回 Err(domain_error)。

pow_int_checked 和 pow_nat_checked 以精确转换的整数指数调用幂运算,该指数至少有十位数字,足以容纳每个可接受的指数。因此精度为 7 时 (-1).pow_int_checked(12345679, ctx) 为 -1。在以下情形它们返回错误:

用例结果
零底数,负指数Err(division_by_zero)
power 引发 invalid_context:在指数边界不在 ±999 999\pm 999\,999 以内的上下文下,指数为大于 999,999,999 的正数带 invalid context: 消息的 Err(unsupported)
任何其他无效结果Err(domain_error)
pow_nat_checked,指数大于 999,999,999Err(unsupported)

DivChecked::div_checked 在给定上下文下做除法,除数为零时返回 Err(division_by_zero),除法无效时返回 Err(domain_error)。

FromNat、FromInteger 与 luna-generic 代数 trait

Decimal 实现 luna-generic 的 trait,使泛型代数代码可以使用它。

pub impl @luna-generic.FromNat for Decimal
pub impl @luna-generic.FromInteger for Decimal
pub impl @luna-generic.Zero for Decimal
pub impl @luna-generic.One for Decimal
pub impl @luna-generic.AddMonoid for Decimal
pub impl @luna-generic.MulMonoid for Decimal
pub impl @luna-generic.AddGroup for Decimal
pub impl @luna-generic.Semiring for Decimal
pub impl @luna-generic.Ring for Decimal

FromNat::from_natural 和 FromInteger::from_integer 是从自然数和整数出发的典范映射。Decimal 没有这些名字的固有方法:请调用 trait 方法,或 Decimal::from_bigint(n).normalized(),它们计算的就是这个。它们用 Decimal::from_bigint(34 位)转换并约简结果,因此只对至多 34 位有效数字的整数是精确的。要转换其他 Luna-Flow 整数类型,请先用 Integral::normalize 取其代表元,或使用 lift_to。

Zero::zero 和 One::one 即 Decimal::zero() 和 Decimal::one()。

环结构使用无上下文的运算符。由于 + 会舍入到操作数精度,只有当和保持在该精度之内时,环公理才精确成立。

Decimal::to_repr 以及 @def.Floating、Show 与 Debug 的实现

Decimal 实现了 def 包的浮点词汇、Show(参见 Decimal::to_string)以及 Debug。

pub impl @def.Floating for Decimal
pub fn Decimal::to_repr(Self) -> @debug.Repr

Floating 方法为 classify、signum、precision、with_precision 和 normalized。前三个见 Decimal::classify,后两个见 Decimal::normalized。to_repr 是结构化的 Debug 表示。

///|
test "trait adapters" {
  let d = (s : String) => @decimal_gda.Decimal::from_string(s).unwrap()
  let actx = @lf_arith.ArithmeticContext::new(4)
  let out = d("2").div_contextual(d("3"), actx).unwrap()
  inspect(out.value, content="0.6667")
  inspect(out.diagnostics.inexact, content="true")
  inspect(d("1").div_contextual(d("0"), actx) is Err(_), content="true")
  inspect(@decimal_gda.Decimal::epsilon_contextual(actx), content="0.001")
  inspect(d("1.5").pow_int_checked(3, actx).unwrap(), content="3.375")
  inspect(@decimal_gda.Decimal::from_bigint(1200N).normalized(), content="1.2E+3")
}

完整公共接口

此快照是该包生成的 pkg.generated.mbti。当正文与接口不一致时,以接口为准。

// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating/decimal_gda"

import {
  "Luna-Flow/arithmetic",
  "Luna-Flow/floating/binary",
  "Luna-Flow/floating/def",
  "Luna-Flow/luna-generic",
  "moonbitlang/core/bigint",
  "moonbitlang/core/debug",
}

// Values
pub fn abs(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn add(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn apply(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn class_name(Decimal, GdaContext) -> GdaOutcome[String]

pub fn compare(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn compare_signal(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn compare_total(Decimal, Decimal, GdaContext) -> GdaOutcome[Int32]

pub fn compare_total_magnitude(Decimal, Decimal, GdaContext) -> GdaOutcome[Int32]

pub fn divide(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn divide_integer(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn exp(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn fma(Decimal, Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub let gda_value_e_max : Int32

pub let gda_value_e_tiny : Int32

pub fn is_normal(Decimal, GdaContext) -> GdaOutcome[Bool]

pub fn is_subnormal(Decimal, GdaContext) -> GdaOutcome[Bool]

pub fn ln(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn log10(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn logb(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn logical_and(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn logical_invert(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn logical_or(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn logical_xor(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn max(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn max_magnitude(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn min(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn min_magnitude(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn minus(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn multiply(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn next_minus(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn next_plus(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn next_toward(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn plus(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn power(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn quantize(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn reduce(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn remainder(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn remainder_near(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn rescale(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn rotate(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn round_to_integral_exact(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn round_to_integral_value(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn scaleb(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn shift(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn sqrt(Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn subtract(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]

pub fn to_number(String, GdaContext) -> GdaOutcome[Decimal]

pub fn try_exp(Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]

pub fn try_ln(Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]

pub fn try_log10(Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]

pub fn try_power(Decimal, Decimal, GdaContext) -> Result[GdaOutcome[Decimal], @arithmetic.ArithmeticError]

// Errors

// Types and methods
#derive(@debug.Debug)
pub struct Decimal {
  // private fields
}
pub fn Decimal::abs(Self) -> Self
pub fn Decimal::abs_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::add(Self, Self) -> Self
pub fn Decimal::add_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::clamp(Self, min~ : Self, max~ : Self) -> Self
pub fn Decimal::clamp_checked(Self, min~ : Self, max~ : Self) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::classify(Self) -> @arithmetic.FpClass
pub fn Decimal::classify_contextual(Self) -> @arithmetic.FpClass
pub fn Decimal::coefficient(Self) -> @bigint.BigInt
pub fn Decimal::compare(Self, Self) -> Int32
pub fn Decimal::compare_checked(Self, Self) -> Result[Int32, @arithmetic.ArithmeticError]
pub fn Decimal::compare_total(Self, Self) -> Int32
pub fn Decimal::compare_total_magnitude(Self, Self) -> Int32
pub fn Decimal::copy(Self) -> Self
pub fn Decimal::copy_abs(Self) -> Self
pub fn Decimal::copy_negate(Self) -> Self
pub fn Decimal::copy_sign(Self, Self) -> Self
pub fn Decimal::div(Self, Self) -> Self
pub fn Decimal::div_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::epsilon_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::equal(Self, Self) -> Bool
pub fn Decimal::exp_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::exponent10(Self) -> Int32
pub fn Decimal::from_bigint(@bigint.BigInt, precision? : Int32) -> Self
pub fn Decimal::from_binary(@binary.Binary, precision~ : Int32, mode? : GdaRoundingMode) -> Self
pub fn Decimal::from_double(Double, precision? : Int32) -> Self
pub fn Decimal::from_float(Float, precision? : Int32) -> Self
pub fn Decimal::from_int(Int32, precision? : Int32) -> Self
pub fn Decimal::from_string(String, precision? : Int32) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::identical(Self, Self) -> Bool
pub fn Decimal::inf(negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::is_canonical(Self) -> Bool
pub fn Decimal::is_finite(Self) -> Bool
pub fn Decimal::is_infinite(Self) -> Bool
pub fn Decimal::is_nan(Self) -> Bool
pub fn Decimal::is_negative_zero(Self) -> Bool
pub fn Decimal::is_qnan(Self) -> Bool
pub fn Decimal::is_signed(Self) -> Bool
pub fn Decimal::is_snan(Self) -> Bool
pub fn Decimal::is_zero(Self) -> Bool
pub fn Decimal::make(@bigint.BigInt, Int32, Int32, mode? : GdaRoundingMode) -> Self
pub fn Decimal::max_finite_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::min_normal_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::mul(Self, Self) -> Self
pub fn Decimal::mul_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::nan(precision? : Int32) -> Self
pub fn Decimal::nan_payload(Self) -> @bigint.BigInt
pub fn Decimal::neg(Self) -> Self
pub fn Decimal::negative_zero(precision? : Int32) -> Self
pub fn Decimal::normalized(Self) -> Self
pub fn Decimal::not_equal(Self, Self) -> Bool
pub fn Decimal::one(precision? : Int32) -> Self
pub fn Decimal::one_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::op_ge(Self, Self) -> Bool
pub fn Decimal::op_gt(Self, Self) -> Bool
pub fn Decimal::op_le(Self, Self) -> Bool
pub fn Decimal::op_lt(Self, Self) -> Bool
pub fn Decimal::output(Self, &Logger) -> Unit
pub fn Decimal::parse_checked(String, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::pow_int_checked(Self, Int32, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::pow_nat_checked(Self, UInt32, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::precision(Self) -> Int32
pub fn Decimal::quiet_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::same_quantum(Self, Self) -> Bool
pub fn Decimal::signaling_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::signum(Self) -> @def.Sign
pub fn Decimal::sqrt(Self) -> Self
pub fn Decimal::sqrt_checked(Self, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::sqrt_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::sub(Self, Self) -> Self
pub fn Decimal::sub_contextual(Self, Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::to_binary(Self, precision~ : Int32, mode? : @binary.BinaryRoundingMode) -> @binary.Binary
pub fn Decimal::to_eng_string(Self) -> String
pub fn Decimal::to_repr(Self) -> @debug.Repr
pub fn Decimal::to_sci_string(Self) -> String
pub fn Decimal::to_string(Self) -> String
pub fn Decimal::trim(Self) -> Self
pub fn Decimal::try_make(@bigint.BigInt, Int32, Int32, mode? : GdaRoundingMode) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::with_precision(Self, Int32, GdaRoundingMode) -> Self
pub fn Decimal::zero(precision? : Int32) -> Self
pub fn Decimal::zero_contextual(@arithmetic.ArithmeticContext) -> Self
pub impl @arithmetic.AbsContextual for Decimal
pub impl @arithmetic.AddContextual for Decimal
pub impl @arithmetic.CompareChecked for Decimal
pub impl @arithmetic.DivChecked for Decimal
pub impl @arithmetic.DivContextual for Decimal
pub impl @arithmetic.ExpContextual for Decimal
pub impl @arithmetic.MulContextual for Decimal
pub impl @arithmetic.NumericFormatContextual for Decimal
pub impl @arithmetic.ParseChecked for Decimal
pub impl @arithmetic.PowIntChecked for Decimal
pub impl @arithmetic.PowNatChecked for Decimal
pub impl @arithmetic.SqrtChecked for Decimal
pub impl @arithmetic.SqrtContextual for Decimal
pub impl @arithmetic.SubContextual for Decimal
pub impl @def.Floating for Decimal
pub impl @luna-generic.AddGroup for Decimal
pub impl @luna-generic.AddMonoid for Decimal
pub impl @luna-generic.FromInteger for Decimal
pub impl @luna-generic.FromNat for Decimal
pub impl @luna-generic.MulMonoid for Decimal
pub impl @luna-generic.One for Decimal
pub impl @luna-generic.Ring for Decimal
pub impl @luna-generic.Semiring for Decimal
pub impl @luna-generic.Zero for Decimal
pub impl Add for Decimal
pub impl Compare for Decimal
pub impl Div for Decimal
pub impl Eq for Decimal
pub impl Mul for Decimal
pub impl Neg for Decimal
pub impl Show for Decimal
pub impl Sub for Decimal

pub struct GdaContext {
  // private fields
}
pub fn GdaContext::basic_default() -> Self
pub fn GdaContext::clamp(Self) -> Bool
pub fn GdaContext::clear_flags(Self) -> Self
pub fn GdaContext::decimal128(rounding? : GdaRoundingMode) -> Self
pub fn GdaContext::decimal32(rounding? : GdaRoundingMode) -> Self
pub fn GdaContext::decimal64(rounding? : GdaRoundingMode) -> Self
pub fn GdaContext::e_max(Self) -> Int32
pub fn GdaContext::e_min(Self) -> Int32
pub fn GdaContext::extended(Self) -> Bool
pub fn GdaContext::flags(Self) -> GdaFlags
pub fn GdaContext::new(precision? : Int32, rounding? : GdaRoundingMode, e_min? : Int32, e_max? : Int32, clamp? : Bool, extended? : Bool, traps? : GdaTrapSet) -> Self
pub fn GdaContext::precision(Self) -> Int32
pub fn GdaContext::radix(Self) -> Int32
pub fn GdaContext::refinement_limit(Self) -> Int32?
pub fn GdaContext::reset(Self) -> Self
pub fn GdaContext::rounding(Self) -> GdaRoundingMode
pub fn GdaContext::trap(Self, GdaSignal, enabled? : Bool) -> Self
pub fn GdaContext::traps(Self) -> GdaTrapSet
pub fn GdaContext::try_new(precision? : Int32, rounding? : GdaRoundingMode, e_min? : Int32, e_max? : Int32, clamp? : Bool, extended? : Bool, traps? : GdaTrapSet) -> Result[Self, @arithmetic.ArithmeticError]
pub fn GdaContext::with_exponent_range(Self, e_min~ : Int32, e_max~ : Int32) -> Self
pub fn GdaContext::with_precision(Self, Int32) -> Self
pub fn GdaContext::with_refinement_limit(Self, Int32?) -> Self
pub fn GdaContext::with_rounding(Self, GdaRoundingMode) -> Self
pub fn GdaContext::with_traps(Self, GdaTrapSet) -> Self

#derive(Eq)
pub struct GdaFlags {
  // private fields
}
pub fn GdaFlags::clamped(Self) -> Bool
pub fn GdaFlags::combine(Self, Self) -> Self
pub fn GdaFlags::contains(Self, GdaSignal) -> Bool
pub fn GdaFlags::conversion_syntax(Self) -> Bool
pub fn GdaFlags::division_by_zero(Self) -> Bool
pub fn GdaFlags::division_impossible(Self) -> Bool
pub fn GdaFlags::division_undefined(Self) -> Bool
pub fn GdaFlags::equal(Self, Self) -> Bool
pub fn GdaFlags::inexact(Self) -> Bool
pub fn GdaFlags::invalid_context(Self) -> Bool
pub fn GdaFlags::invalid_operation(Self) -> Bool
pub fn GdaFlags::lost_digits(Self) -> Bool
pub fn GdaFlags::new() -> Self
pub fn GdaFlags::not_equal(Self, Self) -> Bool
pub fn GdaFlags::overflow(Self) -> Bool
pub fn GdaFlags::rounded(Self) -> Bool
pub fn GdaFlags::subnormal(Self) -> Bool
pub fn GdaFlags::underflow(Self) -> Bool

pub struct GdaInterchange {
  // private fields
}
pub fn GdaInterchange::canonical(Self) -> Self
pub fn GdaInterchange::copy(Self) -> Self
pub fn GdaInterchange::copy_abs(Self) -> Self
pub fn GdaInterchange::copy_negate(Self) -> Self
pub fn GdaInterchange::copy_sign(Self, Self) -> Self
pub fn GdaInterchange::format(Self) -> GdaInterchangeFormat
pub fn GdaInterchange::from_decimal(Decimal, GdaInterchangeFormat) -> (Self, GdaFlags)
pub fn GdaInterchange::from_hex(String, GdaInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn GdaInterchange::is_canonical(Self) -> Bool
pub fn GdaInterchange::to_decimal(Self) -> Decimal
pub fn GdaInterchange::to_hex(Self) -> String

#derive(Eq)
pub(all) enum GdaInterchangeFormat {
  Decimal32
  Decimal64
  Decimal128
}
pub fn GdaInterchangeFormat::context(Self) -> GdaContext
pub fn GdaInterchangeFormat::equal(Self, Self) -> Bool
pub fn GdaInterchangeFormat::not_equal(Self, Self) -> Bool

pub enum GdaOutcome[T] {
  Completed(T, GdaContext, GdaFlags)
  Trapped(GdaSignal, T, GdaContext, GdaFlags)
}
pub fn[T, U] GdaOutcome::absorb(Self[T], Self[U]) -> Self[T]
pub fn[T] GdaOutcome::completed(T, GdaContext) -> Self[T]
pub fn[T] GdaOutcome::next_context(Self[T]) -> GdaContext
pub fn[T] GdaOutcome::raised(Self[T]) -> GdaFlags
pub fn[T] GdaOutcome::resume_defined(Self[T]) -> Self[T]
pub fn[T] GdaOutcome::value(Self[T]) -> T

#derive(Eq)
pub(all) enum GdaRoundingMode {
  HalfEven
  HalfUp
  HalfDown
  Down
  Ceiling
  Floor
  Up
  ZeroFiveUp
}
pub fn GdaRoundingMode::equal(Self, Self) -> Bool
pub fn GdaRoundingMode::not_equal(Self, Self) -> Bool

#derive(Eq)
pub(all) enum GdaSignal {
  Clamped
  DivisionByZero
  Inexact
  InvalidOperation
  LostDigits
  Overflow
  Rounded
  Subnormal
  Underflow
}
pub fn GdaSignal::equal(Self, Self) -> Bool
pub fn GdaSignal::not_equal(Self, Self) -> Bool

#derive(Eq)
pub struct GdaTrapSet {
  // private fields
}
pub fn GdaTrapSet::clamped(Self) -> Bool
pub fn GdaTrapSet::contains(Self, GdaSignal) -> Bool
pub fn GdaTrapSet::division_by_zero(Self) -> Bool
pub fn GdaTrapSet::equal(Self, Self) -> Bool
pub fn GdaTrapSet::inexact(Self) -> Bool
pub fn GdaTrapSet::invalid_operation(Self) -> Bool
pub fn GdaTrapSet::lost_digits(Self) -> Bool
pub fn GdaTrapSet::new() -> Self
pub fn GdaTrapSet::not_equal(Self, Self) -> Bool
pub fn GdaTrapSet::overflow(Self) -> Bool
pub fn GdaTrapSet::rounded(Self) -> Bool
pub fn GdaTrapSet::subnormal(Self) -> Bool
pub fn GdaTrapSet::underflow(Self) -> Bool
pub fn GdaTrapSet::with_signal(Self, GdaSignal, enabled? : Bool) -> Self

// Type aliases

// Traits