Function malachite_base::num::conversion::mantissa_and_exponent::sci_mantissa_and_exponent_round
source · pub fn sci_mantissa_and_exponent_round<T: PrimitiveUnsigned, U: PrimitiveFloat>(
x: T,
rm: RoundingMode,
) -> Option<(U, u64, Ordering)>
Expand description
Returns the scientific mantissa and exponent of an unsinged value. An Ordering
is also
returned, indicating whether the mantissa and exponent correspond to a value less than, equal
to, or greater than the original value.
When $x$ is positive, we can write $x = 2^{e_s}m_s$, where $e_s$ is an integer and $m_s$ is a
rational number with $1 \leq m_s < 2$. We represent the rational mantissa as a float. The
conversion might not be exact, so we round to the nearest float using the provided rounding
mode. If the rounding mode is Exact
but the conversion is not exact, None
is returned.
$$
f(x, r) \approx (\frac{x}{2^{\lfloor \log_2 x \rfloor}}, \lfloor \log_2 x \rfloor).
$$
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
use malachite_base::num::conversion::mantissa_and_exponent::*;
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use std::cmp::Ordering::{self, *};
fn test<T: PrimitiveUnsigned, U: PrimitiveFloat>(
n: T,
rm: RoundingMode,
out: Option<(U, u64, Ordering)>,
) {
assert_eq!(
sci_mantissa_and_exponent_round(n, rm).map(|(m, e, o)| (NiceFloat(m), e, o)),
out.map(|(m, e, o)| (NiceFloat(m), e, o))
);
}
test::<u32, f32>(3, Down, Some((1.5, 1, Equal)));
test::<u32, f32>(3, Ceiling, Some((1.5, 1, Equal)));
test::<u32, f32>(3, Up, Some((1.5, 1, Equal)));
test::<u32, f32>(3, Nearest, Some((1.5, 1, Equal)));
test::<u32, f32>(3, Exact, Some((1.5, 1, Equal)));
test::<u32, f32>(123, Floor, Some((1.921875, 6, Equal)));
test::<u32, f32>(123, Down, Some((1.921875, 6, Equal)));
test::<u32, f32>(123, Ceiling, Some((1.921875, 6, Equal)));
test::<u32, f32>(123, Up, Some((1.921875, 6, Equal)));
test::<u32, f32>(123, Nearest, Some((1.921875, 6, Equal)));
test::<u32, f32>(123, Exact, Some((1.921875, 6, Equal)));
test::<u32, f32>(1000000000, Nearest, Some((1.8626451, 29, Equal)));
test::<u32, f32>(999999999, Nearest, Some((1.8626451, 29, Greater)));