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

 struct Context
   ?
   ?

enum Decimal

 enum Decimal
   Finite
   Infinity
   QuietNaN
   SignallingNaN

enum DecimalError

 enum DecimalError
   InvalidOperation
   OutOfRange

struct DigitScan

 struct DigitScan
   ?
   ?
   ?

struct ExponentScan

 struct ExponentScan
   ?
   ?
   ?

struct FiniteDecimal

 struct FiniteDecimal
   ?
   ?
   ?

enum NaNPayload

 enum NaNPayload
   NoPayload
   Diagnostic

enum RoundingMode

 enum RoundingMode
   Down
   Up
   Ceiling
   Floor
   HalfUp
   HalfEven
   HalfDown

enum Sign

 enum Sign
   Positive
   Negative

Functions

abs_int(value: Int) -> Int

 fn abs_int(value: Int) -> Int

absolute_value(d: Decimal) -> Decimal

 fn absolute_value(d: Decimal) -> Decimal

Return the non-negative magnitude of d, preserving NaN values.

add(a: Decimal, b: Decimal) -> Decimal

 fn add(a: Decimal, b: Decimal) -> Decimal

Add two decimals with exact aligned-exponent arithmetic.

add_finite(a: FiniteDecimal, b: FiniteDecimal) -> Decimal

 fn add_finite(a: FiniteDecimal, b: FiniteDecimal) -> Decimal

aligned_division(a: Decimal, b: Decimal) -> Result(Tuple(Decimal, Decimal), DecimalError)

 fn aligned_division(a: Decimal, b: Decimal) -> Result(Tuple(Decimal, Decimal), DecimalError)

aligned_finite_division(a: FiniteDecimal, b: FiniteDecimal) -> Result(Tuple(Decimal, Decimal), DecimalError)

 fn aligned_finite_division(a: FiniteDecimal, b: FiniteDecimal) -> Result(Tuple(Decimal, Decimal), DecimalError)

apply_context(d: Decimal, ctx: Context) -> Decimal

 fn apply_context(d: Decimal, ctx: Context) -> Decimal

Round d to at most ctx.precision significant digits using ctx.rounding.

apply_sign(s: Sign, value: Int) -> Int

 fn apply_sign(s: Sign, value: Int) -> Int

coefficient(d: Decimal) -> Int

 fn coefficient(d: Decimal) -> Int

Return the non-negative integer coefficient of a finite decimal, or 0 for non-finite values.

compare(a: Decimal, b: Decimal) -> Result(Ordering, DecimalError)

 fn compare(a: Decimal, b: Decimal) -> Result(Ordering, DecimalError)

Numerically compare a and b, returning Error(InvalidOperation) if either is NaN.

compare_finite(a: FiniteDecimal, b: FiniteDecimal) -> Ordering

 fn compare_finite(a: FiniteDecimal, b: FiniteDecimal) -> Ordering

compare_int(a: Int, b: Int) -> Ordering

 fn compare_int(a: Int, b: Int) -> Ordering

compare_ordered(a: Decimal, b: Decimal) -> Ordering

 fn compare_ordered(a: Decimal, b: Decimal) -> Ordering

compare_signs(a: Sign, b: Sign) -> Ordering

 fn compare_signs(a: Sign, b: Sign) -> Ordering

compare_with_threshold(a: Decimal, b: Decimal, threshold: Decimal) -> Result(Ordering, DecimalError)

 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.

context(precision: Int, rounding: RoundingMode) -> Context

 fn context(precision: Int, rounding: RoundingMode) -> Context

Build an arithmetic context with precision (clamped to at least 1) and rounding.

decimal_json_decimal(literal: DecimalLiteral) -> Result(Decimal, DecodeError)

 fn decimal_json_decimal(literal: DecimalLiteral) -> Result(Decimal, DecodeError)

decimal_json_integer(literal: IntegerLiteral) -> Result(Decimal, DecodeError)

 fn decimal_json_integer(literal: IntegerLiteral) -> Result(Decimal, DecodeError)

decimal_json_natural(literal: NaturalLiteral) -> Result(Decimal, DecodeError)

 fn decimal_json_natural(literal: NaturalLiteral) -> Result(Decimal, DecodeError)

default_context() -> Context

 fn default_context() -> Context

Return the default arithmetic context (28 significant digits, HalfUp rounding).

digit_count(value: Int) -> Int

 fn digit_count(value: Int) -> Int

digit_value(char: Char) -> Int

 fn digit_value(char: Char) -> Int

divide(a: Decimal, b: Decimal) -> Decimal

 fn divide(a: Decimal, b: Decimal) -> Decimal

Divide a by b using default_context().

divide_finite(a: FiniteDecimal, b: FiniteDecimal, ctx: Context) -> Decimal

 fn divide_finite(a: FiniteDecimal, b: FiniteDecimal, ctx: Context) -> Decimal

divide_integer(a: Decimal, b: Decimal) -> 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.

divide_remainder(a: Decimal, b: Decimal) -> Tuple(Decimal, Decimal)

 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.

divide_with_context(a: Decimal, b: Decimal, ctx: Context) -> Decimal

 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.

eq(a: Decimal, b: Decimal) -> Bool

 fn eq(a: Decimal, b: Decimal) -> Bool

Return true if a and b are numerically equal (always false if either is NaN).

eq_with_threshold(a: Decimal, b: Decimal, threshold: Decimal) -> Bool

 fn eq_with_threshold(a: Decimal, b: Decimal, threshold: Decimal) -> Bool

Return true if a and b are within threshold of each other.

exponent(d: Decimal) -> Int

 fn exponent(d: Decimal) -> Int

Return the base-10 exponent of a finite decimal, or 0 for non-finite values.

exponent_marker(exp: Int) -> String

 fn exponent_marker(exp: Int) -> String

finish_exponent(s: Sign, coefficient: Int, base_exponent: Int, chars: List(Char), unparsed: List(Char), original: String) -> Result(Tuple(Decimal, String), String)

 fn finish_exponent(s: Sign, coefficient: Int, base_exponent: Int, chars: List(Char), unparsed: List(Char), original: String) -> Result(Tuple(Decimal, String), String)

finish_parse(s: Sign, digits: DigitScan, base_exponent: Int, original: String) -> Result(Tuple(Decimal, String), String)

 fn finish_parse(s: Sign, digits: DigitScan, base_exponent: Int, original: String) -> Result(Tuple(Decimal, String), String)

finite(s: Sign, coefficient: Int, exponent: Int) -> Decimal

 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.

finite_to_string(value: FiniteDecimal) -> String

 fn finite_to_string(value: FiniteDecimal) -> String

flip_sign(s: Sign) -> Sign

 fn flip_sign(s: Sign) -> Sign

format_decimal(prefix: String, digits: String, position: Int) -> String

 fn format_decimal(prefix: String, digits: String, position: Int) -> String

format_scientific(prefix: String, digits: String, adjusted: Int) -> String

 fn format_scientific(prefix: String, digits: String, adjusted: Int) -> String

format_zero(prefix: String, exp: Int) -> String

 fn format_zero(prefix: String, exp: Int) -> String

from_float(value: Float) -> Decimal

 fn from_float(value: Float) -> Decimal

Convert a Float to a Decimal via its string representation, returning NaN on failure.

from_int(value: Int) -> Decimal

 fn from_int(value: Int) -> Decimal

Convert an exact Int to a finite Decimal with exponent 0.

from_parts(s: Sign, coefficient: Int, exponent: Int) -> Decimal

 fn from_parts(s: Sign, coefficient: Int, exponent: Int) -> Decimal

Construct a finite Decimal from s, the absolute value of coefficient, and exponent.

from_string(input: String) -> Result(Decimal, String)

 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.

gt(a: Decimal, b: Decimal) -> Bool

 fn gt(a: Decimal, b: Decimal) -> Bool

Return true if a is strictly greater than b (always false if either is NaN).

gte(a: Decimal, b: Decimal) -> Bool

 fn gte(a: Decimal, b: Decimal) -> Bool

Return true if a is greater than or equal to b (always false if either is NaN).

infinity() -> Decimal

 fn infinity() -> Decimal

Return positive infinity (+Infinity).

int_to_float(value: Int) -> Float

 fn int_to_float(value: Int) -> Float

integer_sqrt(n: Int) -> Int

 fn integer_sqrt(n: Int) -> Int

integer_sqrt_loop(n: Int, guess: Int) -> Int

 fn integer_sqrt_loop(n: Int, guess: Int) -> Int

is_inf(d: Decimal) -> Bool

 fn is_inf(d: Decimal) -> Bool

Return true if d is positive or negative infinity.

is_integer(d: Decimal) -> Bool

 fn is_integer(d: Decimal) -> Bool

Return true if d is finite and represents an exact integer value.

is_nan(d: Decimal) -> Bool

 fn is_nan(d: Decimal) -> Bool

Return true if d is either a quiet or signalling NaN.

is_negative(d: Decimal) -> Bool

 fn is_negative(d: Decimal) -> Bool

Return true if d is finite and strictly less than zero.

is_positive(d: Decimal) -> Bool

 fn is_positive(d: Decimal) -> Bool

Return true if d is finite and strictly greater than zero.

is_zero(d: Decimal) -> Bool

 fn is_zero(d: Decimal) -> Bool

Return true if d is finite and equal to zero.

lt(a: Decimal, b: Decimal) -> Bool

 fn lt(a: Decimal, b: Decimal) -> Bool

Return true if a is strictly less than b (always false if either is NaN).

lte(a: Decimal, b: Decimal) -> Bool

 fn lte(a: Decimal, b: Decimal) -> Bool

Return true if a is less than or equal to b (always false if either is NaN).

max(a: Decimal, b: Decimal) -> Decimal

 fn max(a: Decimal, b: Decimal) -> Decimal

Return the larger of a and b, preferring the non-NaN operand if one is NaN.

max_equal(a: Decimal, b: Decimal) -> Decimal

 fn max_equal(a: Decimal, b: Decimal) -> Decimal

max_int(a: Int, b: Int) -> Int

 fn max_int(a: Int, b: Int) -> Int

min(a: Decimal, b: Decimal) -> Decimal

 fn min(a: Decimal, b: Decimal) -> Decimal

Return the smaller of a and b, preferring the non-NaN operand if one is NaN.

min_equal(a: Decimal, b: Decimal) -> Decimal

 fn min_equal(a: Decimal, b: Decimal) -> Decimal

min_int(a: Int, b: Int) -> Int

 fn min_int(a: Int, b: Int) -> Int

mult_signs(a: Sign, b: Sign) -> Sign

 fn mult_signs(a: Sign, b: Sign) -> Sign

multiply(a: Decimal, b: Decimal) -> Decimal

 fn multiply(a: Decimal, b: Decimal) -> Decimal

Multiply two decimals with exact coefficient multiplication.

multiply_finite_infinity(value: FiniteDecimal, sb: Sign) -> Decimal

 fn multiply_finite_infinity(value: FiniteDecimal, sb: Sign) -> Decimal

multiply_infinity(sa: Sign, b: Decimal) -> Decimal

 fn multiply_infinity(sa: Sign, b: Decimal) -> Decimal

nan() -> Decimal

 fn nan() -> Decimal

Return a quiet NaN with no diagnostic payload.

neg_infinity() -> Decimal

 fn neg_infinity() -> Decimal

Return negative infinity (-Infinity).

negate(d: Decimal) -> Decimal

 fn negate(d: Decimal) -> Decimal

Negate the sign of d, leaving NaN values unchanged.

normalize(d: Decimal) -> Decimal

 fn normalize(d: Decimal) -> Decimal

Strip trailing fractional zeros from a finite Decimal and normalize zero's exponent to 0.

normalize_finite(value: FiniteDecimal) -> Decimal

 fn normalize_finite(value: FiniteDecimal) -> Decimal

one() -> Decimal

 fn one() -> Decimal

Return finite decimal one (1).

parse(input: String) -> Result(Tuple(Decimal, String), String)

 fn parse(input: String) -> Result(Tuple(Decimal, String), String)

Parse a decimal prefix and return the unconsumed suffix.

parse_numeric(chars: List(Char), original: String) -> Result(Tuple(Decimal, String), String)

 fn parse_numeric(chars: List(Char), original: String) -> Result(Tuple(Decimal, String), String)

parse_sign(chars: List(Char)) -> Tuple(Sign, List(Char))

 fn parse_sign(chars: List(Char)) -> Tuple(Sign, List(Char))

pow10(n: Int) -> Int

 fn pow10(n: Int) -> Int

remainder(a: Decimal, b: Decimal) -> Decimal

 fn remainder(a: Decimal, b: Decimal) -> Decimal

Compute the remainder of truncated integer division of a by b, or NaN on invalid operands.

reverse_order(order: Ordering) -> Ordering

 fn reverse_order(order: Ordering) -> Ordering

round(d: Decimal, places: Int, mode: RoundingMode) -> Decimal

 fn round(d: Decimal, places: Int, mode: RoundingMode) -> Decimal

Round d to places decimal places using mode.

round_finite(value: FiniteDecimal, target_exponent: Int, mode: RoundingMode) -> Decimal

 fn round_finite(value: FiniteDecimal, target_exponent: Int, mode: RoundingMode) -> Decimal

round_significant(value: FiniteDecimal, shift: Int, mode: RoundingMode) -> Decimal

 fn round_significant(value: FiniteDecimal, shift: Int, mode: RoundingMode) -> Decimal

rounded_coefficient(quotient: Int, remainder: Int, divisor: Int, s: Sign, mode: RoundingMode) -> Int

 fn rounded_coefficient(quotient: Int, remainder: Int, divisor: Int, s: Sign, mode: RoundingMode) -> Int

same_sign(a: Sign, b: Sign) -> Bool

 fn same_sign(a: Sign, b: Sign) -> Bool

scale(d: Decimal) -> Int

 fn scale(d: Decimal) -> Int

Return the number of fractional decimal places in d (-exponent(d) when negative, otherwise 0).

scan_digits(chars: List(Char), value: Int, count: Int) -> DigitScan

 fn scan_digits(chars: List(Char), value: Int, count: Int) -> DigitScan

scan_exponent(chars: List(Char)) -> ExponentScan

 fn scan_exponent(chars: List(Char)) -> ExponentScan

should_increment(quotient: Int, remainder: Int, divisor: Int, s: Sign, mode: RoundingMode) -> Bool

 fn should_increment(quotient: Int, remainder: Int, divisor: Int, s: Sign, mode: RoundingMode) -> Bool

sign(d: Decimal) -> Sign

 fn sign(d: Decimal) -> Sign

Return the Sign of d (Positive for NaN values).

sign_prefix(s: Sign) -> String

 fn sign_prefix(s: Sign) -> String

signed_coefficient(value: FiniteDecimal) -> Int

 fn signed_coefficient(value: FiniteDecimal) -> Int

split_chars(chars: List(Char), count: Int, reversed: List(Char)) -> Tuple(String, String)

 fn split_chars(chars: List(Char), count: Int, reversed: List(Char)) -> Tuple(String, String)

sqrt_finite(value: FiniteDecimal, ctx: Context) -> Decimal

 fn sqrt_finite(value: FiniteDecimal, ctx: Context) -> Decimal

sqrt_with_context(d: Decimal, 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.

square_root(d: Decimal) -> Decimal

 fn square_root(d: Decimal) -> Decimal

Compute the square root of d using default_context().

string_equal(a: String, b: String) -> Bool

 fn string_equal(a: String, b: String) -> Bool

subtract(a: Decimal, b: Decimal) -> Decimal

 fn subtract(a: Decimal, b: Decimal) -> Decimal

Subtract b from a (add(a, negate(b))).

to_float(d: Decimal) -> Result(Float, DecimalError)

 fn to_float(d: Decimal) -> Result(Float, DecimalError)

Convert a finite Decimal to an IEEE 754 Float, returning Error(InvalidOperation) if non-finite.

to_int(d: Decimal) -> Result(Int, DecimalError)

 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.

to_string(d: Decimal) -> String

 fn to_string(d: Decimal) -> String

Format d as a standard decimal string, switching to scientific notation for extreme exponents.

to_string_raw(d: Decimal) -> String

 fn to_string_raw(d: Decimal) -> String

Format d using its raw coefficient and exponent without normalization (e.g. "125E-2").

to_string_scientific(d: Decimal) -> String

 fn to_string_scientific(d: Decimal) -> String

Format d in normalized scientific notation (e.g. "1.25E+2").

to_string_xsd(d: Decimal) -> String

 fn to_string_xsd(d: Decimal) -> String

Format d in XSD canonical decimal form, always including a decimal point and no exponent.

zero() -> Decimal

 fn zero() -> Decimal

Return finite decimal zero (0).