Expand description

Implementations of traits for converting a Natural to a primitive integer.

The traits are TryFrom, ConvertibleFrom, OverflowingFrom, SaturatingFrom, and WrappingFrom.

§try_from

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::natural::conversion::primitive_int_from_natural::{
    SignedFromNaturalError, UnsignedFromNaturalError,
};
use malachite_nz::natural::Natural;

assert_eq!(u32::try_from(&Natural::from(123u32)), Ok(123));
assert_eq!(
    u32::try_from(&Natural::from(10u32).pow(12)),
    Err(UnsignedFromNaturalError)
);
assert_eq!(u8::try_from(&Natural::from(123u32)), Ok(123));
assert_eq!(
    u8::try_from(&Natural::from(10u32).pow(12)),
    Err(UnsignedFromNaturalError)
);
assert_eq!(u64::try_from(&Natural::from(123u32)), Ok(123));
assert_eq!(
    u64::try_from(&(Natural::ONE << 100)),
    Err(UnsignedFromNaturalError)
);

assert_eq!(i32::try_from(&Natural::from(123u32)), Ok(123));
assert_eq!(
    i32::try_from(&Natural::from(10u32).pow(12)),
    Err(SignedFromNaturalError)
);
assert_eq!(i8::try_from(&Natural::from(123u32)), Ok(123));
assert_eq!(
    i8::try_from(&Natural::from(10u32).pow(12)),
    Err(SignedFromNaturalError)
);
assert_eq!(i64::try_from(&Natural::from(123u32)), Ok(123));
assert_eq!(
    i64::try_from(&(Natural::ONE << 100)),
    Err(SignedFromNaturalError)
);

§wrapping_from

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_base::num::conversion::traits::WrappingFrom;
use malachite_nz::natural::Natural;

assert_eq!(u32::wrapping_from(&Natural::from(123u32)), 123);
assert_eq!(
    u32::wrapping_from(&Natural::from(10u32).pow(12)),
    3567587328
);
assert_eq!(u8::wrapping_from(&Natural::from(123u32)), 123);
assert_eq!(u8::wrapping_from(&Natural::from(10u32).pow(12)), 0);
assert_eq!(u64::wrapping_from(&Natural::from(123u32)), 123);
assert_eq!(u64::wrapping_from(&(Natural::ONE << 100)), 0);

assert_eq!(i32::wrapping_from(&Natural::from(123u32)), 123);
assert_eq!(
    i32::wrapping_from(&Natural::from(10u32).pow(12)),
    -727379968
);
assert_eq!(i8::wrapping_from(&Natural::from(123u32)), 123);
assert_eq!(i8::wrapping_from(&Natural::from(10u32).pow(12)), 0);
assert_eq!(i64::wrapping_from(&Natural::from(123u32)), 123);
assert_eq!(i64::wrapping_from(&(Natural::ONE << 100)), 0);

§saturating_from

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_base::num::conversion::traits::SaturatingFrom;
use malachite_nz::natural::Natural;

assert_eq!(u32::saturating_from(&Natural::from(123u32)), 123);
assert_eq!(
    u32::saturating_from(&Natural::from(10u32).pow(12)),
    u32::MAX
);
assert_eq!(u8::saturating_from(&Natural::from(123u32)), 123);
assert_eq!(u8::saturating_from(&Natural::from(10u32).pow(12)), 255);
assert_eq!(u64::saturating_from(&Natural::from(123u32)), 123);
assert_eq!(
    u64::saturating_from(&(Natural::ONE << 100)),
    18446744073709551615
);

assert_eq!(i32::saturating_from(&Natural::from(123u32)), 123);
assert_eq!(
    i32::saturating_from(&Natural::from(10u32).pow(12)),
    2147483647
);
assert_eq!(i8::saturating_from(&Natural::from(123u32)), 123);
assert_eq!(i8::saturating_from(&Natural::from(10u32).pow(12)), 127);
assert_eq!(i64::saturating_from(&Natural::from(123u32)), 123);
assert_eq!(
    i64::saturating_from(&(Natural::ONE << 100)),
    9223372036854775807
);

§overflowing_from

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_base::num::conversion::traits::OverflowingFrom;
use malachite_nz::natural::Natural;

assert_eq!(u32::overflowing_from(&Natural::from(123u32)), (123, false));
assert_eq!(
    u32::overflowing_from(&Natural::from(10u32).pow(12)),
    (3567587328, true)
);
assert_eq!(u8::overflowing_from(&Natural::from(123u32)), (123, false));
assert_eq!(
    u8::overflowing_from(&Natural::from(10u32).pow(12)),
    (0, true)
);
assert_eq!(u64::overflowing_from(&Natural::from(123u32)), (123, false));
assert_eq!(u64::overflowing_from(&(Natural::ONE << 100)), (0, true));

assert_eq!(i32::overflowing_from(&Natural::from(123u32)), (123, false));
assert_eq!(
    i32::overflowing_from(&Natural::from(10u32).pow(12)),
    (-727379968, true)
);
assert_eq!(i8::overflowing_from(&Natural::from(123u32)), (123, false));
assert_eq!(
    i8::overflowing_from(&Natural::from(10u32).pow(12)),
    (0, true)
);
assert_eq!(i64::overflowing_from(&Natural::from(123u32)), (123, false));
assert_eq!(i64::overflowing_from(&(Natural::ONE << 100)), (0, true));

§convertible_from

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::natural::Natural;

assert_eq!(u32::convertible_from(&Natural::from(123u32)), true);
assert_eq!(u32::convertible_from(&Natural::from(10u32).pow(12)), false);
assert_eq!(u8::convertible_from(&Natural::from(123u32)), true);
assert_eq!(u8::convertible_from(&Natural::from(10u32).pow(12)), false);
assert_eq!(u64::convertible_from(&Natural::from(123u32)), true);
assert_eq!(u64::convertible_from(&(Natural::ONE << 100)), false);

assert_eq!(i32::convertible_from(&Natural::from(123u32)), true);
assert_eq!(i32::convertible_from(&Natural::from(10u32).pow(12)), false);
assert_eq!(i8::convertible_from(&Natural::from(123u32)), true);
assert_eq!(i8::convertible_from(&Natural::from(10u32).pow(12)), false);
assert_eq!(i64::convertible_from(&Natural::from(123u32)), true);
assert_eq!(i64::convertible_from(&(Natural::ONE << 100)), false);

Structs§