malachite_nz/integer/conversion/
from_natural.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use crate::natural::Natural;
11
12impl Integer {
13    /// Converts a sign and a [`Natural`] to an [`Integer`], taking the [`Natural`] by value. The
14    /// [`Natural`] becomes the [`Integer`]'s absolute value, and the sign indicates whether the
15    /// [`Integer`] should be non-negative. If the [`Natural`] is zero, then the [`Integer`] will be
16    /// non-negative regardless of the sign.
17    ///
18    /// # Worst-case complexity
19    /// Constant time and additional memory.
20    ///
21    /// # Examples
22    /// ```
23    /// use malachite_nz::integer::Integer;
24    /// use malachite_nz::natural::Natural;
25    ///
26    /// assert_eq!(Integer::from_sign_and_abs(true, Natural::from(123u32)), 123);
27    /// assert_eq!(
28    ///     Integer::from_sign_and_abs(false, Natural::from(123u32)),
29    ///     -123
30    /// );
31    /// ```
32    pub fn from_sign_and_abs(sign: bool, abs: Natural) -> Self {
33        Self {
34            sign: sign || abs == 0,
35            abs,
36        }
37    }
38
39    /// Converts a sign and an [`Natural`] to an [`Integer`], taking the [`Natural`] by reference.
40    /// The [`Natural`] becomes the [`Integer`]'s absolute value, and the sign indicates whether the
41    /// [`Integer`] should be non-negative. If the [`Natural`] is zero, then the [`Integer`] will be
42    /// non-negative regardless of the sign.
43    ///
44    /// # Worst-case complexity
45    /// $T(n) = O(n)$
46    ///
47    /// $M(n) = O(n)$
48    ///
49    /// where $T$ is time, $M$ is additional memory, $n$ is `abs.significant_bits()`.
50    ///
51    /// # Examples
52    /// ```
53    /// use malachite_nz::integer::Integer;
54    /// use malachite_nz::natural::Natural;
55    ///
56    /// assert_eq!(
57    ///     Integer::from_sign_and_abs_ref(true, &Natural::from(123u32)),
58    ///     123
59    /// );
60    /// assert_eq!(
61    ///     Integer::from_sign_and_abs_ref(false, &Natural::from(123u32)),
62    ///     -123
63    /// );
64    /// ```
65    pub fn from_sign_and_abs_ref(sign: bool, abs: &Natural) -> Self {
66        Self {
67            sign: sign || *abs == 0,
68            abs: abs.clone(),
69        }
70    }
71}
72
73impl From<Natural> for Integer {
74    /// Converts a [`Natural`] to an [`Integer`], taking the [`Natural`] by value.
75    ///
76    /// # Worst-case complexity
77    /// Constant time and additional memory.
78    ///
79    /// # Examples
80    /// ```
81    /// use malachite_base::num::arithmetic::traits::Pow;
82    /// use malachite_nz::integer::Integer;
83    /// use malachite_nz::natural::Natural;
84    ///
85    /// assert_eq!(Integer::from(Natural::from(123u32)), 123);
86    /// assert_eq!(
87    ///     Integer::from(Natural::from(10u32).pow(12)),
88    ///     1000000000000u64
89    /// );
90    /// ```
91    fn from(value: Natural) -> Self {
92        Self {
93            sign: true,
94            abs: value,
95        }
96    }
97}
98
99impl<'a> From<&'a Natural> for Integer {
100    /// Converts a [`Natural`] to an [`Integer`], taking the [`Natural`] by reference.
101    ///
102    /// # Worst-case complexity
103    /// $T(n) = O(n)$
104    ///
105    /// $M(n) = O(n)$
106    ///
107    /// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
108    ///
109    /// # Examples
110    /// ```
111    /// use malachite_base::num::arithmetic::traits::Pow;
112    /// use malachite_nz::integer::Integer;
113    /// use malachite_nz::natural::Natural;
114    ///
115    /// assert_eq!(Integer::from(&Natural::from(123u32)), 123);
116    /// assert_eq!(
117    ///     Integer::from(&Natural::from(10u32).pow(12)),
118    ///     1000000000000u64
119    /// );
120    /// ```
121    fn from(value: &'a Natural) -> Self {
122        Self {
123            sign: true,
124            abs: value.clone(),
125        }
126    }
127}