1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
use integer::Integer;
use natural::Natural;
impl Integer {
/// Converts a sign and a [`Natural`] to an [`Integer`], taking the [`Natural`] by value. The
/// [`Natural`] becomes the [`Integer`]'s absolute value, and the sign indicates whether the
/// [`Integer`] should be non-negative. If the [`Natural`] is zero, then the [`Integer`] will
/// be non-negative regardless of the sign.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(Integer::from_sign_and_abs(true, Natural::from(123u32)), 123);
/// assert_eq!(Integer::from_sign_and_abs(false, Natural::from(123u32)), -123);
/// ```
pub fn from_sign_and_abs(sign: bool, abs: Natural) -> Integer {
Integer {
sign: sign || abs == 0,
abs,
}
}
/// Converts a sign and an [`Natural`] to an [`Integer`], taking the [`Natural`] by reference.
/// The [`Natural`] becomes the [`Integer`]'s absolute value, and the sign indicates whether
/// the [`Integer`] should be non-negative. If the [`Natural`] is zero, then the [`Integer`]
/// will be non-negative regardless of the sign.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `abs.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(Integer::from_sign_and_abs_ref(true, &Natural::from(123u32)), 123);
/// assert_eq!(Integer::from_sign_and_abs_ref(false, &Natural::from(123u32)), -123);
/// ```
pub fn from_sign_and_abs_ref(sign: bool, abs: &Natural) -> Integer {
Integer {
sign: sign || *abs == 0,
abs: abs.clone(),
}
}
}
impl From<Natural> for Integer {
/// Converts a [`Natural`] to an [`Integer`], taking the [`Natural`] by value.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::Pow;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(Integer::from(Natural::from(123u32)), 123);
/// assert_eq!(Integer::from(Natural::from(10u32).pow(12)), 1000000000000u64);
/// ```
fn from(value: Natural) -> Integer {
Integer {
sign: true,
abs: value,
}
}
}
impl<'a> From<&'a Natural> for Integer {
/// Converts a [`Natural`] to an [`Integer`], taking the [`Natural`] by reference.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::Pow;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(Integer::from(&Natural::from(123u32)), 123);
/// assert_eq!(Integer::from(&Natural::from(10u32).pow(12)), 1000000000000u64);
/// ```
fn from(value: &'a Natural) -> Integer {
Integer {
sign: true,
abs: value.clone(),
}
}
}