decimal_gda API
目的
decimal_gda は M. F. Cowlishaw による General Decimal Arithmetic Specification 1.70(GDA 1.70)を実装します。3 つの部分からなります。
Decimalは、符号、任意の長さの十進の係数、十進の指数からなります。GdaContextは、精度、丸め、指数の限界、クランプ、拡張算術とサブセット算術の切り替え、フラグ、有効なトラップを保持します。- GDA のすべての演算は
GdaOutcomeを返す純関数です。これは、定義された結果、次のコンテキスト、演算が発生させた条件からなります。
チュートリアルはこのパッケージの使い方を示し、設計ページはここに述べた規則を導きます。
このパッケージは IEEE 754 のパッケージ decimal に依存しません。公開するのは GDA の層(Decimal、GdaContext、Gda* 型、GDA の演算にちなんで名付けられた関数)と、Luna-Flow/arithmetic および Luna-Flow/luna-generic のトレイトへのアダプターだけです。関数の土台となるエンジンはフラグを保持せず、パッケージ内で非公開です。
このページを通じて、有限値は係数 と指数 を用いて と書きます。その調整指数は 、すなわち先頭の桁の指数です。精度 のコンテキストに対して、 は結果が取りうる最小の指数であり、クランプ時には が最大の指数です。
インポート
moon.pkg にパッケージを追加します。
import {
"Luna-Flow/floating/decimal_gda",
}
このページの例では、パッケージを別名 @decimal_gda. で、トレイトアダプタを Luna-Flow/arithmetic の別名 @lf_arith. で呼び出します。いくつかの例では局所的な補助関数 d(s) を定義します。これは Decimal::from_string でリテラルをパースし、その指数を保持します。
Decimal 値
Decimal
Decimal は不変の GDA の数で、有限の数、または特殊値(無限大、quiet NaN、signaling NaN)のいずれかです。
pub struct Decimal {
// private fields
} derive(@debug.Debug)
有限の数は指数を保持するため、2.50 と 2.5 は同じコホートの異なる要素です。両者は数値としては等しく比較されますが、compare_total、same_quantum、出力では異なります。ゼロは符号付きです。NaN は符号と非負の整数のペイロードを持ちます。ペイロードが 0 の NaN は診断情報を持たない NaN を表し、NaN と出力されます。GDA 1.70、11 ページ(Special values)はペイロードがある場合のそれを正の整数としており、18 ページと 19 ページは文字列形式を、先頭にゼロを持たない任意の桁が続く NaN としています。
すべての値は精度、すなわち値の精度(precision())を保持します。これは、その値を生成したコンテキストまたはコンストラクタの精度です。GDA の演算は値の精度を無視し、代わりにコンテキストの精度を使います。
係数は永続的な十進のリム配列として保持されるため、値は自由に共有できます。
Decimal::zero, Decimal::negative_zero, Decimal::one
それぞれ指数 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() | |
inf(negative=true) | |
nan() | ペイロード 0 の正の quiet NaN で、診断情報を持たない NaN を表すもの |
ペイロードは高々 precision - 1 桁です。GDA 1.70 の 11 ページ(Special values)が、ペイロードは係数で表せる値より 1 桁少ない範囲に収まると定めているためです。負のペイロード、または指定した precision に対して広すぎるペイロードは中止します。精度 1 ではペイロード 0 だけが存在します。
signaling NaN を使う算術演算は InvalidOperation を発生させ、その NaN を、符号とペイロードが同じ quiet 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_max | 調整指数 | |
gda_value_e_tiny | 最下位桁の指数。精度によらず、単純な演算子の作業コンテキストの です |
有限値は、ゼロを含めて(ゼロは 1 桁を持ちます)、 gda_value_e_tiny かつ gda_value_e_max のときに限り保持されます。ここで はその指数、 はその係数、 はその調整指数です。
コンストラクタ make、try_make、from_string は、限界の外の値を拒否します。演算は、丸め、アンダーフロー、オーバーフローによって、生成する値を限界の内側に収めます。したがって、得られるすべての Decimal はこの条件を満たします。これは、保持されるすべての有限値について x + 0 == x と x * 1 == x が成り立つことの背後にある不変条件です(単純な演算子を参照)。
限界の外の文字列は、丸めた値が必要なときにコンテキストを使って読み取ります。to_number は、コンテキストに従って丸め、オーバーフローし、アンダーフローします。
Decimal::make, Decimal::try_make
make(c, e, p) は、 を 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。コンテキストの 8 つの丸めアルゴリズムの 1 つ)で一度だけ 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 のべき乗へ繰り上がる場合を含む)は、ゼロや無限大に置き換えられることはありません。
| 条件 | make | try_make |
|---|---|---|
| 限界の外の指数または調整指数 | 2 つの限界と該当する指数を示すメッセージで中止する | 同じメッセージの定義域エラー種別の 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 桁に丸められます。偶数への丸め(half to even)が既定で、from_binary では mode が別の方式を指定した場合はそれに従います |
precision の既定値は 34 桁です。from_binary では必須で、引数のビット数ではなく十進の桁数を数えます。
の二進浮動小数点数 はまず厳密に と書かれるため、結果が precision 桁に収まるときは変換は厳密です。たとえば from_double(0.1) は、 に最も近い binary64 の厳密な値を、偶数への丸め(half to even)で 34 桁に丸めたものです。
| 引数 | 結果 |
|---|---|
| NaN | quiet NaN(from_double は符号を保ちます) |
| 無限大 | 符号が同じ無限大 |
| ゼロ | 同じ符号のゼロ(from_binary は を返します) |
| 呼び出し | 結果 |
|---|---|
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 を含め、どの丸めも、10 進数を厳密な数としての値から precision ビットへ一度だけ丸め、途中での丸めはありません。
RoundTowardNegative と RoundTowardPositive で得られる 2 つの結果は十進値を包含します。初等関数はこの方法で認証付きの入力区間を構築します。ゼロは に、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") の指数は です。有効桁数が 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
| 値 | 出力形式 |
|---|---|
| かつ の有限数 | 指数なし: 0.000123、7.50 |
| それ以外の有限数 | 1 桁、小数点の後に残りの桁、そして E±: 1.23E+7、1E-7、0E+2 |
| 無限大 | inf または -inf |
| NaN | nan、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 |
| NaN | Zero |
| それ以外の値 | Positive または Negative |
符号ビットを読むには is_signed を使ってください。
Decimal::coefficient, Decimal::exponent10
値の格納表現 を返します。
pub fn Decimal::coefficient(Self) -> @bigint.BigInt
pub fn Decimal::exponent10(Self) -> Int32
coefficient は を返します。NaN ではペイロード、無限大では 0 です。exponent10 は を返し、特殊値では 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 は符号ビットを読むので、、、負の NaN で真になります。is_zero は指数にかかわらず有限のゼロで真です。
Decimal::is_qnan, Decimal::is_snan
値が quiet NaN か signaling 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 にあります。
符号、コホート、精度の変換
符号演算
ここにあるすべての演算は値の符号ビットだけを変更します。signaling 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 は値の精度に偶数への最近接丸め(half to even)で丸め(Decimal を参照)、結果から末尾のゼロをすべて除きます。7.50 は 7.5 に、1200 は 1.2E+3 になります。
trim は小数部の末尾のゼロだけを除き、指数を正にすることはありません。7.50 は 7.5 になり、1200 は 1200 のままです。零は指数 0 の 0 になります。
with_precision(p, mode) は丸めアルゴリズム mode で p 桁に丸め、末尾のゼロを除き、値の精度を設定します。特殊値は新しい精度を得るだけです。
3 つのいずれもフラグを報告しません。
丸めの桁上がりが格納された値の指数の処理系限界の上端に達した場合は、その限界を最大の調整指数として、オーバーフローの規則(GDA 1.70、53 ページ(Overflow))に従うので、結果が限界を超える有限値になることはありません。たとえば 9999E+2147483643 を 2 桁に丸めると、ゼロから遠ざかる方向に丸める丸めアルゴリズムや最近接丸めでは inf になります。ゼロ方向に丸める丸めアルゴリズムでは 9.9E+2147483646 になり、これはその桁幅での最大の有限値です。
Decimal::identical
identical は、2 つの値が同じデータかどうかを判定します。
pub fn Decimal::identical(Self, Self) -> Bool
クラス、符号、係数、指数が同じで、精度も同じ 2 つの値では真になります。NaN では、ペイロードと種類(quiet か signaling か)も同じであることが必要です。これは compare_total が 0 で、かつ精度が等しいことと同じです。GDA は compare_total を精度なしで定義しています。
2.5 と 2.50 は == では等しく、identical ではありません。0 と -0 は等しく、identical ではありません。同じペイロードを持つ 2 つの NaN は identical ですが、== と compare はすべての NaN を同じものとして扱います。
値の精度がこの関係に含まれるのは、通常の演算子がオペランドのうち大きい方の精度に丸めるからです。精度 3 の 1 と精度 34 の 1 は等しく、compare_total は 0 ですが、3 で割るとそれぞれ 0.333 と 0.3333333333333333333333333333333333 になります。identical な 2 つの値は、どの関数でも同じ結果を与えます。
Decimal::same_quantum
same_quantum は 2 つの値が同じ指数を持つかを判定します。
pub fn Decimal::same_quantum(Self, Self) -> Bool
| オペランド | 結果 |
|---|---|
| 2 つの有限値 | 指数が等しいとき真 |
| 2 つの無限大 | 真 |
| 2 つの NaN | 真 |
| それ以外の組 | 偽 |
GDA コンテキスト
GdaContext
GdaContext は不変の GDA コンテキストです。演算ポリシー、コンテキストのフラグ、有効なトラップを保持します。
pub struct GdaContext {
// private fields
}
ポリシーは、精度 、丸め(GdaRoundingMode)、、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]
既定値は次のとおりです。
| パラメータ | 既定値 |
|---|---|
precision | 34 |
rounding | HalfEven |
e_min | -999_999_999 |
e_max | 999_999_999 |
clamp | false |
extended | true |
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のもので、、 と指数の計算をIntの範囲に収めます。GDA 1.70 自体は限界を定めていません。e_minは-e_max、1 - e_max(IEEE 754 の規則)、-e_max - 1のいずれかです。GDA 1.70 の 10 ページ(Abstract representation of numbers)は と の絶対値の差を高々 1 とし、結果の範囲が釣り合うことを求めています。e_min=-50, e_max=60のような範囲は拒否されます。同じ箇所は、係数の最大長がある場合はElimitがその 5 倍を超えることも求めています。コンテキストには係数の最大長がなく、公式 decTest コーパスの 913 行はe_maxが精度の 4 倍未満で実行されるため、ライブラリはこの要件を強制しません。
clamp と extended は、GDA の本文ではコンテキストのパラメーターではありません。これらはライブラリが保持している decNumber の拡張で、公式の decTest ファイルは両方を使います。
clamp=trueは Clamped 条件の折り畳み(fold-down)です(GDA 1.70、51 ページ(Exceptional conditions))。交換形式と同様に、指数を までに制限します。extended=falseは付録 A の GDA サブセット算術を選びます(GDA 1.70、55 ページ(Appendix A, The X3.274 subset)、脚注 43: コンテキストのパラメーターがそれを選ぶ「might」)。サブセット算術では、コンテキストを取るすべての演算が、 桁より長いオペランドを先に丸め(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は無効です。- サブセットコンテキストでは、コンテキストを取るすべての演算の結果が 3 つの規則に従います(GDA 1.70、55 ページ(Appendix A, The X3.274 subset))。ゼロは指数 0 の です。 より小さい結果(非正規化数の結果)は指数 0 の で、フラグ
Underflow、Subnormal、Inexact、Roundedが付きます(その演算が以前からそのフラグを立てていた場合は、expのようにClampedも付きます)。指数が正の結果は、そのとき 桁以下であれば指数 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)は 、next_toward(0, 1)はUnderflowを伴う です。 - コピー演算(
copy、copy_abs、copy_negate、copy_sign)はコンテキストを取らず、規則の対象外です。copy_negate(0)は です。 quantizeとrescaleは、ゼロの指数と正の指数を保持します(quantizeは 55 ページの例外で、rescaleはその旧名です)。quantize(-0, 1E+2)は0E+2です。to_numberは正の指数を保持し、ゼロは指数 0 の として読みます。- 付録 A は
reduce、rescale、scaleb、shift、rotate、remainder、remainder_near、2 つの round-to-integral 演算、next_towardを挙げていませんが、規則はすべての演算に適用されるのと同じようにこれらにも適用されます。decNumber 3.68 は、そのうちのいくつかについて別の結果を返します(規格合致性)。
- サブセットコンテキストでは、コンテキストを取るすべての演算の結果が 3 つの規則に従います(GDA 1.70、55 ページ(Appendix A, The X3.274 subset))。ゼロは指数 0 の です。 より小さい結果(非正規化数の結果)は指数 0 の で、フラグ
デフォルトコンテキスト
デフォルトコンテキスト(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
| コンテキスト | 丸め | clamp | 拡張 | トラップ | |||
|---|---|---|---|---|---|---|---|
basic_default | 9 | HalfUp | いいえ | いいえ | DivisionByZero, InvalidOperation, Overflow, Underflow, Clamped | ||
decimal32 | 7 | HalfEven | あり | あり | なし | ||
decimal64 | 16 | HalfEven | あり | あり | なし | ||
decimal128 | 34 | HalfEven | あり | あり | なし |
basic_default は仕様の基本デフォルトコンテキストです。精度 9、round-half-up で、inexact、rounded、subnormal 以外のすべてのトラップが有効です(GDA 1.70、16 ページ(Default contexts))。付録 A はデフォルトコンテキストで Lost digits 条件を 0 に設定すべきだとしているので(GDA 1.70、56 ページ(Appendix A, The X3.274 subset))、ライブラリは lost-digits のトラップも無効のままにします。ライブラリはこのコンテキストに extended=false(付録 A の X3.274 サブセット)を設定しますが、GDA 1.70 の基本デフォルトコンテキストの定義はこれを設定していません。
decimal32、decimal64、decimal128 は IEEE 754 形式の拡張デフォルトコンテキストで、round-half-even、トラップなしです。rounding を与えると、丸めアルゴリズムがそれに置き換わります。
GdaContext::new() は basic_default() ではありません。精度 34、half-even、トラップなしです。これらの値は一度だけ作られ、共有されます。
GdaContext::with_precision, GdaContext::with_rounding, GdaContext::with_exponent_range
設定を 1 つ変えたコンテキストのコピーを返します。
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 未満の上限は 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) | トラップを 1 つ有効にします。enabled=false を与えると無効にします |
with_traps | 集合全体を置き換えます |
clear_flags | フラグを空にし、トラップは保ちます |
reset | 両方を空にします |
GdaRoundingMode
GdaRoundingMode はコンテキストの丸めの型です。8 つの値は丸めアルゴリズムです(GDA 1.70、13 ページと 14 ページ(Abstract representation of context))。
pub(all) enum GdaRoundingMode {
HalfEven
HalfUp
HalfDown
Down
Ceiling
Floor
Up
ZeroFiveUp
}
厳密な結果が表現可能な隣り合う 2 つの値の間に厳密にあるとき、丸めアルゴリズムは次のように選びます。
| 丸め | 結果 |
|---|---|
Down | ゼロに近い方の隣の値 |
Up | ゼロから遠いほうの隣の値 |
Ceiling | 大きいほうの隣の値 |
Floor | 小さいほうの隣の値 |
HalfEven | 近いほうの隣の値。同点のときは最終桁が偶数のほう |
HalfUp | 近いほうの隣の値。同点のときはゼロから離れるほう |
HalfDown | 近いほうの隣の値。同点のときはゼロに近づくほう |
ZeroFiveUp | ゼロ方向に丸めますが、それで最終桁が 0 または 5 になる場合はゼロから離れる方向に丸めます |
設計ページでは、各アルゴリズムを式で示します。
シグナル、フラグ、トラップ
GdaSignal
GdaSignal は GDA 1.70 の 8 つのシグナルと、その付録 A の Lost digits シグナルを表します。各シグナルには、コンテキストのフラグの中のフラグと、トラップ有効化が 1 つずつあります。
pub(all) enum GdaSignal {
Clamped
DivisionByZero
Inexact
InvalidOperation
LostDigits
Overflow
Rounded
Subnormal
Underflow
}
仕様には 8 つのシグナル(GDA 1.70、15 ページ(flags and trap-enablers))があります。Clamped、DivisionByZero、Inexact、InvalidOperation、Overflow、Rounded、Subnormal、Underflow です。LostDigits は 9 つ目で、サブセット算術でのみ発生します(GDA 1.70、56 ページ(Appendix A, The X3.274 subset))。
仕様が invalid-operation をシグナルとする 4 つの条件、すなわち変換構文エラー、除算不可能、除算未定義、無効なコンテキストは、シグナルではありません(GDA 1.70、51 ページと 52 ページ(Exceptional conditions))。これらは GdaFlags の条件であり、いずれかが発生すると InvalidOperation シグナルが発生します。その方法は GdaFlags::contains に記します。
GdaFlags
GdaFlags は条件の集合です。1 つの演算が発生させた条件と、コンテキストのフラグの両方を記録します。ライブラリが発生させる 13 の条件を保持し、これは Exceptional conditions の節(GDA 1.70、51 ページから 53 ページ)の 12 の条件から Insufficient storage を除いたものと、付録 A の Lost digits です。条件を区別して保持する理由は設計ページで説明します。
pub struct GdaFlags {
// private fields
} derive(Eq)
フィールドは非公開です。13 個の条件はそれぞれ同名のメソッドで読み取ります(例: raised().inexact()、flags().division_by_zero())。公開コンストラクタは new だけなので、集合は new と combine だけで作られます。GdaTrapSet も同じ方法で読み取り、9 つのシグナルにそれぞれメソッドが 1 つあります。
GdaFlags::new, GdaFlags::contains, GdaFlags::combine
new は空集合、combine はフィールドごとの和集合、contains は GDA の invalid-operation のグループ化を伴うシグナルの所属判定です。アクセサメソッドはそれぞれ 1 つの条件を読み取ります。
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() は通常の無効演算の条件だけを読み取り、通常の条件は 4 つのすべてで発生するわけではありません。除算未定義と除算不可能では発生し、変換構文エラーと無効なコンテキストでは発生しません。これは decNumber と同じで、decTest の各行はその状態ワードを記述しています。
GdaTrapSet
GdaTrapSet は有効なトラップ有効化の集合で、9 つのシグナルにそれぞれ 1 つあります。
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 はトラップを 1 つも有効にしません。with_signal(s) はトラップを 1 つ有効にし、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 で分解し、アクセサを使えますが、outcome を構築できるのは GDA 演算、GdaOutcome::completed、GdaOutcome::absorb だけです。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 と次のコンテキストのフラグに加わります。フラグでは、4 つの詳細な無効条件のいずれかがそれらに含まれるとき、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 関数は、いずれも同じ 3 つの手順で終わります。 を演算が発生させた条件、 を入力コンテキストとします。
- が空なら、結果は
Completed(v, C, none)で、同じコンテキストが返ります。 - そうでなければ、次のコンテキスト はフラグを とした です。ここで、 が 4 つの詳細な無効条件のいずれかを含むときは
invalid_operationフラグも設定されます。 - トラップされるシグナルは、次の順で最初に
raised.contains(s)とtraps.contains(s)を満たす です。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 を返します。ただし 4 つの try_ 関数は、これを Result に入れて返します。signaling NaN のオペランドは InvalidOperation を発生させ、対応する quiet NaN を生じます。quiet NaN のオペランドは何も発生させずに伝播します(最初の NaN のオペランドが優先されます)。サブセットコンテキスト(extended=false)では、精度より長い有限のオペランドが先に丸められます(GdaContext::new を参照)。特に断りのない限り、結果は厳密な結果を正しく丸めたもので、結果が厳密な場合は演算ごとに示す理想指数を持ちます。
to_number
to_number は、GDA の to-number 変換によって文字列を Decimal に変換します。
pub fn to_number(String, GdaContext) -> GdaOutcome[Decimal]
リテラルは、精度に合わせて丸める必要があるか、指数範囲にクランプする必要がない限り、その指数を保持します。オーバーフローとアンダーフローは、どれほど大きくてもテキストの厳密な指数から判定されます。
不正なテキストは ConversionSyntax 付きの quiet NaN を与えます。サブセットコンテキストでは、無限大と NaN も変換構文エラーになります。
apply, plus, minus, abs
1 つのオペランドをコンテキストに合わせて丸めます。plus は 、minus は 、abs は 、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 では 、ほかのすべての丸めアルゴリズムでは になります。一方、plus(0)、minus(-0)、およびどちらのゼロの abs も です。サブセットコンテキストでは、ゼロの結果はすべて です。
拡張コンテキストでは、apply はゼロの符号を保ち、サブセットのオペランドを先に丸めません。
add, subtract, multiply, divide, fma
基本的な算術演算を行います。fma(a, b, c) は 1 回の丸めによる です。
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]
各演算の理想指数です。 と はそれぞれ第 1 オペランドと第 2 オペランドの指数です。
| 演算 | 理想指数 |
|---|---|
add, subtract | |
multiply | |
divide | (厳密でない商は 桁の完全な係数を持ちます) |
fma | 厳密な積と加数に適用した add の規則 |
厳密なゼロの和は、両方のオペランドが負なら 、両方が正なら 、それ以外では で、Floor では です。subtract では、オペランドは第 1 オペランドと符号を反転した第 2 オペランドです。サブセットコンテキストでは、精度 桁より長い第 2 オペランドは、まず自身の符号で丸められてから符号が反転されます(#761)。たとえば精度 1、Floor での subtract(0, 12) は -1E+1 です。したがって add(0, -0) は HalfEven では 0、Floor では -0 ですが、add(0, 0) はどの丸めアルゴリズムでも 0 です。
特殊なケース:
| オペランド | 結果 |
|---|---|
| , , | 無効 |
DivisionByZero 付きの | |
DivisionUndefined 付きの NaN | |
指数が のゼロと Clamped |
無効な結果は InvalidOperation を発生させます。このシグナルに数えられる条件は GdaFlags::new に挙げます。fma はサブセットコンテキストでは無効です。
はるかに小さい加数は、和のすべての丸めの境界より下にあるスティッキーな単位として吸収され、結果が非正規化数の格子へ丸められるのは、その厳密な結果が を下回るときだけです。したがって、これらの演算はいずれも一度だけ丸めます。精度 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_integer | 0 | |
remainder | (符号は の符号) | |
remainder_near | 。ここで は を最も近い整数に丸めた値で、同点の場合は偶数を選びます |
(または )が 桁を超える桁を必要とするとき、結果は DivisionImpossible を伴う NaN です。除数がゼロのときは DivisionByZero(非ゼロの数に対する divide_integer)または DivisionUndefined()になります。除数がゼロの場合や被除数が無限大の場合は、どちらの剰余も無効になります。被除数が有限で除数が無限大の場合は、被除数自身が剰余です。
拡張されたクランプ付きコンテキスト(GdaContext::new を参照)では、ゼロの商は厳密な符号を保ち、必要なときは に折り畳まれます。このとき 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 の指数 で表し、コンテキストの丸めで丸めた結果を返します(Rounded を発生させ、桁が失われるときは Inexact も発生させます)。rescale(x, n) は指数 で同じことを行い、 は整数値でなければなりません。
サブセットコンテキストでは、 桁より長い第 2 オペランドは先に 桁に丸められ(非ゼロの桁が失われるときは LostDigits を発生させます)、量子は丸めた後のオペランドです。精度 1 では quantize(0, 12) は 0E+1、rescale(0, 12) は 0E+10 です。
次のとき、結果は無効です
- 目標の指数が の外にある(サブセットコンテキストでは の外にある)。
- 結果の係数が 桁を超える桁を必要とする。または
- オペランドのちょうど一方が無限大である。
二つの無限大に対しては無限大を返します。
非正規化数の結果は 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]
指数が の値はすでに整数であり、 桁より長い場合でもそのまま返されます(clamp コンテキストでは、指数が引き続き折り下げられます)。
指数が負の値は、decNumber と同様に、作業精度 で指数 0 に量子化されます。そのため整数部が 桁に丸められることはありません。精度 3 では 12345.6 は 12346 になります。
round_to_integral_exact は桁が捨てられるとき Inexact と Rounded を発生させ、round_to_integral_value は決して発生させません。サブセットコンテキストでは、オペランドはまず 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 は、、、 のコンテキストだけを受け付けます。限界を超えると InvalidContext を発生させます。既定の指数範囲では NaN を返し、InvalidContext を発生させます。
| 限界 | 適用対象 |
|---|---|
| , , | exp、ln、log10、および非整数の指数での power |
| 非ゼロのオペランドの桁数が 999,999 以下である | exp, ln, log10 |
| 非ゼロのオペランドの調整指数が 999,999 以下かつ 以上である | exp、ln、log10、および非整数の指数を持つ power(その底と、下限にある指数) |
これは処理系限界です。GDA 1.70 の演算の本文にはこのような限界の記述がなく、この限界は Invalid context 条件によって許されます。この条件は、精度が「基となる具体的な表現の能力を超える」場合に適用されます(GDA 1.70、52 ページ(Exceptional conditions))。値 999,999 は decNumber の DEC_MAX_MATH です。理由は設計ページに述べてあります。
sqrt, exp, ln, log10
値の正しく丸められた平方根、指数関数、自然対数、常用対数を返します。
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]
結果はコンテキストの丸めアルゴリズムにかかわらず、最近接偶数丸めで丸められます。サブセットコンテキストでは、 桁より長いオペランドは、最近接偶数丸めではなくコンテキストの丸めアルゴリズムで先に丸められます(#762)。精度 1、Down での sqrt(0.999) は 0.9 です。厳密として挙げていない有限の結果は、すべて 桁で不正確です。
| 関数 | オペランド | 結果 |
|---|---|---|
sqrt | 完全平方数 | 厳密。理想指数は ( はオペランドの指数) |
exp | ||
ln | ||
log10 | 10 の冪 | 整数 ( が 桁を超える桁をもつ場合は丸められる) |
| 関数 | オペランド | 結果 |
|---|---|---|
sqrt, ln, log10 | 負の数 | 無効 |
ln, log10 | 。サブセットコンテキストでは無効 | |
exp | ||
| 四つすべて |
無効な結果は InvalidOperation を発生させます。このシグナルとして扱われる条件は GdaFlags::new に挙げてあります。
exp、ln、log10 には処理系限界があります。評価が精緻化の限界の範囲内で丸めを決定できないときは、結果は InvalidOperation を伴う NaN です。
GDA 1.70 は、exp、ln、log10 の不正確な結果が、正しく丸められた結果から最終桁で最大 1 単位まで離れることを許しています(GDA 1.70、30 ページ(exp)、31 ページ(ln, log10))。このライブラリは、サブセットコンテキストでも拡張算術と同じく、正しく丸められた結果を返します(適合性ページを参照)。
power
power(x, y) について を返します。
pub fn power(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
整数 の場合、結果は 桁の作業桁(サブセットコンテキストでは 1 桁少ない)による二進冪乗で計算され、その後コンテキストの丸めで丸められます。 桁に収まる厳密な冪は、指数 で厳密に返されます。
非整数の の場合、 は正でなければならず(負の底は無効)、結果はコンテキストの丸めで正しく丸められていることが認証されます。 の場合は、コンテキストの丸めでの平方根として計算されます。非整数の指数には処理系限界が適用されます。底が有限の場合、非整数の指数で、底の調整指数が 999,999 を超えるか を下回るもの、または指数の調整指数が を下回るもの、および調整指数が 999,999 を超える整数の指数は、InvalidContext ではなく InvalidOperation を伴う NaN を返します。
特殊な場合は GDA に従います。
| ケース | 結果 |
|---|---|
| 。 は無効 | |
| または の冪 | 整数の指数の偶奇から符号を決める |
| 、 が整数でない、または無限大 | Inexact と Rounded を伴う |
| サブセットコンテキスト、 | 1 |
| サブセットコンテキスト、 | ではなく無効 |
GDA は、実数 が表現可能な場合でも、非整数のべきをすべて不正確として扱います。精度 5 の power(4, 1.5) は、どの丸めアルゴリズムでも 8.0000 で、Inexact と Rounded が立ちます。このような場合、すなわち指数が既約な で、 が完全な 乗数である場合は、精緻化ループの前に決定されます。これらは方向付き丸めセルの境界上または中点上にあり、ループでは認証できないためです。
GDA 1.70 は、不正確な power の結果に最終桁 1 単位までの誤差を許しています(GDA 1.70、35 ページ(power))。このライブラリは常に正しく丸められた結果を返すため、Down、Floor、ZeroFiveUp では、libmpdec が 7.9999 を返すところで 8.0000 を返します。
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 の結果です。これらの場合とは、定義域エラー、signaling NaN、およびサポートされる指数範囲を超えるコンテキストです。後者は exp とその仲間では invalid_context として報告されます(処理系限界)。Err は、精緻化の上限または資源上の上限で計算が打ち切られたことを表します。そのとき通常の関数は invalid_operation を伴う NaN を返します。
reduce
値をコンテキストに合わせて丸め、末尾のゼロを除きます。
pub fn reduce(Decimal, GdaContext) -> GdaOutcome[Decimal]
ゼロは指数 0 の になり、拡張コンテキストでは符号を保ちます。クランプされたコンテキスト(パラメーター clamp、GdaContext::new を参照)では、指数が より大きくされることはありません。
scaleb, logb
scaleb は値を 10 のべきでスケールし、logb はその調整後指数を返します。
pub fn scaleb(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn logb(Decimal, GdaContext) -> GdaOutcome[Decimal]
scaleb(x, n) は、指数に を加えることで を返します。オペランド は指数 0 の整数で、 でなければなりません。そうでなければ結果は無効です。結果はそのあと共通の最終化を通ります。
| ケース | 結果 | 例 |
|---|---|---|
| 桁より長い係数 | 丸められる | |
| オーバーフロー | 丸めアルゴリズムに応じて、 または最大の有限数 | decimal32 の Down では、scaleb(9E+96, 1) は 9.999999E+96 です |
| 結果がゼロ | 指数範囲にクランプされる | decimal64 の scaleb(0E+300, 400) は 0E+369 です |
logb(x) は を整数として返します。logb(0) は DivisionByZero を伴う で、logb(±∞) は です。
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 はコンテキストにおいて より大きい最小の表現可能な数を、next_minus はそれより小さい最大の数を返します。ゼロからは へ移動し、最大の有限数を超えると に達します。どちらも有限の結果に対してはフラグを立てません(GDA 1.70、34 ページ)。サブセットコンテキストでは、正規化数の範囲より下へのステップは になり(非正規化数の結果は認められません)、このときもフラグは立ちません(#770)。
next_toward(x, y) は から に向かって 1 ステップ移動し、両者が等しく比較されるときは を返します(ゼロなら の符号を付けます)。ステップが正規化数の範囲を出るときは、Overflow、または Underflow と Subnormal を、Inexact と Rounded とともに発生させます(35 ページ:無限小を加えた和のフラグ)。サブセットコンテキストでは、正規化数の範囲より下へのステップは Underflow、Subnormal、Inexact、Rounded を伴う になります。
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]
各オペランドはちょうど 桁として読まれます。短いオペランドは先頭をゼロで埋め、長いオペランドは下位 桁だけが使われます。logical_invert は 桁すべてを反転します。それ以外のオペランドは結果を無効にします(InvalidOperation 条件)。
shift, rotate
の係数の桁を 桁の窓の中で 桁移動します(GDA 1.70、49 ページ(shift)、47 ページ(rotate))。
pub fn shift(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
pub fn rotate(Decimal, Decimal, GdaContext) -> GdaOutcome[Decimal]
は指数 0 で の整数でなければならず、そうでなければ結果は無効です。正の は桁を左へ移動します。shift は窓からはみ出た桁を捨ててゼロで埋め、rotate はそれを反対側の端へ回り込ませます。指数と符号は変わらず、無限大の はそのまま返されます。
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 | 値による十進の 、、(、) | quiet NaN。InvalidOperation は signaling NaN の場合だけ |
compare_signal | compare と同じ | quiet NaN。どの NaN に対しても InvalidOperation |
compare_total は GDA の全順序で 、、 を返します。順序はまず符号ビットで決まります。正の値については の順で、有限値は値、次いで指数で(2.50 < 2.5)、NaN はペイロードで順序付けられます。負の値では順序が逆になります。compare_total_magnitude は絶対値に全順序を適用します。これは 2 つの絶対値に対する 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]
| オペランド | 結果 |
|---|---|
| 一方が quiet NaN で、他方のオペランドは NaN でない | NaN でないほうのオペランド |
| 二つの quiet NaN | 最初のもの |
| signaling NaN | InvalidOperation を伴う quiet NaN |
| 等しく比較される値、拡張コンテキスト | 全順序が選ぶオペランド(したがって max(2.5, 2.50) は 2.5) |
| 等しく比較される値、サブセットコンテキスト | 最初のオペランド |
選ばれたオペランドは、そのあと plus と同様にコンテキストに合わせて丸められます。ただし拡張コンテキストではゼロが符号を保ちます。max(-0, -0) はどの丸めアルゴリズムでも -0 ですが、plus(-0) は丸めが Floor でない限り 0 です。
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]
これらはコンテキストの指数の下限 を必要とするため、コンテキストを取る関数です。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 は有限値と無限大を として数値的に順序付け、すべての NaN を他のすべての NaN と等しく、NaN でないすべての値より上に置くため、全前順序であり、ソートが中断することはありません。compare_checked は、どちらかのオペランドが NaN のとき 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 つの NaN | 真 |
<、<=、>、>= は compare に従います。
Decimal::compare_total, Decimal::compare_total_magnitude
GDA の全順序により、、、 のいずれかを返します。
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
丸めずに値を に制限します。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]
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 を実行し、その値を返します。作業コンテキストのパラメータは次のとおりです。
| パラメータ | 値 |
|---|---|
| 精度 | 、2 つのオペランドの精度のうち大きい方 |
| 丸め | 偶数への丸め |
| 算術 | 拡張 |
| clamp | なし |
| トラップ | なし |
| 指数範囲 | 下記のもの |
したがって結果は、GDA が定める理想指数を持つコホートの要素です(GDA の演算 を参照)。
| 演算 | 結果の指数 | 例 |
|---|---|---|
+, - | オペランドの指数のうち小さい方 | 1.10 + 3.50 は 4.60 |
* | 指数の和 | |
/ | 指数の差。ただし、厳密な商が許す範囲の末尾のゼロだけを除く | 1.5 / 0.5 は 3、1.50 / 0.5 は 3.0 |
4 つの演算のいずれでも、結果が 桁を超える場合は 桁に丸められます(このとき * の積は丸められ、厳密ではありません)。
x + 0 == x と x * 1 == x が格納されたすべての有限値で成り立つのは、等しさが値を比較するもので、コホートの要素を比較するのではなく、かつ下の作業指数範囲が格納される値の指数に対する処理系限界だからです。
GdaContext::new では作業指数範囲を作れません。この関数はコンテキストの を 以下に、 を 以上に制限しており(GDA 1.70、10 ページ(Abstract representation of numbers)は、値を示さずに上限 Elimit を認めています)、作業範囲は Int 全体を占めるからです。どの でも、最大の調整指数は 、最小の指数は なので、 は常に有効な Int の指数です。範囲を上回る結果は符号付きの無限大になり(1E+1500000000 * 1E+1500000000 は inf)、下回る結果は half-even の丸めアルゴリズムで に丸められ、符号付きのゼロになることがあります。
特殊値は、IEEE 754-2019 の 6.2(NaN を伴う演算)、7.2(無効演算)、7.3(ゼロ除算)の既定の結果に従い、シグナルは出しません。
| オペランド | 結果 |
|---|---|
| NaN のオペランド | quiet NaN |
| , , , | NaN |
| 、 はゼロでなく NaN でもない | 符号付きの無限大 |
| 符号付きのゼロ |
別の精度、丸め、範囲、あるいはシグナルが必要なときは、GdaContext を渡して自由関数を使ってください。
単項 - は Decimal::neg です。符号ビットを反転するだけで、コンテキストを使いません。上の作業コンテキスト、精度 、コホートの規則、作業範囲は +、-、*、/ にだけ当てはまります。
Decimal::sqrt
sqrt は、素の演算子の作業コンテキストで、その値の精度で平方根を求めます。
pub fn Decimal::sqrt(Self) -> Self
負のオペランドは、自由関数 sqrt と同じく NaN を返します。検査付きの形は 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 の 3 つの十進交換形式 decimal32、decimal64、decimal128 のうち 1 つを選びます。
pub(all) enum GdaInterchangeFormat {
Decimal32
Decimal64
Decimal128
} derive(Eq)
GdaInterchangeFormat::context
context は、その形式の拡張デフォルトの GdaContext を返します。精度は 7、16、34、丸めは half-even、クランプ有効です。
pub fn GdaInterchangeFormat::context(Self) -> GdaContext
コンテキストは、プリセット GdaContext::decimal32()、decimal64()、decimal128() です。
GdaInterchange
GdaInterchange は、densely packed decimal(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 は、ペイロードを保てない変換が言語定義の診断情報を持つ quiet NaN を返すことを求めており、シグナルは指定していません。
GdaInterchange::from_hex, GdaInterchange::to_hex
from_hex は 16 進テキストからビットパターンを解析し、to_hex はそれを出力します。
pub fn GdaInterchange::from_hex(String, GdaInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn GdaInterchange::to_hex(Self) -> String
テキストは #(入力では省略可)に続くちょうど 8、16、または 32 桁の 16 進数字です。前後の空白は無視されます。それ以外の入力は 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 は 2 つのパターンの形式が異なると中断します。
///|
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")
}
トレイト実装
列挙型とフラグ集合の導出された Eq
GdaRoundingMode、GdaSignal、GdaFlags、GdaTrapSet、GdaInterchangeFormat には導出された Eq メソッドがあり、2 つの値が等しいかどうかを判定します。
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 の contextual トレイト
Decimal は、パッケージの非公開エンジンの上に Luna-Flow/arithmetic の contextual トレイトを実装します。各演算は ArithmeticContext のもとで実行され、outcome またはエラーを返します。
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_context | Err(unsupported) |
division_by_zero | Err(division_by_zero) |
| それ以外のエラーフラグ | Err(domain_error) |
| 上のいずれでもない | Ok。値と、診断情報 inexact、rounded、overflow、underflow、subnormal、clamped を持ちます |
丸めは、ArithmeticContext の 5 つの丸めアルゴリズムのいずれかです。sqrt_contextual と exp_contextual は、GDA の関数のような half-even ではなく、この丸めアルゴリズムで丸めます。
exp_contextual には、指数の上下限を設定した ArithmeticContext が必要です。パッケージ関数 exp と同じく、処理系限界の対象で、精度、、 はいずれも 以下でなければなりません。e_min や e_max を持たない ArithmeticContext には、既定の上下限が補われません。全範囲 に変換され、これは exp にとって無効なコンテキストです。
そのようなコンテキストの結果は、オペランドによらず(-Infinity も含めて)Err(unsupported) です。メッセージは 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
をコンテキストの精度、、 をその指数の限界とすると、次のようになります。
| メソッド | 返り値 |
|---|---|
epsilon_contextual | |
min_normal_contextual | |
max_finite_contextual |
ArithmeticContext が無効な場合、つまりエンジンのコンテキストに変換できない場合(たとえば精度が 999,999,999 を超えるとき)、これら 3 つのメソッドは、コンテキストの精度を 1 から 999,999,999 の範囲に制限した精度のゼロを返します。
Luna-Flow/arithmetic の checked トレイト
Decimal は Luna-Flow/arithmetic の checked トレイトを実装し、フラグの代わりに 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 は、厳密に変換した整数の指数(少なくとも 10 桁で、受け付けるすべての指数を収められます)で冪演算を呼び出します。そのため、精度 7 では (-1).pow_int_checked(12345679, ctx) は -1 です。次の場合はエラーを返します。
| ケース | 結果 |
|---|---|
| 底がゼロで指数が負 | Err(division_by_zero) |
power が invalid_context を発生させる場合。指数の上下限が 以内でないコンテキストで、999,999,999 を超える正の指数を使ったとき | invalid context: メッセージ付きの Err(unsupported) |
| その他の無効な結果 | Err(domain_error) |
999,999,999 を超える指数での pow_nat_checked | Err(unsupported) |
DivChecked::div_checked は、与えられたコンテキストのもとで除算し、除数がゼロなら Err(division_by_zero) を、無効な除算なら Err(domain_error) を返します。
FromNat、FromInteger と luna-generic の代数トレイト
Decimal は luna-generic のトレイトを実装しており、ジェネリックな代数のコードから使えます。
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 にこれらの名前の固有メソッドはないので、トレイトのメソッドか、それらが計算している 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 です。前の 3 つは Decimal::classify で、後の 2 つは 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