FixedDecimal::Internal
Low-level arithmetic shared by Fixed and UFixed.
Everything here works on unsigned magnitudes (UInt128) plus a separate
sign, with fraction_bits giving the position of the binary point. All
rounding is round-half-to-even unless stated otherwise.
:nodoc:
Constants
Exponents are saturated to this magnitude while parsing.
A decimal fraction with this many leading zeros is below 2**-129, so it rounds to zero for every supported number of fraction bits.
Beyond this many decimal digits an integer part cannot fit in 128 bits.
Instance methods
The signed sum of two magnitudes, where the right-hand one is the
integer integer scaled by 2**fraction_bits and so may need up to 256
bits. Returns {negative, magnitude}, with nil for a magnitude that
does not fit in 128 bits.
The value as a UInt128, or nil if it does not fit.
Three-way comparison of two signed values, each given as a sign, a magnitude and a number of fraction bits.
Compares a fixed-point value with a float exactly; nil for NaN.
Compares left / 2**left_bits with right / 2**right_bits exactly.
Converts the decimal fraction 0.d1d2d3... to fraction_bits binary
digits, returning {bits, guard, sticky} for rounding.
The magnitude nearest to integer + 0.fraction_digits; nil on overflow.
Splits a finite float into {negative, mantissa, exponent} such that its
value is exactly mantissa * 2**exponent.
Splits a finite float into {negative, mantissa, exponent} such that its
value is exactly mantissa * 2**exponent.
dividend * 2**fraction_bits / divisor, rounded half-to-even; nil on
overflow. fraction_bits may be up to 256. Raises DivisionByZeroError when divisor is zero.
True when magnitude shifted left by count bits still fits in 128 bits.
The exact decimal digits of fraction / 2**fraction_bits (which is
below one), without trailing zeros. There are at most fraction_bits.
The magnitude of an integer, as {negative, magnitude}.
left * right / 2**fraction_bits, rounded half-to-even; nil on overflow.
Full 256-bit product of two 128-bit values, as {high, low}.
Parses [+-]digits[.digits][e[+-]digits], where either run of mantissa
digits may be empty (but not both) and single underscores may separate
digits. Returns nil if the syntax is invalid.
Rescales magnitude from from_bits to to_bits fraction bits,
rounding half-to-even; nil on overflow.
Rounds integer.digits to exactly count fraction digits,
half-to-even, returning the new integer part and digits.
Adds one to value when the discarded bits call for it under
round-half-to-even. Returns nil if that overflows.
mantissa * 2**(exponent + fraction_bits) rounded half-to-even; nil on
overflow.
The 256-bit value high:low shifted right by count (0..128) bits,
rounded half-to-even. Returns nil if the result needs more than 128 bits.
The shortest decimal expansion of magnitude / 2**fraction_bits that
parses back to exactly magnitude, as {integer, fraction_digits}.
A candidate parses back to magnitude when its distance from the exact value is below half a unit in the last place, or equal to it when magnitude is even, since ties round to even. For each length, the nearest candidate is the exact expansion rounded to that length, and its distance is either the dropped tail or the tail's complement.
Converts magnitude / 2**fraction_bits to the nearest Float32,
rounding half-to-even (including into the subnormal range). Returns
infinity when the value is out of range.
Converts magnitude / 2**fraction_bits to the nearest Float64,
rounding half-to-even (including into the subnormal range). Returns
infinity when the value is out of range.
Writes magnitude / 2**fraction_bits in decimal. With precision, exactly
that many fraction digits (rounded half-to-even); without it, the shortest
round-tripping form, which always has at least one fraction digit unless
fraction_bits is zero.
Writes a fully formatted, padded number; see Fixed#format.