malachite-base 0.3.2

A collection of utilities, including new arithmetic traits and iterators that generate all values of a type
Documentation
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::test_util::generators::primitive_float_gen;

fn is_power_of_2_helper<T: PrimitiveFloat>() {
    let test = |n: T, out| {
        assert_eq!(n.is_power_of_2(), out);
    };
    test(T::ZERO, false);
    test(T::NEGATIVE_ZERO, false);
    test(T::POSITIVE_INFINITY, false);
    test(T::NEGATIVE_INFINITY, false);
    test(T::NAN, false);
    test(T::NEGATIVE_ONE, false);
    test(T::from(1.5f32), false);
    test(T::from(-1.5f32), false);

    test(T::ONE, true);
    test(T::TWO, true);
    test(T::from(4.0f32), true);
    test(T::from(0.5f32), true);
    test(T::from(0.25f32), true);
}

#[test]
fn test_is_power_of_2() {
    apply_fn_to_primitive_floats!(is_power_of_2_helper);
}

fn is_power_of_2_properties_helper<T: PrimitiveFloat>() {
    primitive_float_gen::<T>().test_properties(|f| {
        if f.is_power_of_2() {
            assert_eq!(f.precision(), 1);
            assert_eq!(T::power_of_2(f.checked_log_base_2().unwrap()), f);
        }
    });
}

#[test]
fn is_power_of_2_properties() {
    apply_fn_to_primitive_floats!(is_power_of_2_properties_helper);
}