decimal API
Purpose
Luna-Flow/floating/decimal is the IEEE 754-2019 decimal floating-point
package of floating. A Decimal is an arbitrary-precision decimal value
that keeps its quantum (exponent), its sign of zero and its NaN payload; a
DecimalContext is an IEEE 754-2019 format (a precision and an exponent range)
with one rounding-direction attribute; every context operation returns the rounded value together with the
DecimalFlags it raised. The package also encodes and decodes the
decimal32/64/128 interchange formats in both DPD and BID, and evaluates
elementary functions with certified rounding.
The decimal tutorial walks through typical tasks and
the decimal design explains the arithmetic model,
encodings, rounding and certification. The finite evidence for the IEEE claim
is recorded in decimal conformance. Sticky General
Decimal Arithmetic status and traps live in the separate
decimal_gda package; decimal_checked
accumulates the flags of a pipeline of Decimal operations.
Importing
Add the package to your moon.pkg:
import {
"Luna-Flow/floating/decimal",
}
The examples call the package through the alias @decimal.. Some of them also
use @def. for Luna-Flow/floating/def (the shared RoundingMode and Sign),
@lf_arith. for Luna-Flow/arithmetic, @lf_alg. for
Luna-Flow/luna-generic and @bigint. for moonbitlang/core/bigint; import
those packages too when you copy such an example. Most examples build values
with a local helper d(s), which is @decimal.Decimal::from_string(s).unwrap().
Notation
Notation used below: a finite value is with sign , non-negative integer coefficient and exponent (quantum) ; is the context precision, and the context exponent limits for the adjusted exponent , and the smallest exponent of a subnormal.
Values and representation
Decimal
Decimal is an immutable decimal floating-point value.
pub struct Decimal {
// private fields
} derive(@debug.Debug)
A Decimal is one of: a finite value (including ),
, or a quiet or signaling NaN with a sign and a non-negative integer
payload. Every value also carries a working precision, used by the plain
operators and by conversions that have no context argument. The fields are
private; use the observers below. Two values with the same mathematical value
but different exponents (for example 1.2 and 1.20) are different members of
the same cohort: they compare equal numerically but are distinguished by
quantum, same_quantum, total_order_compare, formatting and interchange
encoding.
The derived Debug implementation is promoted as Decimal::to_repr; see
Trait implementations.
Decimal::precision, coefficient, exponent10, quantum
These observers return the stored representation of a value.
pub fn Decimal::precision(Self) -> Int32
pub fn Decimal::coefficient(Self) -> @bigint.BigInt
pub fn Decimal::exponent10(Self) -> Int32
precision is the working precision stored in the value: the precision of a format a context could have, 1..=999999999 (decimal_precision_max). Every constructor that returns a Decimal aborts on a precision outside that range, naming the function; the operations that return a Result return Err.
coefficient returns the non-negative coefficient ;
for a NaN it returns the payload and for an infinity 0. The sign is never
part of the coefficient: use is_sign_minus. exponent10 returns the stored
exponent ; for special values it is 0. The IEEE 754-2019 operation
quantum is quantum_ctx (below), not an accessor.
///|
test "decimal representation observers" {
let x = @decimal.Decimal::from_string("-12.300").unwrap()
inspect(x.coefficient(), content="12300")
inspect(x.exponent10(), content="-3")
inspect(x.is_sign_minus(), content="true")
inspect(x.precision(), content="34")
}
Decimal::signum, is_sign_minus
These observers report the sign of a value.
pub fn Decimal::signum(Self) -> @def.Sign
pub fn Decimal::is_sign_minus(Self) -> Bool
signum returns @def.Sign::Zero for both zeros and for every NaN, and
Negative/Positive otherwise. is_sign_minus returns the stored sign bit, so it
is true for , for and for a negative NaN.
Decimal::classify, class_of
classify returns the coarse class of a value; class_of returns the ten-valued
class of IEEE 754-2019 clause 5.7.2 under a context.
pub fn Decimal::classify(Self) -> @arithmetic.FpClass
pub fn Decimal::class_of(Self, DecimalContext) -> @def.IeeeClass
classify returns Finite, Infinity or NaN. class_of returns one of the
ten IeeeClass values; the normal/subnormal split uses the
context’s . It has no text form: match on the IeeeClass (the
former class_name strings were "sNaN", "NaN", "+Zero" and so on).
Decimal::is_finite, is_infinite, is_nan, is_zero, is_negative_zero, is_quiet_nan, is_signaling_nan, is_canonical, is_normal, is_subnormal
These predicates test the class of a value.
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_negative_zero(Self) -> Bool
pub fn Decimal::is_quiet_nan(Self) -> Bool
pub fn Decimal::is_signaling_nan(Self) -> Bool
pub fn Decimal::is_canonical(Self) -> Bool
pub fn Decimal::is_normal(Self, DecimalContext) -> Bool
pub fn Decimal::is_subnormal(Self, DecimalContext) -> Bool
is_canonical always returns true:
a Decimal has no non-canonical form; non-canonical encodings are a
property of DecimalInterchange. A value is normal
under a context when it is finite, non-zero and its adjusted exponent is at
least ; it is subnormal when it is finite, non-zero and its adjusted
exponent is below . Zeros, infinities and NaNs are neither.
Decimal::nan_payload, get_payload, set_payload, set_payload_signaling
These functions read and replace the payload of a NaN.
pub fn Decimal::nan_payload(Self) -> @bigint.BigInt
pub fn Decimal::get_payload(Self) -> @bigint.BigInt
pub fn Decimal::set_payload(Self, @bigint.BigInt) -> Self
pub fn Decimal::set_payload_signaling(Self, @bigint.BigInt) -> Self
nan_payload returns the payload of a NaN and 0 for every other value.
get_payload is IEEE 754-2019 §9.7 getPayload: the payload of a NaN, and -1
for any other value. set_payload and set_payload_signaling are setPayload
and setPayloadSignaling: when the argument is a non-negative integer of at most
precision - 1 digits (the receiver’s precision, like the trailing
significand of an interchange NaN) the result is a quiet (respectively
signaling) NaN with that payload and the receiver’s sign bit; otherwise it is
+0 with exponent 0. The result does not depend on whether the receiver is a
NaN.
Construction and conversion
decimal_value_e_max, decimal_value_e_tiny
The limits of a stored finite value.
pub let decimal_value_e_max : Int
pub let decimal_value_e_tiny : Int
A finite Decimal holds only a value with exponent + digits - 1 <= decimal_value_e_max (the largest adjusted exponent, ; a
zero has one digit) and exponent >= decimal_value_e_tiny (the smallest
exponent, ). They are the e_max and the etiny of the
private working context of the plain operators, the largest and smallest the
32-bit exponent field allows once e_max, e_min and etiny = e_min - p + 1
have to be representable in it, so that x + 0 == x, x - 0 == x and
x * 1 == x hold for every stored value. The exact constructors (make,
try_make, parse, from_string) refuse a value outside the range; the
constructors from integers, floats and binary floats cannot produce one; an
operation that rounds (the plain operators, with_precision) overflows to an
infinity or the largest finite value of the rounding direction, or underflows
to the smallest exponent, like a context result. These are not the limits of a
context (decimal_context_e_max_limit, below): a context is narrower than
what can be stored, and a value outside every context range can still be
stored and is rounded when a context operation reads it.
Decimal::make, try_make
make builds a finite value from a signed integer coefficient and an exponent,
rounding it to a precision; try_make is its Result form.
pub fn Decimal::make(@bigint.BigInt, Int, Int, mode? : DecimalRoundingMode) -> Self
pub fn Decimal::try_make(@bigint.BigInt, Int, Int, mode? : DecimalRoundingMode) -> Result[Self, @arithmetic.ArithmeticError]
Decimal::make(c, q, p, mode~) represents ; the sign comes
from c. A coefficient of at most p digits keeps the exponent q (the cohort
member asked for is not reduced: make(100, 0, 5) is 100, make(0, 5, 5) is
0E+5); a longer one is rounded to exactly p digits with mode (default
RoundTiesToEven) and not reduced (make(1000000, 0, 3) is 100E+4). No
context exponent range applies, but the value must be within the stored
range, checked after the rounding: make aborts
naming the exponent and both limits, try_make returns the domain error (and an
error for a p outside 1..=999999999).
Decimal::zero, negative_zero, one, inf, nan, quiet_nan, signaling_nan
These constructors build the special and unit values.
pub fn Decimal::zero(precision? : Int) -> Self
pub fn Decimal::negative_zero(precision? : Int) -> Self
pub fn Decimal::one(precision? : Int) -> Self
pub fn Decimal::inf(negative? : Bool, precision? : Int) -> Self
pub fn Decimal::nan(precision? : Int) -> Self
pub fn Decimal::quiet_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int) -> Self
pub fn Decimal::signaling_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int) -> Self
The default precision is 34. zero, negative_zero and one have exponent
0. inf(negative=true) is and inf() is . nan is
a positive quiet NaN with payload 0; quiet_nan and signaling_nan default
to payload 0 and a positive sign. The payload is the trailing significand field
(IEEE 754-2019 3.5.2): a non-negative integer of at most precision - 1 digits,
otherwise the constructor aborts; a conversion to a narrower format keeps the
low digits of the payload, construction never truncates. The precision must be
within 1..=999999999.
Decimal::from_int, from_bigint
These constructors convert an integer.
pub fn Decimal::from_int(Int, precision? : Int) -> Self
pub fn Decimal::from_bigint(@bigint.BigInt, precision? : Int) -> Self
Both implement convertFromInt (IEEE 754-2019 §5.4.1, preferred exponent 0)
with the default precision 34: an integer of at most precision digits keeps
exponent 0, so from_int(1000) is 1000 and not the reduced 1E+3; a longer
one is rounded half-even to exactly precision digits (§5.2), so
from_int(99999, precision=3) is 1.00E+5. Zero is with exponent 0.
Decimal::from_double, from_float
These constructors convert a binary floating-point number.
pub fn Decimal::from_double(Double, precision? : Int) -> Self
pub fn Decimal::from_float(Float, precision? : Int) -> Self
Every finite Double is a dyadic rational and therefore has a
finite decimal expansion for ; the
conversion forms that exact expansion and, when it fits in precision digits
(default 34), returns it with the exponent closest to the preferred exponent 0
(from_double(0.5) is 0.5, from_double(100.0) is 100). Otherwise it is
rounded half-even to exactly precision digits, the least possible exponent
(IEEE 754-2019 §5.2): from_double(0.1, precision=16) is 0.1000000000000000.
Signed zeros and infinities are preserved; every NaN
becomes a quiet NaN with payload 0 and the input’s sign. from_float widens
to Double first, which is exact.
Decimal::from_binary, to_binary
These functions convert between Decimal and the binary
Binary.
pub fn Decimal::from_binary(@binary.Binary, precision~ : Int, mode? : DecimalRoundingMode) -> Self
pub fn Decimal::to_binary(Self, precision~ : Int, mode? : @binary.BinaryRoundingMode) -> @binary.Binary
from_binary(x, precision~, mode~) is exact whenever the decimal expansion of
x fits in precision decimal digits, with the exponent closest to 0 as for
from_double, and is otherwise rounded once to exactly precision digits in
mode (default RoundTiesToEven, the rounding type of the radix of the result).
The precision is required and counts digits; it was the number of bits of x
by default, a count of the wrong kind. A binary zero keeps its sign (
becomes , as IEEE 754-2019 §5.4.2 and §6.3 require), and a NaN becomes a
quiet NaN with payload 0. to_binary(precision~, mode~) rounds the exact
decimal value once to a Binary of precision bits with mode, one of the
five rounding-direction attributes of IEEE 754-2019 4.3.1 (default
RoundTiesToEven). The precision is required and counts bits, from 1 to
@binary.binary_precision_max; outside that range the call aborts (it was the
number of digits of the decimal by default). Most decimal fractions are not
dyadic, so this direction is usually inexact; converting with RoundTowardNegative
and RoundTowardPositive gives a binary enclosure of the decimal value. Zeros
keep their sign and NaN payloads are not kept. The same conversions are in
decimal_gda, with GdaRoundingMode for the decimal result.
Decimal::convert_format, from_binary_ctx, to_binary_ctx
These functions are IEEE 754-2019 convertFormat (§5.4.2) with the flags, for the conversions between decimal formats and between the radices.
pub fn Decimal::convert_format(Self, DecimalInterchangeFormat) -> (Self, DecimalFlags)
pub fn Decimal::from_binary_ctx(@binary.Binary, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::to_binary_ctx(Self, @binary.BinaryContext) -> (@binary.Binary, @binary.BinaryFlags)
convert_format(format) converts to decimal32, decimal64 or decimal128: the
value is rounded once, to nearest even, to the precision and exponent range of
the format, with the flags of that rounding. The preferred exponent is the
quantum of the source (“For conversions between decimal formats, the preferred
exponent is Q(source)”): a conversion to a wider format is exact and keeps the
cohort member, a narrower one rounds, can overflow or underflow, and folds a
large exponent down.
from_binary_ctx(x, ctx) rounds the exact value of a binary float once
under the decimal context (precision, exponent range, direction) with the flags
of the rounding: an exact conversion has the preferred exponent 0 (the
exponent closest to 0 that holds the digits: 0.5 is 5E-1, 96 is 96), an
inexact one has precision digits (“the preferred exponent is the least
possible”). A value beyond the exponent range, or far below it, overflows or
underflows without its digits being formed. An in-range value is converted at
any binary exponent, correctly rounded, at a cost that does not grow with the
exponent; a binary exponent beyond is refused with NaN and
invalid_operation. to_binary_ctx(ctx) rounds the exact decimal value once
under a BinaryContext (binary16 to binary128, or any precision and range)
and returns the BinaryFlags (inexact, overflow, underflow); it is the
conversion of Binary::from_string_ctx, correctly rounded for any exponent.
A zero keeps its sign in every direction. A signaling NaN gives a quiet NaN
and the invalid operation exception; the sign of a NaN is kept, and so is its
payload when the destination holds it (§6.2.3): p - 1 digits for the decimal
formats and p - 2 bits for a binary format, otherwise the low digits or bits.
from_binary and to_binary are unchanged.
The FromNat and FromInteger implementations
Decimal implements the luna-generic traits FromNat and FromInteger, the
canonical maps out of the naturals and the integers. There are no Decimal::from_natural
or Decimal::from_integer methods: call Decimal::from_bigint(n) (exponent 0,
the cohort of the integer) or the trait functions.
The trait functions return from_bigint(n).normalized() with precision 34, that
is, with the trailing zeros removed. To convert another Luna-Flow integer type,
pick its representative with Integral::normalize first, or use lift_to.
Parsing and formatting
Decimal::from_string
from_string converts decimal text to a Decimal while keeping its quantum.
pub fn Decimal::from_string(String, precision? : Int) -> Result[Self, @arithmetic.ArithmeticError]
The accepted syntax is an optional sign, digits with an optional decimal
point, an optional exponent E/e with optional sign, or one of Inf,
Infinity, NaN and sNaN (any letter case) followed for NaNs by optional
decimal payload digits. Conversion rounds once, half-even, to precision
(default 34), while preserving the preferred exponent as far as the precision
and representation permit. For example, "1.2300" has coefficient 12300
and exponent ; "1.0001" at precision 3 rounds to 1.00, and the exact
"1.000" at precision 3 is 1.00 because its four coefficient digits do
not fit. "10001" at precision 3 rounds to 1.00E+4. Invalid text returns
Err(parse_error).
No context exponent range is applied; the limits are those of the
stored range: the exponent of the
value must be at least decimal_value_e_tiny () and its
adjusted exponent at most decimal_value_e_max (), checked
after the rounding to precision (a carry included) and for a zero as for any
finite value. A text whose value is outside them is refused, never replaced:
from_string returns the domain error, which names the
exponent and both limits ("1e3000000000", "12e2147483647", "0e-2147483646"
are refused; "1249e-2147483646" at precision 3 is 1.25E-2147483643, its
exponent after the rounding being inside the range). Read the text with
from_string_ctx when a rounded value (an
infinity, a zero) and the overflow and underflow flags are wanted.
Decimal::from_string_ctx
from_string_ctx converts text under a context and reports conversion flags.
pub fn Decimal::from_string_ctx(String, DecimalContext) -> (Self, DecimalFlags)
The syntax is the one of from_string. The value is rounded to the context
precision (raising inexact when non-zero digits are
discarded), checked against the exponent range (overflow, underflow)
and folded down. A NaN payload with more digits than the
format allows (precision digits) is a syntax error. Invalid text returns a
quiet NaN with invalid_operation.
///|
test "decimal from_string_ctx rounds and flags" {
let ctx = @decimal.DecimalContext::decimal32()
let (x, flags) = @decimal.Decimal::from_string_ctx("3.14159265", ctx)
inspect(x, content="3.141593")
inspect(flags.inexact(), content="true")
let (bad, bad_flags) = @decimal.Decimal::from_string_ctx("1..2", ctx)
inspect(bad.is_nan(), content="true")
inspect(bad_flags.invalid_operation(), content="true")
}
Decimal::to_sci_string, to_eng_string
These functions implement the General Decimal Arithmetic to-scientific-string
and to-engineering-string conversions of a value.
pub fn Decimal::to_sci_string(Self) -> String
pub fn Decimal::to_eng_string(Self) -> String
Both format the value they are called on; to convert text under a context,
read it with from_string_ctx first, which also gives the flags of the
conversion (Decimal::from_string_ctx("1000000000", ctx).0.to_sci_string()).
Scientific form writes the coefficient with an exponent E±n whenever the
exponent is positive or the adjusted exponent is below , and plain digits
otherwise; engineering form uses an exponent that is a multiple of three.
Special values are written Infinity, -Infinity, NaN, sNaN, with a
payload such as NaN7.
Values without a context are formatted by Decimal::to_string (the Show
implementation), which uses the same scientific rule; see
Show and Debug.
Contexts
DecimalContext
A DecimalContext is an immutable set of arithmetic parameters.
pub struct DecimalContext {
// private fields
} derive(Eq)
A context is the IEEE 754-2019 format of a precision and an exponent range
with (§3.3), and one
rounding-direction attribute (rounding, a DecimalRoundingMode). Every finite
result of a context operation is a member of the format: its exponent
satisfies , so
(large exponents are folded down, as the format requires), and tininess is
detected before rounding (§7.5). There is no option for any of this. No
operation reads ambient state: every context operation receives its context as
an argument.
decimal_context_e_max_limit, decimal_context_e_min_limit, decimal_precision_max
The limits of the context parameters that DecimalContext::try_new accepts.
pub let decimal_context_e_max_limit : Int
pub let decimal_context_e_min_limit : Int
pub let decimal_precision_max : Int
They are , (, the smallest
of a context) and , the decNumber decContext.h
limits (decimal_context_e_min_limit was before 0.12).
try_new and new check precision and e_max against these constants
(precision in 1..=decimal_precision_max, e_max in
1..=decimal_context_e_max_limit), so the values cannot drift from the
validation. They bound the context only: the certified elementary functions
have their own, lower limit ( and ), which is
documented in Elementary functions.
DecimalContext::new, try_new
new builds the context of an IEEE 754-2019 decimal format from a precision and
an e_max and aborts on an invalid one; try_new takes the same parameters and
returns a Result.
pub fn DecimalContext::new(precision? : Int, e_max? : Int, rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::try_new(precision? : Int, e_max? : Int, rounding? : DecimalRoundingMode) -> Result[Self, @arithmetic.ArithmeticError]
| Parameter | Default | Meaning |
|---|---|---|
precision | 34 | coefficient digits |
e_max | the largest exponent ; is | |
rounding | RoundTiesToEven | rounding-direction attribute (IEEE 754-2019 §4.3) |
precision must be in 1..=999,999,999 and e_max in 1..=999,999,999. The
default of e_max follows IEEE 754-2019 §3.7: an extendable format given by its
precision alone needs from 51 digits on, and 999,999 is
the widest range in which every operation of the package runs. new() is
therefore the format , not decimal128: use decimal128() for
that. A precision above 999,999 has no default range (1000 exceeds the
limit): pass e_max. try_new returns Err(domain_error) naming the invalid
field; new aborts with a message that names it, like BinaryContext::new, so
use try_new for parameters that come from outside. NOTE 3 of §3.7 says that
should be at least 2; the library accepts 1, the least value for which
(§3.3).
There is one rounding parameter: finite results, infinities and every other
use of the context round by it, and rounding() reports exactly the mode that
is applied. The commercial rounding of GDA (round-half-up) is
RoundTiesToAway; the GDA modes round-half-down,
round-up and round-05up are not attributes of an IEEE 754-2019 decimal
format and exist only in decimal_gda. A context has no
clamp, extended or tininess setting and no unlimited precision (exact()
is gone): an IEEE format always has the exponent range of its quantum, the
subset arithmetic of the General Decimal Arithmetic appendix is not an IEEE
format, and exact results are obtained with a precision large enough for the
digits (a context of digits(a) + digits(b) digits and an e_max of at least
the exponent of the product makes mul_ctx exact).
DecimalContext::decimal32, decimal64, decimal128
These constructors return the contexts of the IEEE 754-2019 interchange formats (§3.6).
pub fn DecimalContext::decimal32(rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::decimal64(rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::decimal128(rounding? : DecimalRoundingMode) -> Self
| Context | |||
|---|---|---|---|
decimal32 | 7 | 96 | |
decimal64 | 16 | 384 | |
decimal128 | 34 | 6144 |
All three use RoundTiesToEven by default (pass rounding~ for another
attribute).
DecimalContext::from_arithmetic_context
from_arithmetic_context converts the shared Luna-Flow context.
pub fn DecimalContext::from_arithmetic_context(@arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
Precision and rounding are copied; clamp is ignored (it is a property of every
IEEE format, and the arithmetic package’s default is false). A given e_max
is used; a given e_min must equal 1 - e_max or the conversion returns
Err(domain_error); e_min alone gives e_max = 1 - e_min; neither gives the
default range of new. The conversion also returns Err(domain_error) for
AwayFromZero (no attribute of §4.3) and when the precision or e_max is out of
range. GDA 1.70 itself sets no fixed limits; these are the adopted decNumber
decContext.h limits.
DecimalContext::precision, rounding, e_min, e_max
These accessors return the fields of a context.
pub fn DecimalContext::precision(Self) -> Int
pub fn DecimalContext::rounding(Self) -> DecimalRoundingMode
pub fn DecimalContext::e_min(Self) -> Int
pub fn DecimalContext::e_max(Self) -> Int
e_min is always 1 - e_max.
DecimalContext::refinement_limit, DecimalContext::with_refinement_limit
Read and set the refinement limit of the certified elementary functions.
pub fn DecimalContext::refinement_limit(Self) -> Int32?
pub fn DecimalContext::with_refinement_limit(Self, Int32?) -> Self
The certified elementary functions evaluate an enclosure at a growing working
precision until both ends round alike. The limit is the number of evaluations:
Some(n) allows n (a value below 1 counts as 1), the default is Some(12),
and None removes the count limit. When the limit is reached without a
decision, try_*_ctx returns the CertificationFailure error with the
refinement count, as before, and the plain and *_ctx forms return NaN with
invalid_operation. With None the loop stops when the working precision
passes a cap, the larger of 8,000,000 bits and 256 times the first working
precision. The default changes no result. The field takes part in ==: two
contexts that differ only in the limit are different.
///|
test "a refinement limit makes a hard-to-round result fail" {
let context = @decimal.DecimalContext::new(precision=34, e_max=999_999)
inspect(context.refinement_limit() == Some(12), content="true")
// exp(5E-34) lies just above the midpoint 1 + 5E-34 at 34 digits.
let x = @decimal.Decimal::from_string("5E-34", precision=34).unwrap()
let short = context.with_refinement_limit(Some(1))
inspect(x.try_exp_ctx(short) is Err(_), content="true")
let unlimited = context.with_refinement_limit(None)
inspect(x.try_exp_ctx(unlimited) is Ok(_), content="true")
}
DecimalContext::elementary_precision_limit, DecimalContext::with_elementary_precision_limit
Read and set the precision limit of the elementary functions and of the pow
family: the escape entry from an engineering limit.
pub fn DecimalContext::elementary_precision_limit(Self) -> Int32?
pub fn DecimalContext::with_elementary_precision_limit(Self, Int32?) -> Self
Some(n) refuses an ordinary operand in a context whose precision is above
n digits, at once (not_computed, Err in the try_ forms); the default is
Some(999_999), and None removes the limit. The default is a cap that the
library chose, not a property of the operations: above it the library does not
bound the running time or the memory, and that range is not covered by the
conformance gates, with no guarantee. The results come from the same certified
kernels, so a value that is returned is still correctly rounded. For example
pow(2, 0.5), a square root, took 0.6 s at 100,000 digits, 14.7 s at 1,000,000
and more than 300 s at 10,000,000 (release build, loaded machine). Where the
certified kernel cannot represent the working precision (above 80,807,093
digits) the functions that go through it return the context-limit error
whatever the limit; the square-root route of pow (exponent 0.5, sqrt_ctx)
and the exact routes are not refused there and run on. The cases that IEEE
754-2019 9.2.1 lists are values whatever the limit. The field takes part in
==, like the refinement limit, and with_precision, with_e_max and
with_rounding keep it.
///|
test "the precision limit of the elementary functions" {
let context = @decimal.DecimalContext::new(precision=101)
inspect(context.elementary_precision_limit() == Some(999_999), content="true")
let low = context.with_elementary_precision_limit(Some(100))
let two = @decimal.Decimal::from_int(2)
inspect(two.try_pow_ctx(@decimal.Decimal::from_int(3), low) is Err(_), content="true")
let open = low.with_elementary_precision_limit(None)
inspect(two.pow_ctx(@decimal.Decimal::from_int(3), open).0.to_string(), content="8")
}
DecimalContext::with_precision, with_e_max, with_rounding
These functions derive a context.
pub fn DecimalContext::with_precision(Self, Int) -> Self
pub fn DecimalContext::with_e_max(Self, Int) -> Self
pub fn DecimalContext::with_rounding(Self, DecimalRoundingMode) -> Self
with_precision replaces the precision and with_e_max the exponent range
(e_min follows); both abort outside the limits of new. with_rounding
replaces the rounding-direction attribute.
Rounding modes
DecimalRoundingMode
DecimalRoundingMode lists the five rounding-direction attributes of IEEE
754-2019 §4.3 that a decimal format has, in the spelling of BinaryRoundingMode.
pub(all) enum DecimalRoundingMode {
RoundTiesToEven
RoundTiesToAway
RoundTowardZero
RoundTowardPositive
RoundTowardNegative
}
Let lie strictly between two adjacent representable coefficients
and (in units of the last place). RoundTowardZero returns ;
RoundTowardPositive and RoundTowardNegative round toward and
(so they depend on the sign); RoundTiesToEven and
RoundTiesToAway return the nearer of the two and break an exact tie toward
the even coefficient and away from zero respectively. The kernels also have
three working modes that no context can select (HalfDown, Up and
ZeroFiveUp: the double-rounding guard digit of division and the directed
bounds of the certified functions); they are internal.
DecimalRoundingMode::from_arithmetic, to_arithmetic
These functions map between the attributes and the shared
@arithmetic.RoundingMode.
pub fn DecimalRoundingMode::from_arithmetic(@arithmetic.RoundingMode) -> Self?
pub fn DecimalRoundingMode::to_arithmetic(Self) -> @arithmetic.RoundingMode?
@arithmetic.RoundingMode | DecimalRoundingMode |
|---|---|
ToNearestEven | RoundTiesToEven |
TowardZero | RoundTowardZero |
TowardPositive | RoundTowardPositive |
TowardNegative | RoundTowardNegative |
AwayFromZero | none (from_arithmetic returns None) |
to_arithmetic returns None for RoundTiesToAway, which the shared enum
does not have. DecimalContext::from_arithmetic_context and the _contextual
forms return Err for AwayFromZero.
Tininess is detected before rounding in every context (IEEE 754-2019 §7.5: “For
decimal formats, tininess is detected before rounding”): a non-zero result whose
exact magnitude is below is tiny, whatever the delivered value. A
tiny result raises underflow when it is inexact. There is
no option for the binary-style after-rounding rule.
Status flags
DecimalFlags
DecimalFlags records the conditions raised by one operation.
pub struct DecimalFlags {
// private fields
} derive(Eq)
| Field | Raised when |
|---|---|
inexact | the result differs from the exact result |
underflow | the result is tiny and inexact |
overflow | the rounded result’s adjusted exponent exceeds |
division_by_zero | an exact infinite result from finite operands (, , , , , , , , , , ) |
invalid_operation | no usefully definable result (signaling NaN, , , , bad quantize, text that is not a number, a context outside the range an elementary function supports) |
not_computed | a result the library could not compute within a limit of the implementation (a context beyond the elementary kernels, an operand beyond their range, an exact sum with an exponent gap beyond the work bound, a certification that did not conclude): the value is a NaN, invalid_operation is not raised, and the try_ form returns Err (certification failure kind) |
These are the five flags of IEEE 754-2019 7.2 to 7.6 and not_computed. The General Decimal Arithmetic flags rounded, subnormal, clamped, lost_digits, conversion_syntax, division_impossible, division_undefined and invalid_context are not part of decimal; decimal_gda keeps them.
The fields are private. Each flag is read with a method of the same name, such as flags.inexact() or flags.division_by_zero(); the flags never accumulate implicitly.
DecimalFlags::new, combine, has_error
These functions create, merge and query flag sets.
pub fn DecimalFlags::new() -> Self
pub fn DecimalFlags::combine(Self, Self) -> Self
pub fn DecimalFlags::has_error(Self) -> Bool
new has every flag clear. combine is the field-wise OR, so it is
associative, commutative and idempotent with new() as identity. has_error is
the conditions for which IEEE 754-2019 delivers no usable number (7.2: invalid
operation is “no usefully definable result”). A failed from_string_ctx, which
raises invalid_operation, therefore counts as an error. has_error does not
include overflow, underflow or inexact: those deliver a usable result (IEEE 754-2019 7.4 to 7.6 define it, as 7.3 does for division by zero). division_by_zero is in the set only so that the checked forms of Luna-Flow/arithmetic can return Err for a division by zero.
///|
test "decimal flags accumulate by combine" {
let ctx = @decimal.DecimalContext::decimal64()
let one = @decimal.Decimal::one()
let three = @decimal.Decimal::from_int(3)
let (third, f1) = one.div_ctx(three, ctx)
let (_, f2) = one.div_ctx(@decimal.Decimal::zero(), ctx)
let all = f1.combine(f2)
inspect(third, content="0.3333333333333333")
inspect(all.inexact(), content="true")
inspect(all.division_by_zero(), content="true")
inspect(all.has_error(), content="true")
}
Plain operations without a context
The operators and the functions in this group take no context and return no flags. They are convenient for exact work and for generic code over the Luna-Flow algebra traits; use the context arithmetic whenever rounding, the exponent range or flags matter.
Decimal::add, sub, mul, div, neg
These functions are the arithmetic operators +, -, *, / and unary -.
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 fn Decimal::neg(Self) -> Self
Each operator is the matching context operation (add_ctx,
sub_ctx, mul_ctx, div_ctx) in a private working context with the
flags dropped: the format of the larger precision of the
operand precision fields, round half even, the storage range of a value
(decimal_value_e_max, , and decimal_value_e_tiny,
) and no fold-down. The result precision field is
. The checked
division is the trait method DivChecked::div_checked(a, b, ctx) (see
parse_checked and the checked traits); there is no inherent Decimal::div_checked.
- An exact result takes the cohort member closest to the preferred exponent
(IEEE 754-2019 5.4.1: for
addandsub, formul, fordiv):1.20 + 3.40is4.60,6.0 / 2is3.0,1.25 * 2.50is3.1250,1.20 - 1.20is0.00. - An inexact result has exactly digits (the least exponent, 5.2) and is not
reduced:
1 / 3at seven digits is0.3333333, a product of two five-digit values is rounded to five digits. The product used to be returned exact, with up to digits. negflips the sign bit of every value, including zeros and NaNs.
Special values follow the context operations: a NaN operand gives a quiet NaN
with the payload of the first NaN; , ,
and give a NaN; is an infinity with the sign of the
exact quotient (the sign of a zero counts: 1 / -0 is ); the sign of
a zero product is the exclusive or of the signs; finite is a signed
zero at the smallest exponent of the working context. A result beyond the
working range is an overflow of that context, an infinity, and a result below
it rounds half-even to the smallest exponent, possibly to a signed zero
(1E+2147483646 * 1E+2147483646 is inf, 123E-2147483640 * 1E-10 is
0E-2147483645). x + 0 == x, x - 0 == x and x * 1 == x hold for every
finite value inside that range, at any exponent: the sum or product is the
value, at the cohort member closest to the preferred exponent that holds it in
digits.
///|
test "decimal plain operators" {
let a = @decimal.Decimal::from_string("1.20").unwrap()
let b = @decimal.Decimal::from_string("3.40").unwrap()
inspect(a + b, content="4.60")
inspect(a * b, content="4.0800")
inspect(a - b, content="-2.20")
inspect(-a, content="-1.20")
inspect(@decimal.Decimal::one() / @decimal.Decimal::from_int(8), content="0.125")
}
Decimal::abs, neg, copy, copy_sign
These functions change only the sign bit.
pub fn Decimal::abs(Self) -> Self
pub fn Decimal::copy(Self) -> Self
pub fn Decimal::copy_sign(Self, Self) -> Self
They never round, never raise flags and keep exponent, payload and NaN kind.
abs clears the sign, neg flips it, copy_sign
copies the sign bit of the second operand and copy returns its argument.
Decimal::normalized, with_precision
These functions change the cohort member or the precision field.
pub fn Decimal::normalized(Self) -> Self
pub fn Decimal::with_precision(Self, Int, DecimalRoundingMode) -> Self
normalized returns the reduced member of the cohort (all trailing zeros
removed, zero with exponent 0), rounding half-even to the value’s own
precision if the coefficient is longer. It is the only way to strip trailing zeros: IEEE 754-2019 has no operation for it, so decimal no longer has reduce_ctx or trim (x.apply_ctx(ctx).0.normalized() is the former reduce_ctx).
with_precision(p, mode) rounds a finite value to p digits with mode,
reduces it and stores p as the precision field; special values only get the
new precision field. All of them are flag-free.
Decimal::sqrt
sqrt takes the square root of the value under its own format.
pub fn Decimal::sqrt(Self) -> Self
sqrt is sqrt_ctx in the working context of the plain
operators (precision ) and returns NaN for a negative non-zero
operand (IEEE 754-2019 7.2; sqrt_checked reports it as Err). Both are
correctly rounded half-even.
Context arithmetic
Every function in this group takes a DecimalContext and returns
(result, flags). The result is the exact result rounded once to the context
precision with the context’s decimal rounding mode, then checked against the
exponent range: overflow gives the rounding-mode-dependent result of the
design page, a result whose exact magnitude
is below is rounded directly to the subnormal grid
(a normal result is rounded only to digits, even
when its exact exponent is below ), and large
exponents are folded down to . When the exact result fits, the exponent is the
preferred exponent of the operation, so cohorts carry information. NaN
operands propagate as a quiet NaN with the first NaN’s sign and payload (the
payload is cut to its low digits, as in
from_string_ctx and the interchange encodings); any
signaling NaN operand raises invalid_operation.
Near the underflow threshold the single rounding matters. In decimal32, the
exact product 3.000001E-45 * 1.500001E-45 is , a normal number whose exact exponent is below ; it is rounded once to seven digits, 4.500005E-90. The quotient
1 / 1.9999999999998E+101 is , which is
subnormal; its guarded quotient is rounded once to the grid ,
giving 1E-101 with inexact and underflow.
Decimal::add_ctx, sub_ctx, mul_ctx, div_ctx
These functions are the correctly rounded arithmetic operations.
pub fn Decimal::add_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::sub_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::mul_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::div_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
Preferred exponents: for addition and subtraction,
for multiplication, for division. An exact quotient is returned in
the member closest to the preferred exponent; an inexact quotient has
digits, except a subnormal one, which ends at . A zero sum
is except under Floor (where it is if either operand is negative)
or when both operands are .
Special cases: and , give NaN
with invalid_operation; gives NaN with invalid_operation; for finite non-zero gives a signed infinity
with division_by_zero; finite gives a signed zero with exponent
.
When one operand of add_ctx or sub_ctx lies entirely below every
rounding boundary of the sum, the operation does not align it digit by
digit: it is replaced by a sticky unit of the same sign just below those
boundaries, and the sum is rounded once (see the
design). With precision 7,
1598617.000000000001 - 0.000000000002 is 1598616 under Down, and
6.0000005E-73 + 1E-101 is 6.000001E-73 under HalfEven.
///|
test "decimal context results are rounded once" {
let c32 = @decimal.DecimalContext::decimal32()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("3.000001E-45").mul_ctx(d("1.500001E-45"), c32).0, content="4.500005E-90")
let (q, flags) = d("1").div_ctx(d("1.9999999999998E+101"), c32)
inspect(q, content="1E-101")
inspect(flags.underflow(), content="true")
let down = @decimal.DecimalContext::new(precision=7, rounding=@decimal.DecimalRoundingMode::RoundTowardZero)
inspect(
d("1598617.000000000001").add_ctx(d("-0.000000000002"), down).0,
content="1598616",
)
}
///|
test "decimal context arithmetic keeps preferred exponents" {
let ctx = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("1.20").add_ctx(d("3.40"), ctx).0, content="4.60")
inspect(d("1.25").mul_ctx(d("2.50"), ctx).0, content="3.1250")
inspect(d("2.400").div_ctx(d("1.2"), ctx).0, content="2.00")
let (q, flags) = d("2").div_ctx(d("3"), ctx)
inspect(q, content="0.6666666666666667")
inspect(flags.inexact(), content="true")
}
Decimal::fma_ctx
fma_ctx computes with a single rounding.
pub fn Decimal::fma_ctx(Self, Self, Self, DecimalContext) -> (Self, DecimalFlags)
The product is formed exactly and added to z exactly; only the sum is
rounded. is invalid for a number or infinite z; when
z is a quiet NaN the result is that NaN without invalid_operation
(IEEE 754-2019 §7.2 leaves this case to the implementation).
with opposite signs is invalid.
Decimal::sqrt_ctx
sqrt_ctx returns the correctly rounded square root.
pub fn Decimal::sqrt_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
The preferred exponent is . An exact root is returned in
the member closest to it (sqrt(0.0400) is 0.20); an inexact root has
digits and raises inexact. ; a negative non-zero operand or gives
NaN with invalid_operation; .
Decimal::abs_ctx, apply_ctx
These functions round one operand to the context.
pub fn Decimal::abs_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::apply_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
apply_ctx rounds a value to the precision and the exponent range of the
context (an exponent above is folded down) and quiets a NaN,
keeping the sign of zero. It replaces the former
plus_ctx (and minus_ctx is neg followed by apply_ctx). abs_ctx is rounded to the context; signaling NaNs raise
invalid_operation.
Decimal::remainder_ctx
This function computes the IEEE remainder.
pub fn Decimal::remainder_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
remainder_ctx(x, y) is where is
rounded to the nearest integer, ties to even (IEEE 754 remainder; the
GDA name of it is remainder-near, which decimal_gda keeps). The remainder is exact when it fits,
with preferred exponent , and its sign is that of when it
is zero. and are invalid
(so is );
.
///|
test "decimal remainders" {
let ctx = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("10").remainder_ctx(d("6"), ctx).0, content="-2")
inspect(d("-7.5").remainder_ctx(d("2"), ctx).0, content="0.5")
}
Quantum and exponent operations
Decimal::quantum_ctx
quantum_ctx is the IEEE 754-2019 5.3.2 operation quantum.
pub fn Decimal::quantum_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
For a finite x the result is the number , that is , with
the exponent of x (zeros included), so quantum_ctx(1.2300) is 0.0001; the
preferred exponent is , which the exact result has. For an infinite x
the result is with no exception, whatever its sign. A quiet NaN is
propagated with its payload and no flag; a signaling NaN signals
invalid_operation and gives a quiet NaN with its payload (6.2). The
operation can signal only for a signaling NaN, so it takes the context and
returns the flags, like logb_ctx and next_up_ctx (doc/manual/conventions.md);
there is no plain quantum. The stored exponent is exponent10. 5.3.2 says
should for quantum and shall for quantize.
Decimal::quantize_ctx
quantize_ctx rounds a value to the exponent of another value.
pub fn Decimal::quantize_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
x.quantize_ctx(y, ctx) returns the value of x with exponent , rounding
with the context’s mode when digits are dropped (raising
inexact if they were non-zero). The result is NaN with invalid_operation
when the target exponent is outside , when the
resulting coefficient needs more than digits, when its adjusted exponent
exceeds , or when exactly one operand is infinite. Two infinities
give the infinity of x. The quantum never silently changes to another
value of the cohort range: if the result cannot have exponent the
operation fails. The one exception is the fold-down: a target exponent in
is accepted and the result is then folded down
to exponent , like every other result
(1E+384 quantized to 1E+384 in decimal64 is 1.000000000000000E+384).
///|
test "decimal quantize_ctx to cents" {
let ctx = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
let (cents, flags) = d("12.3456").quantize_ctx(d("0.01"), ctx)
inspect(cents, content="12.35")
inspect(flags.inexact(), content="true")
let small = @decimal.DecimalContext::new(precision=3, e_max=100)
let (bad, bad_flags) = d("999.9").quantize_ctx(d("0.1"), small)
inspect(bad.is_nan(), content="true")
inspect(bad_flags.invalid_operation(), content="true")
}
Decimal::same_quantum
same_quantum tests whether two values have the same exponent.
pub fn Decimal::same_quantum(Self, Self) -> Bool
Two finite values have the same quantum when their exponents are equal; two infinities, and two NaNs, always have the same quantum; any other pair does not. It never raises flags.
Decimal::to_integral_exact_ctx, to_integral_value_ctx
These functions round to an integer.
pub fn Decimal::to_integral_exact_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::to_integral_value_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
A finite value with exponent is already an integer and is returned
unchanged, even when it is longer than digits (the fold-down of the
format still applies to its exponent). A value with negative exponent is quantized to
exponent 0 with the context’s rounding mode, at a working precision of
, so the integral part is never rounded to
digits: at precision 3, 12345.6 gives 12346. to_integral_exact_ctx
reports inexact when non-zero digits are dropped; to_integral_value_ctx
returns the same value with inexact cleared. Infinities are
returned unchanged; NaNs are quieted.
IEEE 754 §5.9 defines roundToIntegral for an operand in the context’s format (at most digits), and for such an operand these are its results. An operand longer than digits is not a value of the format, so §5.9 does not cover it and the result is implementation-defined. The behaviour described above for such operands (a longer integer unchanged, a longer fraction quantized at digits) is the package’s chosen, GDA-style behaviour: General Decimal Arithmetic rounds to an integral value at the operand’s own precision.
Decimal::to_int_ctx, to_int64_ctx, to_uint_ctx, to_uint64_ctx
These functions are IEEE 754-2019 convertToInteger (§5.4.1, §5.8): they round a decimal to a fixed-width integer in the context’s rounding direction.
pub fn Decimal::to_int_ctx(Self, DecimalContext, exact? : Bool) -> (Int32?, DecimalFlags)
pub fn Decimal::to_int64_ctx(Self, DecimalContext, exact? : Bool) -> (Int64?, DecimalFlags)
pub fn Decimal::to_uint_ctx(Self, DecimalContext, exact? : Bool) -> (UInt32?, DecimalFlags)
pub fn Decimal::to_uint64_ctx(Self, DecimalContext, exact? : Bool) -> (UInt64?, DecimalFlags)
They have the shape of the Binary functions of the same names. The value
is rounded to an integer with the rounding of the context: ties to even,
toward zero, toward positive, toward negative and ties to away are the five
directions of §5.8. A NaN (quiet or signaling), an infinity and a value
outside the range of the integer type give None with invalid_operation;
the result of a negative value that rounds to zero is 0 (also for the
unsigned types), and any other negative value is out of range for them. With
exact=true (convertToIntegerExact) inexact is raised when rounding changed
the value; without it the flags of a valid conversion are empty. The context’s
precision and exponent range play no part: an integer of any number of digits
that fits the type is converted exactly. The conversion never forms the power
of ten of the exponent: 1E+999999 is out of range and 1E-999999 rounds to
zero (or to one unit in an away direction) at once.
Decimal::scaleb_ctx, logb_ctx
These functions scale by a power of ten and extract the adjusted exponent.
pub fn Decimal::scaleb_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::logb_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
x.scaleb_ctx(n, ctx) returns by adding to the exponent;
must be a finite integer with exponent 0 and
, otherwise the result is NaN with
invalid_operation (IEEE 754-2019 5.3.3 defines scaleB for integral counts only
and leaves a non-integral second operand, an infinity included, to the language;
the bound on is the GDA scaleb rule, where Binary::scaleb_ctx clamps a
larger count instead). The scaled value is then rounded once like every other
context result: a coefficient longer than digits is rounded to digits
(12345678901234567890 scaled by 0 in decimal64 is
1.234567890123457E+19 with inexact), a subnormal result is rounded to
, overflow gives the rounding-mode-dependent result with
overflow and inexact (in decimal64 with TowardZero, 9E+384
scaled by 1 is 9.999999999999999E+384), and large exponents are folded
down. A zero keeps its sign and has its exponent clamped into the range:
0 scaled by 700 in decimal64 is 0E+369.
///|
test "decimal scaleb rounds like other context results" {
let c64 = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
let toward_zero = c64.with_rounding(@decimal.DecimalRoundingMode::RoundTowardZero)
inspect(d("9E+384").scaleb_ctx(d("1"), toward_zero).0, content="9.999999999999999E+384")
inspect(d("9E+384").scaleb_ctx(d("1"), c64).0, content="inf")
let (zero, flags) = d("0").scaleb_ctx(d("700"), c64)
inspect(zero, content="0E+369")
inspect(flags.inexact(), content="false")
inspect(d("12345678901234567890").scaleb_ctx(d("0"), c64).0, content="1.234567890123457E+19")
}
logb_ctx(x) returns the adjusted exponent as an
integer Decimal; with
division_by_zero and .
Adjacent values
Decimal::next_up_ctx, next_down_ctx
These functions return the neighbouring representable values.
pub fn Decimal::next_up_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::next_down_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
next_up_ctx is the smallest representable value greater than x in the
context and next_down_ctx the largest smaller one, including subnormals down to
and the largest finite value
; next_up_ctx(-∞) is the most negative finite
value. They raise no flags for finite results. IEEE 754-2019 5.3.1 defines nextUp
and nextDown only: the next-toward operation of GDA 1.70 is next_toward of
decimal_gda, and there is no next_toward_ctx in this package.
///|
test "decimal neighbours of one" {
let ctx = @decimal.DecimalContext::decimal64()
let one = @decimal.Decimal::one()
inspect(one.next_up_ctx(ctx).0, content="1.000000000000001")
inspect(one.next_down_ctx(ctx).0, content="0.9999999999999999")
}
Comparison and ordering
Decimal::compare
compare is the numeric three-way comparison used by Compare, == and the
comparison operators.
pub fn Decimal::compare(Self, Self) -> Int
compare returns , 0 or 1 by numeric value, with and all
members of a cohort equal. NaN has no numeric order, so that Compare stays a
total preorder (sorting never aborts) every NaN compares equal to every other
NaN and greater than every non-NaN. equal (==) agrees with compare:
NaN == NaN is true. This is not IEEE equality; use
compare_checked, compare_quiet or the
predicates when NaN must be unordered. The operator methods are listed under
trait implementations.
///|
test "decimal numeric order is a total preorder" {
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("1.0") == d("1.00"), content="true")
inspect(d("-0").compare(d("0")), content="0")
inspect(@decimal.Decimal::nan().compare(d("1E+999")), content="1")
let sorted = [d("2"), @decimal.Decimal::nan(), d("-1")]
sorted.sort()
inspect(sorted.map(fn(x) { x.to_string() }).join(" "), content="-1 2 nan")
}
Decimal::compare_checked
compare_checked is the IEEE numeric comparison with NaN as an error.
pub fn Decimal::compare_checked(Self, Self) -> Result[Int, @arithmetic.ArithmeticError]
It returns Ok(compare(x, y)) when neither operand is a NaN and
Err(unordered_comparison) otherwise. It is the CompareChecked
implementation.
Decimal::compare_quiet, compare_signaling
IEEE 754-2019 5.11 comparison as a four-valued relation with flags.
pub fn Decimal::compare_quiet(Self, Self) -> (@def.PartialOrder, DecimalFlags)
pub fn Decimal::compare_signaling(Self, Self) -> (@def.PartialOrder, DecimalFlags)
The relation is Less, Equal, Greater or Unordered (some operand is a
NaN), with and all members of a cohort equal. The quiet form raises
invalid_operation only for a signaling NaN; the signaling form raises it for
every unordered comparison. These are the functions of Binary; they replace
compare_ctx and compare_signal_ctx, which returned the Decimal , 0, 1
or a NaN, a General Decimal Arithmetic result and not an IEEE one.
Decimal::compare_quiet_equal and the other comparison predicates
The predicates of IEEE 754-2019 Tables 5.1 and 5.2 that Binary has.
pub fn Decimal::compare_quiet_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_less_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_less_equal(Self, Self) -> (Bool, DecimalFlags)
Each is defined from compare_quiet or compare_signaling and returns its
flags; every predicate except compare_quiet_unordered is false on an
unordered pair.
Decimal::compare_quiet_not_equal and the other comparison predicates of Tables 5.1 and 5.2
The fifteen predicates of IEEE 754-2019 5.11 not listed above.
pub fn Decimal::compare_quiet_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_greater_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_greater_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_less_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_not_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_not_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_not_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_ordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_greater_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_greater_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_less_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_not_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_not_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_not_less(Self, Self) -> (Bool, DecimalFlags)
Clause 5.11 lists 22 predicates, 10 in Table 5.1 (with the common mathematical
symbols) and 12 in Table 5.2; with the seven above every one of them has a
function. Each is defined from compare_quiet or compare_signaling through
the relations that make it true, which is how the tables define it, and returns
the flags of that comparison: a quiet predicate raises invalid_operation for
a signaling NaN only, a signaling predicate for every NaN operand. The
doc comment of each names its row. Name spelling follows the seven above:
compare_, quiet or signaling, then the predicate name of the table in
snake case. The pairs the standard states as negations of one another (the two
predicates of one line of a table) return opposite values and the same flags.
| Table | predicate | true relations |
|---|---|---|
| 5.1 | Decimal::compare_quiet_equal (compareQuietEqual) | equal |
| 5.1 | Decimal::compare_quiet_not_equal (compareQuietNotEqual) | less, greater, unordered |
| 5.1 | Decimal::compare_signaling_greater (compareSignalingGreater) | greater |
| 5.1 | Decimal::compare_signaling_not_greater (compareSignalingNotGreater) | less, equal, unordered |
| 5.1 | Decimal::compare_signaling_greater_equal (compareSignalingGreaterEqual) | equal, greater |
| 5.1 | Decimal::compare_signaling_less_unordered (compareSignalingLessUnordered) | less, unordered |
| 5.1 | Decimal::compare_signaling_less (compareSignalingLess) | less |
| 5.1 | Decimal::compare_signaling_not_less (compareSignalingNotLess) | equal, greater, unordered |
| 5.1 | Decimal::compare_signaling_less_equal (compareSignalingLessEqual) | less, equal |
| 5.1 | Decimal::compare_signaling_greater_unordered (compareSignalingGreaterUnordered) | greater, unordered |
| 5.2 | Decimal::compare_signaling_equal (compareSignalingEqual) | equal |
| 5.2 | Decimal::compare_signaling_not_equal (compareSignalingNotEqual) | less, greater, unordered |
| 5.2 | Decimal::compare_quiet_greater (compareQuietGreater) | greater |
| 5.2 | Decimal::compare_quiet_not_greater (compareQuietNotGreater) | less, equal, unordered |
| 5.2 | Decimal::compare_quiet_greater_equal (compareQuietGreaterEqual) | equal, greater |
| 5.2 | Decimal::compare_quiet_less_unordered (compareQuietLessUnordered) | less, unordered |
| 5.2 | Decimal::compare_quiet_less (compareQuietLess) | less |
| 5.2 | Decimal::compare_quiet_not_less (compareQuietNotLess) | equal, greater, unordered |
| 5.2 | Decimal::compare_quiet_less_equal (compareQuietLessEqual) | less, equal |
| 5.2 | Decimal::compare_quiet_greater_unordered (compareQuietGreaterUnordered) | greater, unordered |
| 5.2 | Decimal::compare_quiet_unordered (compareQuietUnordered) | unordered |
| 5.2 | Decimal::compare_quiet_ordered (compareQuietOrdered) | less, equal, greater |
Decimal::total_order, total_order_mag, total_order_compare
These functions implement the IEEE 754-2019 §5.10 totalOrder and
totalOrderMag predicates, and the same order as a three-way comparison.
pub fn Decimal::total_order(Self, Self) -> Bool
pub fn Decimal::total_order_mag(Self, Self) -> Bool
pub fn Decimal::total_order_compare(Self, Self) -> Int
total_order(x, y) is true when x is ordered at or before y, and
total_order_compare(x, y) is -1, 0 or 1 for the same order. The order
covers every representation:
Equal finite values are ordered by exponent: for positive values the smaller
exponent comes first (), for negative values the larger. NaNs of
the same sign and kind are ordered by payload (reversed for negative NaNs).
total_order_compare returns 0 only for identical representations.
total_order_mag(x, y) is total_order of the absolute values. None of them
takes a context, rounds or raises a flag.
Decimal::identical
identical tests that two values are the same datum.
pub fn Decimal::identical(Self, Self) -> Bool
x.identical(y) is true when the class, sign, signaling bit, coefficient (the
payload of a NaN), exponent and precision label are all equal. It is finer than
==, which compares values (1.20 == 1.2), and than total_order_compare,
which ignores the precision label: 1.20 and 1.2 are not identical, a zero and
its negative are not identical, and two NaNs that differ in payload are not
identical.
///|
test "decimal identical compares the representation" {
let a = @decimal.Decimal::from_string("1.20").unwrap()
inspect(a.identical(@decimal.Decimal::from_string("1.20").unwrap()), content="true")
inspect(a.identical(@decimal.Decimal::from_string("1.2").unwrap()), content="false")
inspect(a == @decimal.Decimal::from_string("1.2").unwrap(), content="true")
}
Decimal::min, max, clamp, clamp_checked
These functions select by numeric order without a context.
pub fn Decimal::min(Self, Self) -> Self
pub fn Decimal::max(Self, Self) -> Self
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 and max return the other operand when exactly one is a quiet NaN, a
quiet NaN when both are NaNs or either is signaling, and the receiver on
ties. clamp returns min below the range, max above it, and the value
(including a NaN) otherwise; it aborts when a bound is NaN or min > max.
clamp_checked returns Err(domain_error) in those cases.
Decimal::minimum_ctx, maximum_ctx, minimum_number_ctx, maximum_number_ctx, minimum_magnitude_ctx, maximum_magnitude_ctx, minimum_magnitude_number_ctx, maximum_magnitude_number_ctx
These eight functions are the IEEE 754-2019 §9.6 minimum and maximum
operations. The General Decimal Arithmetic min, max, min-magnitude and
max-magnitude are in decimal_gda.
pub fn Decimal::minimum_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::minimum_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::minimum_magnitude_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_magnitude_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::minimum_magnitude_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_magnitude_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
minimum_ctx and maximum_ctx propagate NaN: any NaN operand gives a quiet
NaN. The _number_ variants return the number when exactly one operand is a
NaN. In both, a signaling NaN raises invalid_operation. The magnitude
variants compare and and fall back to the signed order on equal
magnitudes. Ties are broken by total_order_compare, so . The selected value is rounded
to the context.
Elementary functions
Every elementary function exists in two forms. f_ctx(x, ctx) returns
(result, flags). try_f_ctx(x, ctx) returns the same pair in Ok, or
Err with an ArithmeticError whose certification_failure_detail() names
the operation, the target precision and the exhausted refinement budget when
the result could not be certified. When certification fails, f_ctx returns
NaN with not_computed, and try_f_ctx returns Err of the certification-failure kind.
Transcendental results are certified: the implementation evaluates a
guaranteed enclosure of in interval and accepts it
only when both endpoints round to the same Decimal with the same flags (see
the design). Because
rounding is monotone, an accepted result is the rounding of in every
DecimalRoundingMode. A list of exact cases (such as
, , , ,
, , ,
exact roots such as rootn_ctx(0.008, 3) , exact norms such as
hypot_ctx(0.3, 0.4) ) is decided before the enclosure loop and
returned without inexact, and an exact binary result such as
pow_ctx(4, 1.5) is returned exactly by the enclosure itself. Integer
powers are not certified: pow_ctx
rounds them once from the exact power or from
refined directed bounds.
These operations return NaN with not_computed (and the try_ forms Err, of the certification-failure kind) when ,
or , or when a finite operand has
more than 999,999 digits or an adjusted exponent beyond about .
In such a context the rule is one: a case that IEEE 754-2019 9.2.1 or Table 9.1
lists is a value, and everything else is not_computed. The listed cases are a
NaN operand; an infinite or zero operand; the listed exact points
(ln_ctx(1), log2_ctx(1), log10_ctx(1), acos_ctx(1), acospi_ctx(1),
acosh_ctx(1) are 0, atanpi_ctx(+inf) is 0.5, sinpi_ctx(n) for an
integer n, cospi_ctx(n + 0.5), tanpi_ctx at integers and half-integers);
the domain errors (invalid_operation) and the poles (division_by_zero);
compound, rootn and rsqrt at the operands the clause lists; and the
constants of atan2, atan2pi and atan(+-inf). The limit of the context is
tested after them and before the first step that computes. An exact result of
an ordinary operand that the clause does not list stays not_computed:
hypot_ctx(3, 4), rsqrt_ctx(4), log10_ctx(1E+5), atanpi_ctx(1),
acospi_ctx(0), sinpi_ctx(0.5), cospi_ctx(1), exp2_ctx(3), rootn_ctx(8, 3).
Accepting every context for ordinary operands is the subject of
#478. decimal_gda keeps
the other order, which the decTest corpus requires. Cost: the constants of
atan(+-inf) and of the multiples of pi of atan2 are computed at the
precision of the context; at a precision of 1,000,000 digits that takes about
100 seconds each, and from 80,807,094 digits on the result is not_computed at once:
decimal_certified_work_refusal refuses a first working precision (3.321929
bits per digit plus 31 guard bits) above binary_precision_max, 268,435,456
bits (binary/context.mbt:43); 80,807,093 digits is the largest precision
served. Integral powers in decimal support counts through
999,999,999 for positive exponents and -1,999,999,997 for negative exponents;
these are adopted decNumber implementation limits, not limits imposed by GDA
1.70. Integer powers above those counts use bounded square-and-multiply when
the exact exponent coefficient has at most 4,096 decimal digits. Larger
coefficients return NaN with invalid_operation before expansion. Thus
pow_ctx(2, 1E+9) is finite under DecimalContext::new(), while
pow_ctx(2, 1E+9999999) is rejected within the fixed work bound. The same
large input to decimal_gda still follows the GDA adapter’s decNumber count
limit and returns invalid_operation; IEEE decimal and decimal_gda
therefore intentionally differ on this input. Non-integral powers such as
pow_ctx(2, 1.5) and exp2_ctx(2) remain subject to the elementary-function
context limits.
Decimal::exp_ctx, ln_ctx, log10_ctx, try_exp_ctx, try_ln_ctx, try_log10_ctx
These functions are the General Decimal Arithmetic exponential and logarithms.
pub fn Decimal::exp_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::ln_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log10_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_exp_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_ln_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log10_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
, , . and
of are with division_by_zero, as IEEE 754-2019 §9.2.1 requires and as log2_ctx, log1p_ctx and
logb_ctx do (General Decimal Arithmetic, which
decimal_gda follows, raises no condition here); of
are , of a negative value or are NaN with
invalid_operation; , and of a power of ten is the
exact integer exponent. Arguments so large or small that certainly
overflows or underflows are decided without evaluation.
Decimal::pow_ctx, pown_ctx, rootn_ctx, hypot_ctx, try_pow_ctx, try_pown_ctx, try_rootn_ctx, try_hypot_ctx
These functions compute powers, roots and the Euclidean norm.
The pow family (pow_ctx, pown_ctx, powr_ctx and their try_ forms)
has an engineering limit on the precision of the context: above 999,999
decimal digits by default (the precision part of the limit of the elementary
functions, DecimalContext::elementary_precision_limit; None or a larger
value is the escape entry, with no guarantee) it computes nothing for an ordinary
operand, not even the exponent 0.5, which would otherwise be a square root at
that precision, and returns the context-limit error (not_computed in the
plain form) at once. The cost is the reason: pow(2, 0.5) takes about 0.6 s at
100,000 digits, 14.7 s at 1,000,000 and more than 300 s at 10,000,000. The
special cases that IEEE 754-2019 9.2.1 lists for pow stay values in every
context. pown_ctx is pow_ctx with an integral exponent and is under the
same limit, an exact integral power included; rootn_ctx and compound_ctx
were already under it. sqrt_ctx has no such limit: it is a basic operation
(5.4.1).
pub fn Decimal::pow_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::pown_ctx(Self, Int, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::rootn_ctx(Self, Int, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::hypot_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_pow_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_pown_ctx(Self, Int, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_rootn_ctx(Self, Int, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_hypot_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pow_ctx(x, y) is the IEEE 754-2019 §9.2 power operation. A signaling NaN
operand is quieted and raises invalid_operation; this takes precedence over
the identities below. Otherwise , including for zero, infinity,
and quiet NaN, and for every (including quiet
NaN). Also . An infinite exponent
classifies the magnitude of any other base: tends to zero for
and infinity for , while does the reverse; the
sign of a negative base does not make these cases invalid.
For a zero base and a negative integer exponent, the result is the signed
infinity selected by the zero sign and exponent parity, with division_by_zero:
for example, and . Positive integer powers
preserve the zero sign by parity: and . Zero raised to a
negative non-integer exponent is positive infinity with division_by_zero.
Integer powers within the bounded count use binary square-and-multiply. An exact
power is returned when it fits in digits; otherwise the result is correctly
rounded. An integral
exponent outside the bounded count is still supported: the magnitude is
certified, and an odd power of a negative base swaps the directed rounding
modes before restoring its sign. For large integer exponents the zero and
infinity cases are decided from sign and parity without expanding the exponent:
for example, pown_ctx(-0, Int::max_value()) is -0,
pown_ctx(-0, Int::min_value()) is +infinity with division_by_zero, and
pown_ctx(-infinity, Int::min_value()) is +0.
For other integral exponents, an exact power is returned when it fits in
digits and otherwise the correctly rounded power (in
decimal32, pow_ctx(3.339434, 3) is 37.24077; the exact cube is
); is sqrt_ctx; positive non-integer powers
are certified and negative bases with a finite non-integer exponent are
invalid. These IEEE rules intentionally differ from
decimal_gda, which follows General Decimal Arithmetic
(including its 0^0 invalid result and the powx2015 rounded 1.00 vector).
pown_ctx(x, n) converts the integer n exactly, so
at any precision. rootn_ctx(x, n) is
for integer ; an even root of a negative value and
are invalid, and is an infinity with
division_by_zero; an exactly representable root is returned exactly
(rootn_ctx(0.008, 3) is 0.2 with no flag). hypot_ctx(x, y) is
; it is if either operand is infinite, even if the
other is a quiet NaN, and an exactly representable norm, or when is
zero, is returned exactly (hypot_ctx(0.3, 0.4) is 0.5).
An integral exponent with more than 4,096 digits is never expanded: for a base
other than , and the magnitude is far
beyond any context, so the result is the overflow or the underflow value of the
rounding direction (decided from the base being above or below 1 and the sign
of the exponent) and the sign follows the parity, not an invalid_operation. A
base with more than about 4,000 fractional digits, whose bound is not met, still
returns NaN with invalid_operation.
An exact result is delivered in the cohort member whose exponent is closest to
the preferred exponent of IEEE 754-2019 §9.2.2, for
pow_ctx(x, y) and for pown_ctx(x, n), where
is the exponent of the stored x and the member must fit in digits:
in decimal64 pown_ctx(1.00, 2) is 1.0000, pown_ctx(-1.0, 3) is -1.000,
pown_ctx(-1E+5, 5) is -1E+25 (exact, no flags), pow_ctx(1.00, 1.5) is
1.000 and pow_ctx(4.00, 1.5) is 8.000; a negative exponent cannot reach
a preferred exponent above the value (pown_ctx(1.00, -2) is 1). The
standard defines no preferred exponent when is infinite or a NaN, so
pow_ctx(1.00, Infinity) stays 1. decimal_gda follows the repeated
multiplication exponent of General Decimal Arithmetic instead.
Decimal::exp2_ctx, exp10_ctx, expm1_ctx, log2_ctx, log1p_ctx, try_exp2_ctx, try_exp10_ctx, try_expm1_ctx, try_log2_ctx, try_log1p_ctx
These functions are the IEEE 754-2019 §9.2 exponentials and logarithms in bases 2 and 10 and near zero. Each has the f_ctx/try_f_ctx shape described
at the start of this section.
pub fn Decimal::exp2_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exp10_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::expm1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log2_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log1p_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_exp2_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp10_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_expm1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log2_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log1p_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
Decimal::exp2m1_ctx, exp10m1_ctx, log2p1_ctx, log10p1_ctx, try_exp2m1_ctx, try_exp10m1_ctx, try_log2p1_ctx, try_log10p1_ctx
These functions are , , and
of IEEE 754-2019 §9.2, without the cancellation of
exp2(x) - 1 or the rounding of 1 + x near zero. Each has the
f_ctx/try_f_ctx shape described at the start of this section.
pub fn Decimal::exp2m1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exp10m1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log2p1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log10p1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_exp2m1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp10m1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log2p1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log10p1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
The special values are those of §9.2.1: and
for all four, for exp2m1 and
exp10m1, and with division_by_zero for log2p1 and
log10p1, whose domain is (, including , is
invalid). The exact results are and for an integer
and the integer when (resp. ); they take the
preferred exponent 0 of §9.2.2, so in decimal64 exp2m1_ctx(4) is 15,
exp2m1_ctx(-1) is -0.5, exp10m1_ctx(-2.0) is -0.99 and
log2p1_ctx(-0.50) is -1, all without flags. A context that cannot hold an
exact value rounds it with its flags: log2p1_ctx(32767) at precision 1 is
2E+1 (a midpoint, inexact). exactly and
; the rule of §9.2.1
belongs to log, log2 and log10 only. Results below the subnormal range
are rounded, never replaced, so they keep the sign of
(exp2m1_ctx(-1E-500) is -0E-398 in decimal64, -1E-398 when rounding
toward ), and the same restricted contexts as exp_ctx give
not_computed.
///|
test "decimal exp2m1 exp10m1 log2p1 log10p1" {
let c64 = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("4").exp2m1_ctx(c64).0, content="15")
inspect(d("-2.0").exp10m1_ctx(c64).0, content="-0.99")
inspect(d("1E-10").exp2m1_ctx(c64).0, content="6.931471805839680E-11")
inspect(d("-0.50").log2p1_ctx(c64).0, content="-1")
let (pole, flags) = d("-1").log10p1_ctx(c64)
inspect(pole, content="-inf")
inspect(flags.division_by_zero(), content="true")
}
Decimal::rsqrt_ctx, compound_ctx, powr_ctx, try_rsqrt_ctx, try_compound_ctx, try_powr_ctx
These functions are , for an Int32 and
of IEEE 754-2019 §9.2. Each has the f_ctx/try_f_ctx
shape described at the start of this section.
pub fn Decimal::rsqrt_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::compound_ctx(Self, Int32, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::powr_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_rsqrt_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_compound_ctx(Self, Int32, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_powr_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
rsqrt_ctx has domain : (including ) is
invalid, with division_by_zero and
(§9.2.1). It is exact when ; the
result then takes the preferred exponent of §9.2.2,
so in decimal64 rsqrt_ctx(4.00) is 0.5, rsqrt_ctx(1E+2) is 0.1 and
rsqrt_ctx(1E-400) is 1E+200.
compound_ctx(x, n) follows the list of §9.2.1: compound(x, 0) is 1 for
and for a quiet NaN, (also with ) is invalid,
compound(-1, n) is with division_by_zero for and
for , compound(±0, n) is 1 and compound(+∞, n) is or
. An exact result takes the preferred exponent
: compound_ctx(0.10, 2) is 1.2100 and
compound_ctx(0.00, 3) is 1.000000. Every of Int32 is accepted, also
the Int32 minimum .
powr_ctx(x, y) is , so its domain excludes (invalid,
also when is a quiet NaN). It returns a quiet NaN for a NaN operand even
when , is invalid for powr(±0, ±0), powr(+∞, ±0) and
powr(1, ±∞), and otherwise equals pow_ctx on (results, cohort and
flags): powr_ctx(-0, 3) is 0 where pow_ctx gives -0.
///|
test "decimal rsqrt compound powr" {
let c64 = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("6.25").rsqrt_ctx(c64).0, content="0.4")
inspect(d("2").rsqrt_ctx(c64).0, content="0.7071067811865475")
inspect(d("0.05").compound_ctx(3, c64).0, content="1.157625")
inspect(d("0.05").compound_ctx(-3, c64).0, content="0.8638375985314761")
inspect(d("4").powr_ctx(d("0.5"), c64).0, content="2")
let (invalid, flags) = d("-2").powr_ctx(d("2"), c64)
inspect(invalid, content="nan")
inspect(flags.invalid_operation(), content="true")
}
Decimal::sin_ctx, cos_ctx, tan_ctx, sinpi_ctx, cospi_ctx, tanpi_ctx, try_sin_ctx, try_cos_ctx, try_tan_ctx, try_sinpi_ctx, try_cospi_ctx, try_tanpi_ctx
These functions are the trigonometric functions of and of . Each has the f_ctx/try_f_ctx shape described
at the start of this section.
pub fn Decimal::sin_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::cos_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::tan_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::sinpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::cospi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::tanpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_sin_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_cos_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_tan_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_sinpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_cospi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_tanpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
Decimal::asin_ctx, acos_ctx, atan_ctx, atan2_ctx, try_asin_ctx, try_acos_ctx, try_atan_ctx, try_atan2_ctx
These functions are the inverse trigonometric functions; atan2(y, x) is the angle of the point . Each has the f_ctx/try_f_ctx shape described
at the start of this section.
pub fn Decimal::asin_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::acos_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atan_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atan2_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_asin_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_acos_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atan_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atan2_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
Decimal::asinpi_ctx, acospi_ctx, atanpi_ctx, atan2pi_ctx, try_asinpi_ctx, try_acospi_ctx, try_atanpi_ctx, try_atan2pi_ctx
These functions are the inverse circular functions measured in
half-revolutions of IEEE 754-2019 §9.2: ,
, and
, the last with the ordinate as the receiver
(y.atan2pi_ctx(x, ctx)) and a result in . Each has the
f_ctx/try_f_ctx shape described at the start of this section.
pub fn Decimal::asinpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::acospi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atanpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atan2pi_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_asinpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_acospi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atanpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atan2pi_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
The exact results are those of §9.2.1, and by Niven’s theorem the only ones:
, ;
, ,
; ,
and
with no exception; and the
eleven rows of for zero and infinite operands
(, , , , , the sign
bit of a zero abscissa choosing the half-plane), plus for finite nonzero operands ( for ,
for ). None of them signals an exception. They take the
preferred exponent 0 of §9.2.2, so is 0.5, is
0.25, is 0.75 and 1 is 1, whatever the cohort of the operand;
a context too narrow for the constant (precision 1) rounds it, with inexact.
(also ) is invalid for asinpi and acospi.
Every other result is inexact (
and are not
terminating decimals) and is correctly rounded, also next to the representable
values and that atanpi (large ),
acospi (tiny ) and atan2pi (extreme ratios) approach from
one side: there the rounding mode decides between the anchor and its
neighbour.
///|
test "decimal inverse circular functions in half-revolutions" {
let c64 = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("1").atanpi_ctx(c64).0, content="0.25")
inspect(d("-1").atan2pi_ctx(d("-1"), c64).0, content="-0.75")
inspect(d("-0").atan2pi_ctx(d("-0"), c64).0, content="-1")
inspect(d("0.5").asinpi_ctx(c64).0, content="0.1666666666666667")
inspect(d("0.5").acospi_ctx(c64).0, content="0.3333333333333333")
let (value, flags) = d("2").asinpi_ctx(c64)
inspect(value, content="nan")
inspect(flags.invalid_operation(), content="true")
}
Decimal::sinh_ctx, cosh_ctx, tanh_ctx, asinh_ctx, acosh_ctx, atanh_ctx, try_sinh_ctx, try_cosh_ctx, try_tanh_ctx, try_asinh_ctx, try_acosh_ctx, try_atanh_ctx
These functions are the hyperbolic functions and their inverses. Each has the f_ctx/try_f_ctx shape described
at the start of this section.
pub fn Decimal::sinh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::cosh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::tanh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::asinh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::acosh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atanh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::try_sinh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_cosh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_tanh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_asinh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_acosh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atanh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
Domain and special values of the functions in the seven groups above, from exp2 to atanh:
| Function | Invalid (invalid_operation) | Pole (division_by_zero) | Exact cases |
|---|---|---|---|
exp2, exp10 | none | none | integer argument (via pow_ctx), , , |
expm1 | none | none | , , |
log2 | , | , exact binary powers (), | |
log1p | , | , | |
exp2m1, exp10m1 | none | none | , , ; integer : , |
log2p1, log10p1 | , | , ; (resp. ) | |
rsqrt | , | ; | |
compound(x, n) | , (any ) | , , , by the sign of ; exact powers | |
powr(x, y) | , , , | NaN for a NaN operand; otherwise as power on | |
sin, cos, tan | none | , , | |
sinpi, cospi, tanpi | tanpi at odd half-integers | integers, half-integers, tanpi at odd quarter-integers () | |
asin, acos | , | none | , |
atan | none | none | ; (certified) |
asinpi, acospi | , | none | , , , , |
atanpi | none | none | ; ; (no exception) |
atan2pi(y, x) | none | none | the eleven exact rows of §9.2.1 (, , , , ) and |
atan2(y, x) | none | none | for or , and for finite ; any other zero or infinite operand gives , , or with the sign of (certified) |
sinh, tanh, asinh | none | none | ; , keep ; |
cosh | none | none | , |
acosh | , | none | , |
atanh | , |
These special values follow IEEE 754-2019 §9.2.1: in decimal64,
atan2_ctx(-0, -1) is -3.141592653589793, atan2_ctx(+∞, -∞) is
2.356194490192345 and atan2_ctx(-1, +∞) is -0.
///|
test "decimal exact and special elementary results" {
let c64 = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
let (norm, flags) = d("0.3").hypot_ctx(d("0.4"), c64)
inspect(norm, content="0.5")
inspect(flags.inexact(), content="false")
inspect(d("0.008").rootn_ctx(3, c64).0, content="0.2")
let c32 = @decimal.DecimalContext::decimal32()
inspect(d("3.339434").pow_ctx(d("3"), c32).0, content="37.24077")
let seven = @decimal.DecimalContext::new(precision=7)
inspect(d("-1").pown_ctx(12345679, seven).0, content="-1")
inspect(d("-0").atan2_ctx(d("-1"), c64).0, content="-3.141592653589793")
inspect(d("Inf").atan2_ctx(d("-Inf"), c64).0, content="2.356194490192345")
inspect(d("-1").atan2_ctx(d("Inf"), c64).0, content="-0")
inspect(d("-Inf").cosh_ctx(c64).0, content="inf")
inspect(d("Inf").log2_ctx(c64).0, content="inf")
}
///|
test "decimal certified elementary functions" {
let ctx = @decimal.DecimalContext::decimal64()
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("1").exp_ctx(ctx).0, content="2.718281828459045")
inspect(d("1000").log10_ctx(ctx).0, content="3")
inspect(d("0.5").sinpi_ctx(ctx).0, content="1")
let down = ctx.with_rounding(@decimal.DecimalRoundingMode::RoundTowardZero)
inspect(d("2").ln_ctx(down).0, content="0.6931471805599453")
// a context beyond the elementary kernels is not computed: `Err` in the `try_`
// form, a NaN with `not_computed` (and not `invalid_operation`) in the plain form
let beyond = @decimal.DecimalContext::new(e_max=999_999_999)
match d("2").try_ln_ctx(beyond) {
Ok(_) => fail("a context beyond the kernels must be an Err")
Err(error) => inspect(error.is_certification_failure(), content="true")
}
let (value, flags) = d("2").ln_ctx(beyond)
inspect(value.is_nan(), content="true")
inspect(flags.not_computed(), content="true")
inspect(flags.invalid_operation(), content="false")
// a domain error is a value with invalid_operation
let (nan, nan_flags) = d("-1").ln_ctx(ctx)
inspect(nan.is_nan(), content="true")
inspect(nan_flags.invalid_operation(), content="true")
}
Interchange formats
DecimalInterchangeFormat, DecimalInterchangeFormat::context
DecimalInterchangeFormat names an IEEE 754 decimal interchange format.
pub(all) enum DecimalInterchangeFormat {
Decimal32
Decimal64
Decimal128
}
pub fn DecimalInterchangeFormat::context(Self) -> DecimalContext
context returns DecimalContext::decimal32(), decimal64() or
decimal128().
DecimalInterchangeEncoding
DecimalInterchangeEncoding selects how the coefficient is stored in the
bits.
pub(all) enum DecimalInterchangeEncoding {
DPD
BID
}
DPD stores three decimal digits per 10-bit declet (densely packed decimal);
BID stores the coefficient as a binary integer. Functions without an
encoding argument use DPD.
Decimal::to_interchange_hex, to_interchange_hex_with_encoding
These functions encode a value as interchange bits written in hexadecimal.
pub fn Decimal::to_interchange_hex(Self, DecimalInterchangeFormat) -> (String, DecimalFlags)
pub fn Decimal::to_interchange_hex_with_encoding(Self, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> (String, DecimalFlags)
A finite value is first rounded with apply_ctx under the format context
(its flags are returned), then encoded with its exponent, so the cohort is
kept when it fits. The text is # followed by 8, 16 or 32 upper-case hex
digits. Infinities are encoded with a zero trailing field. A NaN keeps its
sign and kind, and its payload is stored as an integer, independent of the
value’s precision field (IEEE 754-2019 §3.5.2): a payload below is
kept, and a longer one keeps its low digits, in DPD and BID alike.
NaN7 encodes to decimal64 as #7C00000000000007 in both encodings.
Decimal::from_interchange_hex, from_interchange_hex_with_encoding
These functions decode interchange hex text.
pub fn Decimal::from_interchange_hex(String, DecimalInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::from_interchange_hex_with_encoding(String, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> Result[Self, @arithmetic.ArithmeticError]
The text may have surrounding ASCII whitespace and a leading #; it must have
exactly the format’s number of hex digits, in either case. Anything else is an
Err (a parse error) that says what was wrong: the number of digits wanted and
found ("#A23003D" for decimal32 has 7 of 8), or the first character that is
not a hexadecimal digit and its place among the digits. The length is checked
first. No bit pattern of the right length is refused: decoding is exact and keeps the exponent, the sign of zero, the NaN
kind and payload. Non-canonical encodings decode to the value IEEE 754
assigns them: a non-canonical DPD declet to its digits, a BID coefficient
to zero, an out-of-range BID NaN payload to 0; the unused
exponent bits of infinities and NaNs are ignored. The result has the format’s
precision. GdaInterchange::from_hex of decimal_gda accepts the same texts
and refuses the same ones, but its message is one sentence for both faults.
///|
test "decimal64 interchange in both encodings" {
let x = @decimal.Decimal::from_string("1.25").unwrap()
let fmt = @decimal.DecimalInterchangeFormat::Decimal64
let (dpd, _) = x.to_interchange_hex(fmt)
let (bid, _) = x.to_interchange_hex_with_encoding(
fmt,
@decimal.DecimalInterchangeEncoding::BID,
)
inspect(dpd, content="#22300000000000A5")
inspect(bid, content="#318000000000007D")
let back = @decimal.Decimal::from_interchange_hex(dpd, fmt).unwrap()
inspect(back, content="1.25")
// a text of the wrong length is an error that says so
match @decimal.Decimal::from_interchange_hex("#22300", fmt) {
Ok(_) => fail("expected a parse error")
Err(err) => assert_true(err.is_parse_error())
}
}
DecimalInterchange, DecimalInterchange::format, encoding, to_hex
DecimalInterchange holds the raw bits of one interchange value together
with its format and encoding.
pub struct DecimalInterchange {
// private fields
}
pub fn DecimalInterchange::format(Self) -> DecimalInterchangeFormat
pub fn DecimalInterchange::encoding(Self) -> DecimalInterchangeEncoding
pub fn DecimalInterchange::to_hex(Self) -> String
Use it when bits must be inspected or kept exactly, including non-canonical
encodings that a Decimal cannot represent. format and encoding return
the format and encoding the bits belong to; to_hex writes # and the
full-width upper-case hex digits.
DecimalInterchange::from_hex, from_hex_with_encoding, from_decimal, from_decimal_with_encoding
These functions build an interchange value from text or from a Decimal.
pub fn DecimalInterchange::from_hex(String, DecimalInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn DecimalInterchange::from_hex_with_encoding(String, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> Result[Self, @arithmetic.ArithmeticError]
pub fn DecimalInterchange::from_decimal(Decimal, DecimalInterchangeFormat) -> (Self, DecimalFlags)
pub fn DecimalInterchange::from_decimal_with_encoding(Decimal, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> (Self, DecimalFlags)
from_hex stores the bits unchanged (text rules and errors as in
from_interchange_hex). from_decimal encodes like to_interchange_hex and
returns the same flags. The forms without an encoding use DPD.
DecimalInterchange::to_decimal, to_decimal_ctx
These functions decode the stored bits.
pub fn DecimalInterchange::to_decimal(Self) -> Decimal
pub fn DecimalInterchange::to_decimal_ctx(Self) -> (Decimal, DecimalFlags)
Decoding is exact: to_decimal_ctx raises no flag.
DecimalInterchange::canonical, is_canonical
These functions canonicalize the stored bits.
pub fn DecimalInterchange::canonical(Self) -> Self
pub fn DecimalInterchange::is_canonical(Self) -> Bool
canonical decodes and re-encodes in the same format and encoding: every
non-canonical declet, out-of-range BID coefficient, and unused bit of an
infinity or NaN is replaced by its canonical form. It is idempotent.
is_canonical tests whether the bits are already canonical.
DecimalInterchange::copy, copy_abs, copy_negate, copy_sign
These functions change only the sign bit of the stored bits.
pub fn DecimalInterchange::copy(Self) -> Self
pub fn DecimalInterchange::abs(Self) -> Self
pub fn DecimalInterchange::neg(Self) -> Self
pub fn DecimalInterchange::copy_sign(Self, Self) -> Self
All other bits, canonical or not, are kept. copy_sign aborts unless both
operands have the same format and encoding.
Trait implementations
Zero, One, Ring and the operator traits
Decimal implements the Luna-Flow algebra traits through the plain
operators.
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
pub impl @luna-generic.FromNat for Decimal
pub impl @luna-generic.FromInteger for Decimal
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
pub impl Eq for Decimal
pub impl Compare for Decimal
Zero::zero() and One::one() are Decimal::zero() and Decimal::one().
The ring laws hold exactly for +, - and * as long as no result is rounded
(a sum or a product whose exact coefficient fits the larger operand precision);
a rounded result is only approximately associative.
FromNat and FromInteger are documented under
construction.
@def.Floating
Decimal implements the floating scalar trait.
pub impl @def.Floating for Decimal
The trait methods are classify, signum, precision, with_precision and
normalized, all documented above; @def.is_finite(x) and the other generic
predicates work through it.
Decimal::add_contextual, sub_contextual, mul_contextual, div_contextual, abs_contextual, sqrt_contextual, exp_contextual, zero_contextual, one_contextual, epsilon_contextual, min_normal_contextual, max_finite_contextual, classify_contextual
These functions implement the contextual traits of
Luna-Flow/arithmetic.
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 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.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
pub impl @arithmetic.NumericFormatContextual for Decimal
Each *_contextual operation converts the context with
DecimalContext::from_arithmetic_context, runs the matching *_ctx
operation and returns Err(division_by_zero) when division_by_zero was
raised, Err(domain_error) when another has_error flag was raised, and
otherwise Ok with diagnostics inexact, overflow and
underflow copied from the flags. Because the
converted context has the default exponent range, which the certified functions
accept for precisions up to 999, exp_contextual works at the default range; an
explicit e_max above 999,999 fails with certification_failure (not_computed). A result with invalid_operation is a domain_error; one with not_computed is a certification_failure; the try_ forms return Err only for the second kind.
zero_contextual and one_contextual have the context precision.
epsilon_contextual is (next_up_ctx(1) - 1).
min_normal_contextual is of the converted context; its default is , that is for the default range of new (precision 34).
max_finite_contextual is .
classify_contextual is classify.
Decimal::parse_checked, sqrt_checked, pow_nat_checked, pow_int_checked
These functions implement the checked traits of Luna-Flow/arithmetic.
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_nat_checked(Self, UInt, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::pow_int_checked(Self, Int, @arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub impl @arithmetic.ParseChecked for Decimal
pub impl @arithmetic.DivChecked for Decimal
pub impl @arithmetic.CompareChecked for Decimal
pub impl @arithmetic.SqrtChecked for Decimal
pub impl @arithmetic.PowNatChecked for Decimal
pub impl @arithmetic.PowIntChecked for Decimal
parse_checked(s, ctx) converts ctx to DecimalContext and uses
Decimal::from_string_ctx, so the context’s precision, rounding direction,
exponent range applies. It returns the converted value; flags
such as overflow remain available from from_string_ctx. Invalid syntax
returns Err(parse_error). DivChecked::div_checked(x, y, ctx) is div_ctx
under the converted context; it returns Err(division_by_zero) for the
division-by-zero flag and Err(domain_error) for the invalid-operation flag.
sqrt_checked is sqrt_ctx under the converted context and fails for
negative operands. pow_nat_checked and pow_int_checked call pow_ctx
with the integer exponent converted exactly (with at least ten digits, which
hold every accepted exponent), so at precision 7
(-1).pow_int_checked(12345679, ctx) is -1. They return Ok(1) for a zero
or quiet-NaN base with exponent zero, Err(division_by_zero) for a zero base with
a negative exponent, and Err(domain_error) for an
invalid result such as a signaling NaN operand. pow_nat_checked returns Err(unsupported) for exponents
above 999,999,999. CompareChecked is
compare_checked.
Eq and Compare: Decimal::equal, not_equal, op_lt, op_le, op_gt, op_ge
These are the operator methods of Eq and 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
They are defined by compare: x == y is
compare(x, y) == 0, x < y is compare(x, y) < 0, and so on. Cohort
members are equal (1.0 == 1.00), , every NaN equals every NaN and
is greater than every number. Use total_order_compare to tell representations
apart.
Derived equality: DecimalContext::equal, DecimalContext::not_equal, DecimalFlags::equal, DecimalFlags::not_equal, DecimalRoundingMode::equal, DecimalRoundingMode::not_equal, DecimalSignal::equal, DecimalSignal::not_equal, DecimalInterchangeFormat::equal, DecimalInterchangeFormat::not_equal, DecimalInterchangeEncoding::equal, DecimalInterchangeEncoding::not_equal
These are the derived Eq methods of the context, flag and enum types.
pub fn DecimalContext::equal(Self, Self) -> Bool
pub fn DecimalContext::not_equal(Self, Self) -> Bool
pub fn DecimalFlags::equal(Self, Self) -> Bool
pub fn DecimalFlags::not_equal(Self, Self) -> Bool
pub fn DecimalRoundingMode::equal(Self, Self) -> Bool
pub fn DecimalRoundingMode::not_equal(Self, Self) -> Bool
pub fn DecimalSignal::equal(Self, Self) -> Bool
pub fn DecimalSignal::not_equal(Self, Self) -> Bool
pub fn DecimalInterchangeFormat::equal(Self, Self) -> Bool
pub fn DecimalInterchangeFormat::not_equal(Self, Self) -> Bool
pub fn DecimalInterchangeEncoding::equal(Self, Self) -> Bool
pub fn DecimalInterchangeEncoding::not_equal(Self, Self) -> Bool
Contexts compare field by field (precision, rounding, exponent
limits, refinement limit); flag sets compare all six fields.
DecimalContext::from_arithmetic_context(ArithmeticContext::decimal64()) is
equal to DecimalContext::decimal64().
Show and Debug: Decimal::to_string, output, to_repr
Decimal implements Show and Debug.
pub impl Show for Decimal
pub fn Decimal::to_string(Self) -> String
pub fn Decimal::output(Self, &Logger) -> Unit
pub fn Decimal::to_repr(Self) -> @debug.Repr
to_string formats a value in scientific notation without a context. Finite
values use the scientific-string rule of
to_sci_string, so trailing zeros and
the exponent are visible: 1.20, 1E+3, 1.2E-7. Special values are written
in lower case: inf, -inf, nan, snan, -nan12. output writes the same
text to a logger. to_repr is the derived structural representation used by
debug_inspect and assert_eq.
///|
test "decimal to_string" {
let d = fn(s : String) { @decimal.Decimal::from_string(s).unwrap() }
inspect(d("123E+2"), content="1.23E+4")
inspect(d("0.0000001"), content="1E-7")
inspect(d("-0.00"), content="-0.00")
inspect(d("-sNaN7"), content="-snan7")
}
Complete public interface
This snapshot is the generated pkg.generated.mbti of the package. It is the authority when prose and interface disagree.
// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating/decimal"
import {
"Luna-Flow/arithmetic",
"Luna-Flow/floating/binary",
"Luna-Flow/floating/def",
"Luna-Flow/luna-generic",
"moonbitlang/core/bigint",
"moonbitlang/core/debug",
}
// Values
pub let decimal_context_e_max_limit : Int32
pub let decimal_context_e_min_limit : Int32
pub let decimal_precision_max : Int32
pub let decimal_value_e_max : Int32
pub let decimal_value_e_tiny : Int32
// 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::abs_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::acos_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::acosh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::acospi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
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::add_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::apply_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::asin_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::asinh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::asinpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atan2_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atan2pi_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atan_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atanh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::atanpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
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::class_of(Self, DecimalContext) -> @def.IeeeClass
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_quiet(Self, Self) -> (@def.PartialOrder, DecimalFlags)
pub fn Decimal::compare_quiet_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_greater_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_greater_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_less_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_less_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_not_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_not_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_not_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_ordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_quiet_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling(Self, Self) -> (@def.PartialOrder, DecimalFlags)
pub fn Decimal::compare_signaling_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_greater_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_greater_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_less_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_less_unordered(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_not_equal(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_not_greater(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compare_signaling_not_less(Self, Self) -> (Bool, DecimalFlags)
pub fn Decimal::compound_ctx(Self, Int32, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::convert_format(Self, DecimalInterchangeFormat) -> (Self, DecimalFlags)
pub fn Decimal::copy(Self) -> Self
pub fn Decimal::copy_sign(Self, Self) -> Self
pub fn Decimal::cos_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::cosh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::cospi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
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::div_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::epsilon_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::equal(Self, Self) -> Bool
pub fn Decimal::exp10_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exp10m1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exp2_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exp2m1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exp_contextual(Self, @arithmetic.ArithmeticContext) -> Result[@arithmetic.ArithmeticOutcome[Self], @arithmetic.ArithmeticError]
pub fn Decimal::exp_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::expm1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::exponent10(Self) -> Int32
pub fn Decimal::fma_ctx(Self, Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::from_bigint(@bigint.BigInt, precision? : Int32) -> Self
pub fn Decimal::from_binary(@binary.Binary, precision~ : Int32, mode? : DecimalRoundingMode) -> Self
pub fn Decimal::from_binary_ctx(@binary.Binary, DecimalContext) -> (Self, DecimalFlags)
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_interchange_hex(String, DecimalInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::from_interchange_hex_with_encoding(String, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::from_string(String, precision? : Int32) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::from_string_ctx(String, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::get_payload(Self) -> @bigint.BigInt
pub fn Decimal::hypot_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
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_normal(Self, DecimalContext) -> Bool
pub fn Decimal::is_quiet_nan(Self) -> Bool
pub fn Decimal::is_sign_minus(Self) -> Bool
pub fn Decimal::is_signaling_nan(Self) -> Bool
pub fn Decimal::is_subnormal(Self, DecimalContext) -> Bool
pub fn Decimal::is_zero(Self) -> Bool
pub fn Decimal::ln_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log10_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log10p1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log1p_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log2_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::log2p1_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::logb_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::make(@bigint.BigInt, Int32, Int32, mode? : DecimalRoundingMode) -> Self
pub fn Decimal::max_finite_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::max_num(Self, Self) -> Self
pub fn Decimal::maximum_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_magnitude_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_magnitude_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::maximum_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::min_normal_contextual(@arithmetic.ArithmeticContext) -> Self
pub fn Decimal::min_num(Self, Self) -> Self
pub fn Decimal::minimum_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::minimum_magnitude_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::minimum_magnitude_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::minimum_number_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
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::mul_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
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::next_down_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::next_up_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
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_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
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::pown_ctx(Self, Int32, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::powr_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::precision(Self) -> Int32
pub fn Decimal::quantize_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::quantum_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::quiet_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::remainder_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::rootn_ctx(Self, Int32, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::rsqrt_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::same_quantum(Self, Self) -> Bool
pub fn Decimal::scaleb_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::set_payload(Self, @bigint.BigInt) -> Self
pub fn Decimal::set_payload_signaling(Self, @bigint.BigInt) -> Self
pub fn Decimal::signaling_nan(payload? : @bigint.BigInt, negative? : Bool, precision? : Int32) -> Self
pub fn Decimal::signum(Self) -> @def.Sign
pub fn Decimal::sin_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::sinh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::sinpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
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::sqrt_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
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::sub_ctx(Self, Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::tan_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::tanh_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::tanpi_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::to_binary(Self, precision~ : Int32, mode? : @binary.BinaryRoundingMode) -> @binary.Binary
pub fn Decimal::to_binary_ctx(Self, @binary.BinaryContext) -> (@binary.Binary, @binary.BinaryFlags)
pub fn Decimal::to_eng_string(Self) -> String
pub fn Decimal::to_int64_ctx(Self, DecimalContext, exact? : Bool) -> (Int64?, DecimalFlags)
pub fn Decimal::to_int_ctx(Self, DecimalContext, exact? : Bool) -> (Int32?, DecimalFlags)
pub fn Decimal::to_integral_exact_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::to_integral_value_ctx(Self, DecimalContext) -> (Self, DecimalFlags)
pub fn Decimal::to_interchange_hex(Self, DecimalInterchangeFormat) -> (String, DecimalFlags)
pub fn Decimal::to_interchange_hex_with_encoding(Self, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> (String, DecimalFlags)
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::to_uint64_ctx(Self, DecimalContext, exact? : Bool) -> (UInt64?, DecimalFlags)
pub fn Decimal::to_uint_ctx(Self, DecimalContext, exact? : Bool) -> (UInt32?, DecimalFlags)
pub fn Decimal::total_order(Self, Self) -> Bool
pub fn Decimal::total_order_compare(Self, Self) -> Int32
pub fn Decimal::total_order_mag(Self, Self) -> Bool
pub fn Decimal::try_acos_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_acosh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_acospi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_asin_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_asinh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_asinpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atan2_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atan2pi_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atan_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atanh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_atanpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_compound_ctx(Self, Int32, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_cos_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_cosh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_cospi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp10_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp10m1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp2_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp2m1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_exp_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_expm1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_hypot_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_ln_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log10_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log10p1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log1p_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log2_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_log2p1_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_make(@bigint.BigInt, Int32, Int32, mode? : DecimalRoundingMode) -> Result[Self, @arithmetic.ArithmeticError]
pub fn Decimal::try_pow_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_pown_ctx(Self, Int32, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_powr_ctx(Self, Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_rootn_ctx(Self, Int32, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_rsqrt_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_sin_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_sinh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_sinpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_tan_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_tanh_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::try_tanpi_ctx(Self, DecimalContext) -> Result[(Self, DecimalFlags), @arithmetic.ArithmeticError]
pub fn Decimal::with_precision(Self, Int32, DecimalRoundingMode) -> 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
#derive(Eq)
pub struct DecimalContext {
// private fields
}
pub fn DecimalContext::decimal128(rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::decimal32(rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::decimal64(rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::e_max(Self) -> Int32
pub fn DecimalContext::e_min(Self) -> Int32
pub fn DecimalContext::elementary_precision_limit(Self) -> Int32?
pub fn DecimalContext::equal(Self, Self) -> Bool
pub fn DecimalContext::from_arithmetic_context(@arithmetic.ArithmeticContext) -> Result[Self, @arithmetic.ArithmeticError]
pub fn DecimalContext::new(precision? : Int32, e_max? : Int32, rounding? : DecimalRoundingMode) -> Self
pub fn DecimalContext::not_equal(Self, Self) -> Bool
pub fn DecimalContext::precision(Self) -> Int32
pub fn DecimalContext::refinement_limit(Self) -> Int32?
pub fn DecimalContext::rounding(Self) -> DecimalRoundingMode
pub fn DecimalContext::try_new(precision? : Int32, e_max? : Int32, rounding? : DecimalRoundingMode) -> Result[Self, @arithmetic.ArithmeticError]
pub fn DecimalContext::with_e_max(Self, Int32) -> Self
pub fn DecimalContext::with_elementary_precision_limit(Self, Int32?) -> Self
pub fn DecimalContext::with_precision(Self, Int32) -> Self
pub fn DecimalContext::with_refinement_limit(Self, Int32?) -> Self
pub fn DecimalContext::with_rounding(Self, DecimalRoundingMode) -> Self
#derive(Eq)
pub struct DecimalFlags {
// private fields
}
pub fn DecimalFlags::combine(Self, Self) -> Self
pub fn DecimalFlags::division_by_zero(Self) -> Bool
pub fn DecimalFlags::equal(Self, Self) -> Bool
pub fn DecimalFlags::has_error(Self) -> Bool
pub fn DecimalFlags::inexact(Self) -> Bool
pub fn DecimalFlags::invalid_operation(Self) -> Bool
pub fn DecimalFlags::new() -> Self
pub fn DecimalFlags::not_computed(Self) -> Bool
pub fn DecimalFlags::not_equal(Self, Self) -> Bool
pub fn DecimalFlags::overflow(Self) -> Bool
pub fn DecimalFlags::underflow(Self) -> Bool
pub struct DecimalInterchange {
// private fields
}
pub fn DecimalInterchange::abs(Self) -> Self
pub fn DecimalInterchange::canonical(Self) -> Self
pub fn DecimalInterchange::copy(Self) -> Self
pub fn DecimalInterchange::copy_sign(Self, Self) -> Self
pub fn DecimalInterchange::encoding(Self) -> DecimalInterchangeEncoding
pub fn DecimalInterchange::format(Self) -> DecimalInterchangeFormat
pub fn DecimalInterchange::from_decimal(Decimal, DecimalInterchangeFormat) -> (Self, DecimalFlags)
pub fn DecimalInterchange::from_decimal_with_encoding(Decimal, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> (Self, DecimalFlags)
pub fn DecimalInterchange::from_hex(String, DecimalInterchangeFormat) -> Result[Self, @arithmetic.ArithmeticError]
pub fn DecimalInterchange::from_hex_with_encoding(String, DecimalInterchangeFormat, DecimalInterchangeEncoding) -> Result[Self, @arithmetic.ArithmeticError]
pub fn DecimalInterchange::is_canonical(Self) -> Bool
pub fn DecimalInterchange::neg(Self) -> Self
pub fn DecimalInterchange::to_decimal(Self) -> Decimal
pub fn DecimalInterchange::to_decimal_ctx(Self) -> (Decimal, DecimalFlags)
pub fn DecimalInterchange::to_hex(Self) -> String
#derive(Eq)
pub(all) enum DecimalInterchangeEncoding {
DPD
BID
}
pub fn DecimalInterchangeEncoding::equal(Self, Self) -> Bool
pub fn DecimalInterchangeEncoding::not_equal(Self, Self) -> Bool
#derive(Eq)
pub(all) enum DecimalInterchangeFormat {
Decimal32
Decimal64
Decimal128
}
pub fn DecimalInterchangeFormat::context(Self) -> DecimalContext
pub fn DecimalInterchangeFormat::equal(Self, Self) -> Bool
pub fn DecimalInterchangeFormat::not_equal(Self, Self) -> Bool
#derive(Eq)
pub(all) enum DecimalRoundingMode {
RoundTiesToEven
RoundTiesToAway
RoundTowardZero
RoundTowardPositive
RoundTowardNegative
}
pub fn DecimalRoundingMode::equal(Self, Self) -> Bool
pub fn DecimalRoundingMode::from_arithmetic(@arithmetic.RoundingMode) -> Self?
pub fn DecimalRoundingMode::not_equal(Self, Self) -> Bool
pub fn DecimalRoundingMode::to_arithmetic(Self) -> @arithmetic.RoundingMode?
// Type aliases
// Traits