Expand description

Traits for converting between different number types. The traits are WrappingFrom, SaturatingFrom, OverflowingFrom, ConvertibleFrom, and RoundingFrom.

§try_from

use malachite_base::num::conversion::from::{
    PrimitiveFloatFromUnsignedError,
    PrimitiveFloatFromSignedError,
    SignedFromFloatError,
    UnsignedFromFloatError
};
use malachite_base::num::float::NiceFloat;

assert_eq!(NiceFloat::<f32>::try_from(100u8), Ok(NiceFloat(100.0)));
assert_eq!(NiceFloat::<f32>::try_from(u32::MAX), Err(PrimitiveFloatFromUnsignedError));
assert_eq!(NiceFloat::<f32>::try_from(100i8), Ok(NiceFloat(100.0)));
assert_eq!(NiceFloat::<f32>::try_from(i32::MAX), Err(PrimitiveFloatFromSignedError));

assert_eq!(u8::try_from(NiceFloat(100.0f32)), Ok(100));
assert_eq!(
    u8::try_from(NiceFloat(100.1f32)),
    Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
);
assert_eq!(
    u8::try_from(NiceFloat(300.0f32)),
    Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
);
assert_eq!(
    u8::try_from(NiceFloat(-100.0f32)),
    Err(UnsignedFromFloatError::FloatNegative)
);
assert_eq!(i8::try_from(NiceFloat(-100.0f32)), Ok(-100));
assert_eq!(
    i8::try_from(NiceFloat(-200.0f32)),
    Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
);

§wrapping_from

use malachite_base::num::conversion::traits::WrappingFrom;

assert_eq!(u8::wrapping_from(123u8), 123);
assert_eq!(i32::wrapping_from(-5i32), -5);

assert_eq!(u16::wrapping_from(123u8), 123);
assert_eq!(i64::wrapping_from(-5i32), -5);
assert_eq!(u32::wrapping_from(5u64), 5);

assert_eq!(u8::wrapping_from(1000u16), 232);
assert_eq!(u32::wrapping_from(-5i32), 4294967291);
assert_eq!(i32::wrapping_from(3000000000u32), -1294967296);
assert_eq!(i8::wrapping_from(-1000i16), 24);

§saturating_from

use malachite_base::num::conversion::traits::SaturatingFrom;

assert_eq!(u8::saturating_from(123u8), 123);
assert_eq!(i32::saturating_from(-5i32), -5);

assert_eq!(u16::saturating_from(123u8), 123);
assert_eq!(i64::saturating_from(-5i32), -5);
assert_eq!(u32::saturating_from(5u64), 5);

assert_eq!(u8::saturating_from(1000u16), 255);
assert_eq!(u32::saturating_from(-5i32), 0);
assert_eq!(i32::saturating_from(3000000000u32), 2147483647);
assert_eq!(i8::saturating_from(-1000i16), -128);

§overflowing_from

use malachite_base::num::conversion::traits::OverflowingFrom;

assert_eq!(u8::overflowing_from(123u8), (123, false));
assert_eq!(i32::overflowing_from(-5i32), (-5, false));

assert_eq!(u16::overflowing_from(123u8), (123, false));
assert_eq!(i64::overflowing_from(-5i32), (-5, false));
assert_eq!(u32::overflowing_from(5u64), (5, false));

assert_eq!(u8::overflowing_from(1000u16), (232, true));
assert_eq!(u32::overflowing_from(-5i32), (4294967291, true));
assert_eq!(i32::overflowing_from(3000000000u32), (-1294967296, true));
assert_eq!(i8::overflowing_from(-1000i16), (24, true));

§convertible_from

use malachite_base::num::conversion::traits::ConvertibleFrom;

assert_eq!(u8::convertible_from(123u8), true);
assert_eq!(i32::convertible_from(-5i32), true);

assert_eq!(u16::convertible_from(123u8), true);
assert_eq!(i64::convertible_from(-5i32), true);
assert_eq!(u32::convertible_from(5u64), true);

assert_eq!(u8::convertible_from(1000u16), false);
assert_eq!(u32::convertible_from(-5i32), false);
assert_eq!(i32::convertible_from(3000000000u32), false);
assert_eq!(i8::convertible_from(-1000i16), false);

assert_eq!(f32::convertible_from(100u8), true);
assert_eq!(f32::convertible_from(u32::MAX), false);

assert_eq!(u8::convertible_from(100.0f32), true);
assert_eq!(u8::convertible_from(100.1f32), false);
assert_eq!(u8::convertible_from(300.0f32), false);
assert_eq!(u8::convertible_from(-100.0f32), false);

§rounding_from

use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::rounding_modes::RoundingMode;
use std::cmp::Ordering;

assert_eq!(f32::rounding_from(100, RoundingMode::Floor), (100.0, Ordering::Equal));
assert_eq!(f32::rounding_from(100, RoundingMode::Down), (100.0, Ordering::Equal));
assert_eq!(f32::rounding_from(100, RoundingMode::Ceiling), (100.0, Ordering::Equal));
assert_eq!(f32::rounding_from(100, RoundingMode::Up), (100.0, Ordering::Equal));
assert_eq!(f32::rounding_from(100, RoundingMode::Nearest), (100.0, Ordering::Equal));
assert_eq!(f32::rounding_from(100, RoundingMode::Exact), (100.0, Ordering::Equal));

assert_eq!(
    f32::rounding_from(i32::MAX, RoundingMode::Floor),
    (2147483500.0, Ordering::Less)
);
assert_eq!(
    f32::rounding_from(i32::MAX, RoundingMode::Down),
    (2147483500.0, Ordering::Less)
);
assert_eq!(
    f32::rounding_from(i32::MAX, RoundingMode::Ceiling),
    (2147483600.0, Ordering::Greater)
);
assert_eq!(f32::rounding_from(i32::MAX, RoundingMode::Up), (2147483600.0, Ordering::Greater));
assert_eq!(
    f32::rounding_from(i32::MAX, RoundingMode::Nearest),
    (2147483600.0, Ordering::Greater)
);

assert_eq!(u32::rounding_from(100.0f32, RoundingMode::Floor), (100, Ordering::Equal));
assert_eq!(u32::rounding_from(100.0f32, RoundingMode::Down), (100, Ordering::Equal));
assert_eq!(u32::rounding_from(100.0f32, RoundingMode::Ceiling), (100, Ordering::Equal));
assert_eq!(u32::rounding_from(100.0f32, RoundingMode::Up), (100, Ordering::Equal));
assert_eq!(u32::rounding_from(100.0f32, RoundingMode::Nearest), (100, Ordering::Equal));
assert_eq!(u32::rounding_from(100.0f32, RoundingMode::Exact), (100, Ordering::Equal));

assert_eq!(u32::rounding_from(100.5f32, RoundingMode::Floor), (100, Ordering::Less));
assert_eq!(u32::rounding_from(100.5f32, RoundingMode::Down), (100, Ordering::Less));
assert_eq!(u32::rounding_from(100.5f32, RoundingMode::Ceiling), (101, Ordering::Greater));
assert_eq!(u32::rounding_from(100.5f32, RoundingMode::Up), (101, Ordering::Greater));
assert_eq!(u32::rounding_from(100.5f32, RoundingMode::Nearest), (100, Ordering::Less));

Structs§

Enums§