Module malachite_base::num::conversion::from

source ·
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::{
    PrimitiveFloatFromSignedError, PrimitiveFloatFromUnsignedError, 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, Floor), (100.0, Equal));
assert_eq!(f32::rounding_from(100, Down), (100.0, Equal));
assert_eq!(f32::rounding_from(100, Ceiling), (100.0, Equal));
assert_eq!(f32::rounding_from(100, Up), (100.0, Equal));
assert_eq!(f32::rounding_from(100, Nearest), (100.0, Equal));
assert_eq!(f32::rounding_from(100, Exact), (100.0, Equal));

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

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

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

Structs§

Enums§