Expand description

An implementation of SciMantissaAndExponent, a trait for converting numbers to and from a mantissa-and-exponent representation.

See PrimitiveFloat for a description of the different types of mantissas and exponents.

sci_mantissa_and_exponent

extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::conversion::traits::SciMantissaAndExponent;
use malachite_base::num::float::NiceFloat;
use malachite_q::Rational;

let test = |n: Rational, mantissa: f32, exponent: i64| {
    let (m, e) = n.clone().sci_mantissa_and_exponent();
    assert_eq!(NiceFloat(m), NiceFloat(mantissa));
    assert_eq!(e, exponent);

    let (m, e) = (&n).sci_mantissa_and_exponent();
    assert_eq!(NiceFloat(m), NiceFloat(mantissa));
    assert_eq!(e, exponent);
};
test(Rational::from(3u32), 1.5, 1);
test(Rational::from(123u32), 1.921875, 6);
test(Rational::from_signeds(1, 123), 1.0406504, -7);
test(Rational::from_signeds(22, 7), 1.5714285, 1);

from_sci_mantissa_and_exponent

extern crate malachite_base;

use malachite_base::num::conversion::traits::SciMantissaAndExponent;
use malachite_q::Rational;

let test = |mantissa: f32, exponent: i64, out: Option<Rational>| {
    assert_eq!(
        <&Rational as SciMantissaAndExponent<_, _, _>>::from_sci_mantissa_and_exponent(
            mantissa, exponent
        ),
        out
    );

    assert_eq!(
        <Rational as SciMantissaAndExponent<_, _, _>>::from_sci_mantissa_and_exponent(
            mantissa, exponent
        ),
        out
    );
};
test(1.5, 1, Some(Rational::from(3u32)));
test(1.51, 1, Some(Rational::from_signeds(6333399, 2097152)));
test(1.921875, 6, Some(Rational::from(123u32)));

test(2.0, 1, None);
test(10.0, 1, None);
test(0.5, 1, None);