Std. Decimal
(Cure Standard Library v0.37.2)
View Source
Arbitrary-precision decimal arithmetic.
A finite value is sign * coefficient * 10 ^ exponent. Int is an
arbitrary-precision BEAM integer, so the coefficient never silently wraps
or loses decimal digits. Arithmetic is performed with integer operations;
Float is used only by the explicit conversion functions.
The default context follows Elixir Decimal: 28 significant digits with half-away-from-zero rounding.
Examples
use Std.Decimal
let total = add(from_int(10), from_parts(Positive, 25, -2))
to_string(total) # => "10.25"
round(divide(from_int(1), from_int(3)), 4, HalfUp)
Summary
Functions
Return the non-negative magnitude of d, preserving NaN values.
Add two decimals with exact aligned-exponent arithmetic.
Round d to at most ctx.precision significant digits using ctx.rounding.
Return the non-negative integer coefficient of a finite decimal, or 0 for non-finite values.
Numerically compare a and b, returning Error(InvalidOperation) if either is NaN.
Compare a and b within a non-negative tolerance threshold, returning
Ok(EqualTo) when |a - b| <= threshold and Error(InvalidOperation) if threshold is negative or NaN.
Build an arithmetic context with precision (clamped to at least 1) and rounding.
Return the default arithmetic context (28 significant digits, HalfUp rounding).
Divide a by b using default_context().
Compute the truncated integer quotient of a / b, or NaN on invalid operands or division by zero.
Compute both the truncated integer quotient and remainder of a divided by b as a tuple.
Divide a by b, rounding the quotient to the precision and rounding mode in ctx.
Return true if a and b are numerically equal (always false if either is NaN).
Return true if a and b are within threshold of each other.
Return the base-10 exponent of a finite decimal, or 0 for non-finite values.
Construct a finite Decimal from s, coefficient, and exponent,
flipping the sign if coefficient is negative.
Convert a Float to a Decimal via its string representation, returning NaN on failure.
Convert an exact Int to a finite Decimal with exponent 0.
Construct a finite Decimal from s, the absolute value of coefficient, and exponent.
Parse a complete decimal string (ignoring surrounding whitespace),
returning Error(input) if invalid or if trailing characters remain.
Return true if a is strictly greater than b (always false if either is NaN).
Return true if a is greater than or equal to b (always false if either is NaN).
Return positive infinity (+Infinity).
Return true if d is positive or negative infinity.
Return true if d is finite and represents an exact integer value.
Return true if d is either a quiet or signalling NaN.
Return true if d is finite and strictly less than zero.
Return true if d is finite and strictly greater than zero.
Return true if d is finite and equal to zero.
Return true if a is strictly less than b (always false if either is NaN).
Return true if a is less than or equal to b (always false if either is NaN).
Return the larger of a and b, preferring the non-NaN operand if one is NaN.
Return the smaller of a and b, preferring the non-NaN operand if one is NaN.
Multiply two decimals with exact coefficient multiplication.
Return a quiet NaN with no diagnostic payload.
Return negative infinity (-Infinity).
Negate the sign of d, leaving NaN values unchanged.
Strip trailing fractional zeros from a finite Decimal and normalize zero's exponent to 0.
Return finite decimal one (1).
Parse a decimal prefix and return the unconsumed suffix.
Compute the remainder of truncated integer division of a by b, or NaN on invalid operands.
Round d to places decimal places using mode.
Return the number of fractional decimal places in d (-exponent(d) when negative, otherwise 0).
Return the Sign of d (Positive for NaN values).
Compute the square root of d, rounding to the precision and rounding mode in ctx.
Compute the square root of d using default_context().
Subtract b from a (add(a, negate(b))).
Convert a finite Decimal to an IEEE 754 Float, returning Error(InvalidOperation) if non-finite.
Convert an exact integral Decimal to Int, returning Error(OutOfRange) if
fractional or Error(InvalidOperation) if non-finite.
Format d as a standard decimal string, switching to scientific notation for extreme exponents.
Format d using its raw coefficient and exponent without normalization (e.g. "125E-2").
Format d in normalized scientific notation (e.g. "1.25E+2").
Format d in XSD canonical decimal form, always including a decimal point and no exponent.
Return finite decimal zero (0).
Types
struct Context
?
?
enum Decimal
Finite
Infinity
QuietNaN
SignallingNaN
enum DecimalError
InvalidOperation
OutOfRange
struct DigitScan
?
?
?
struct ExponentScan
?
?
?
struct FiniteDecimal
?
?
?
enum NaNPayload
NoPayload
Diagnostic
enum RoundingMode
Down
Up
Ceiling
Floor
HalfUp
HalfEven
HalfDown
enum Sign
Positive
Negative
Functions
fn abs_int(value: Int) -> Int
fn absolute_value(d: Decimal) -> Decimal
Return the non-negative magnitude of d, preserving NaN values.
fn add(a: Decimal, b: Decimal) -> Decimal
Add two decimals with exact aligned-exponent arithmetic.
fn add_finite(a: FiniteDecimal, b: FiniteDecimal) -> Decimal
fn aligned_division(a: Decimal, b: Decimal) -> Result(Tuple(Decimal, Decimal), DecimalError)
fn aligned_finite_division(a: FiniteDecimal, b: FiniteDecimal) -> Result(Tuple(Decimal, Decimal), DecimalError)
fn apply_context(d: Decimal, ctx: Context) -> Decimal
Round d to at most ctx.precision significant digits using ctx.rounding.
fn apply_sign(s: Sign, value: Int) -> Int
fn coefficient(d: Decimal) -> Int
Return the non-negative integer coefficient of a finite decimal, or 0 for non-finite values.
fn compare(a: Decimal, b: Decimal) -> Result(Ordering, DecimalError)
Numerically compare a and b, returning Error(InvalidOperation) if either is NaN.
fn compare_finite(a: FiniteDecimal, b: FiniteDecimal) -> Ordering
fn compare_int(a: Int, b: Int) -> Ordering
fn compare_ordered(a: Decimal, b: Decimal) -> Ordering
fn compare_signs(a: Sign, b: Sign) -> Ordering
fn compare_with_threshold(a: Decimal, b: Decimal, threshold: Decimal) -> Result(Ordering, DecimalError)
Compare a and b within a non-negative tolerance threshold, returning
Ok(EqualTo) when |a - b| <= threshold and Error(InvalidOperation) if threshold is negative or NaN.
fn context(precision: Int, rounding: RoundingMode) -> Context
Build an arithmetic context with precision (clamped to at least 1) and rounding.
fn decimal_json_decimal(literal: DecimalLiteral) -> Result(Decimal, DecodeError)
fn decimal_json_integer(literal: IntegerLiteral) -> Result(Decimal, DecodeError)
fn decimal_json_natural(literal: NaturalLiteral) -> Result(Decimal, DecodeError)
fn default_context() -> Context
Return the default arithmetic context (28 significant digits, HalfUp rounding).
fn digit_count(value: Int) -> Int
fn digit_value(char: Char) -> Int
fn divide(a: Decimal, b: Decimal) -> Decimal
Divide a by b using default_context().
fn divide_finite(a: FiniteDecimal, b: FiniteDecimal, ctx: Context) -> Decimal
fn divide_integer(a: Decimal, b: Decimal) -> Decimal
Compute the truncated integer quotient of a / b, or NaN on invalid operands or division by zero.
fn divide_remainder(a: Decimal, b: Decimal) -> Tuple(Decimal, Decimal)
Compute both the truncated integer quotient and remainder of a divided by b as a tuple.
fn divide_with_context(a: Decimal, b: Decimal, ctx: Context) -> Decimal
Divide a by b, rounding the quotient to the precision and rounding mode in ctx.
fn eq(a: Decimal, b: Decimal) -> Bool
Return true if a and b are numerically equal (always false if either is NaN).
fn eq_with_threshold(a: Decimal, b: Decimal, threshold: Decimal) -> Bool
Return true if a and b are within threshold of each other.
fn exponent(d: Decimal) -> Int
Return the base-10 exponent of a finite decimal, or 0 for non-finite values.
fn exponent_marker(exp: Int) -> String
fn finish_exponent(s: Sign, coefficient: Int, base_exponent: Int, chars: List(Char), unparsed: List(Char), original: String) -> Result(Tuple(Decimal, String), String)
fn finish_parse(s: Sign, digits: DigitScan, base_exponent: Int, original: String) -> Result(Tuple(Decimal, String), String)
fn finite(s: Sign, coefficient: Int, exponent: Int) -> Decimal
Construct a finite Decimal from s, coefficient, and exponent,
flipping the sign if coefficient is negative.
fn finite_to_string(value: FiniteDecimal) -> String
fn flip_sign(s: Sign) -> Sign
fn format_decimal(prefix: String, digits: String, position: Int) -> String
fn format_scientific(prefix: String, digits: String, adjusted: Int) -> String
fn format_zero(prefix: String, exp: Int) -> String
fn from_float(value: Float) -> Decimal
Convert a Float to a Decimal via its string representation, returning NaN on failure.
fn from_int(value: Int) -> Decimal
Convert an exact Int to a finite Decimal with exponent 0.
fn from_parts(s: Sign, coefficient: Int, exponent: Int) -> Decimal
Construct a finite Decimal from s, the absolute value of coefficient, and exponent.
fn from_string(input: String) -> Result(Decimal, String)
Parse a complete decimal string (ignoring surrounding whitespace),
returning Error(input) if invalid or if trailing characters remain.
fn gt(a: Decimal, b: Decimal) -> Bool
Return true if a is strictly greater than b (always false if either is NaN).
fn gte(a: Decimal, b: Decimal) -> Bool
Return true if a is greater than or equal to b (always false if either is NaN).
fn infinity() -> Decimal
Return positive infinity (+Infinity).
fn int_to_float(value: Int) -> Float
fn integer_sqrt(n: Int) -> Int
fn integer_sqrt_loop(n: Int, guess: Int) -> Int
fn is_inf(d: Decimal) -> Bool
Return true if d is positive or negative infinity.
fn is_integer(d: Decimal) -> Bool
Return true if d is finite and represents an exact integer value.
fn is_nan(d: Decimal) -> Bool
Return true if d is either a quiet or signalling NaN.
fn is_negative(d: Decimal) -> Bool
Return true if d is finite and strictly less than zero.
fn is_positive(d: Decimal) -> Bool
Return true if d is finite and strictly greater than zero.
fn is_zero(d: Decimal) -> Bool
Return true if d is finite and equal to zero.
fn lt(a: Decimal, b: Decimal) -> Bool
Return true if a is strictly less than b (always false if either is NaN).
fn lte(a: Decimal, b: Decimal) -> Bool
Return true if a is less than or equal to b (always false if either is NaN).
fn max(a: Decimal, b: Decimal) -> Decimal
Return the larger of a and b, preferring the non-NaN operand if one is NaN.
fn max_equal(a: Decimal, b: Decimal) -> Decimal
fn max_int(a: Int, b: Int) -> Int
fn min(a: Decimal, b: Decimal) -> Decimal
Return the smaller of a and b, preferring the non-NaN operand if one is NaN.
fn min_equal(a: Decimal, b: Decimal) -> Decimal
fn min_int(a: Int, b: Int) -> Int
fn mult_signs(a: Sign, b: Sign) -> Sign
fn multiply(a: Decimal, b: Decimal) -> Decimal
Multiply two decimals with exact coefficient multiplication.
fn multiply_finite_infinity(value: FiniteDecimal, sb: Sign) -> Decimal
fn multiply_infinity(sa: Sign, b: Decimal) -> Decimal
fn nan() -> Decimal
Return a quiet NaN with no diagnostic payload.
fn neg_infinity() -> Decimal
Return negative infinity (-Infinity).
fn negate(d: Decimal) -> Decimal
Negate the sign of d, leaving NaN values unchanged.
fn normalize(d: Decimal) -> Decimal
Strip trailing fractional zeros from a finite Decimal and normalize zero's exponent to 0.
fn normalize_finite(value: FiniteDecimal) -> Decimal
fn one() -> Decimal
Return finite decimal one (1).
fn parse(input: String) -> Result(Tuple(Decimal, String), String)
Parse a decimal prefix and return the unconsumed suffix.
fn parse_numeric(chars: List(Char), original: String) -> Result(Tuple(Decimal, String), String)
fn parse_sign(chars: List(Char)) -> Tuple(Sign, List(Char))
fn pow10(n: Int) -> Int
fn remainder(a: Decimal, b: Decimal) -> Decimal
Compute the remainder of truncated integer division of a by b, or NaN on invalid operands.
fn reverse_order(order: Ordering) -> Ordering
fn round(d: Decimal, places: Int, mode: RoundingMode) -> Decimal
Round d to places decimal places using mode.
fn round_finite(value: FiniteDecimal, target_exponent: Int, mode: RoundingMode) -> Decimal
fn round_significant(value: FiniteDecimal, shift: Int, mode: RoundingMode) -> Decimal
fn rounded_coefficient(quotient: Int, remainder: Int, divisor: Int, s: Sign, mode: RoundingMode) -> Int
fn same_sign(a: Sign, b: Sign) -> Bool
fn scale(d: Decimal) -> Int
Return the number of fractional decimal places in d (-exponent(d) when negative, otherwise 0).
fn scan_digits(chars: List(Char), value: Int, count: Int) -> DigitScan
fn scan_exponent(chars: List(Char)) -> ExponentScan
fn should_increment(quotient: Int, remainder: Int, divisor: Int, s: Sign, mode: RoundingMode) -> Bool
fn sign(d: Decimal) -> Sign
Return the Sign of d (Positive for NaN values).
fn sign_prefix(s: Sign) -> String
fn signed_coefficient(value: FiniteDecimal) -> Int
fn split_chars(chars: List(Char), count: Int, reversed: List(Char)) -> Tuple(String, String)
fn sqrt_finite(value: FiniteDecimal, ctx: Context) -> Decimal
fn sqrt_with_context(d: Decimal, ctx: Context) -> Decimal
Compute the square root of d, rounding to the precision and rounding mode in ctx.
fn square_root(d: Decimal) -> Decimal
Compute the square root of d using default_context().
fn string_equal(a: String, b: String) -> Bool
fn subtract(a: Decimal, b: Decimal) -> Decimal
Subtract b from a (add(a, negate(b))).
fn to_float(d: Decimal) -> Result(Float, DecimalError)
Convert a finite Decimal to an IEEE 754 Float, returning Error(InvalidOperation) if non-finite.
fn to_int(d: Decimal) -> Result(Int, DecimalError)
Convert an exact integral Decimal to Int, returning Error(OutOfRange) if
fractional or Error(InvalidOperation) if non-finite.
fn to_string(d: Decimal) -> String
Format d as a standard decimal string, switching to scientific notation for extreme exponents.
fn to_string_raw(d: Decimal) -> String
Format d using its raw coefficient and exponent without normalization (e.g. "125E-2").
fn to_string_scientific(d: Decimal) -> String
Format d in normalized scientific notation (e.g. "1.25E+2").
fn to_string_xsd(d: Decimal) -> String
Format d in XSD canonical decimal form, always including a decimal point and no exponent.
fn zero() -> Decimal
Return finite decimal zero (0).