Expand description

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

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

checked_from

extern crate malachite_base;

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

assert_eq!(u32::checked_from(&Natural::from(123u32)), Some(123));
assert_eq!(u32::checked_from(&Natural::from(10u32).pow(12)), None);
assert_eq!(u8::checked_from(&Natural::from(123u32)), Some(123));
assert_eq!(u8::checked_from(&Natural::from(10u32).pow(12)), None);
assert_eq!(u64::checked_from(&Natural::from(123u32)), Some(123));
assert_eq!(u64::checked_from(&(Natural::ONE << 100)), None);

assert_eq!(i32::checked_from(&Natural::from(123u32)), Some(123));
assert_eq!(i32::checked_from(&Natural::from(10u32).pow(12)), None);
assert_eq!(i8::checked_from(&Natural::from(123u32)), Some(123));
assert_eq!(i8::checked_from(&Natural::from(10u32).pow(12)), None);
assert_eq!(i64::checked_from(&Natural::from(123u32)), Some(123));
assert_eq!(i64::checked_from(&(Natural::ONE << 100)), None);

wrapping_from

extern crate malachite_base;

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

extern crate malachite_base;

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

extern crate malachite_base;

extern crate malachite_base;

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

extern crate malachite_base;

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);