Function core::num::dec2flt::algorithm::algorithm_m
[−]
[src]
pub fn algorithm_m<T: RawFloat>(f: &Big, e: i16) -> T
dec2flt
): internal routines only exposed for testing
Conceptually, Algorithm M is the simplest way to convert a decimal to a float.
We form a ratio that is equal to f * 10^e
, then throwing in powers of two until it gives
a valid float significand. The binary exponent k
is the number of times we multiplied
numerator or denominator by two, i.e., at all times f * 10^e
equals (u / v) * 2^k
.
When we have found out significand, we only need to round by inspecting the remainder of the
division, which is done in helper functions further below.
This algorithm is super slow, even with the optimization described in quick_start()
.
However, it's the simplest of the algorithms to adapt for overflow, underflow, and subnormal
results. This implementation takes over when Bellerophon and Algorithm R are overwhelmed.
Detecting underflow and overflow is easy: The ratio still isn't an in-range significand,
yet the minimum/maximum exponent has been reached. In the case of overflow, we simply return
infinity.
Handling underflow and subnormals is trickier. One big problem is that, with the minimum exponent, the ratio might still be too large for a significand. See underflow() for details.