Skip to main content

malachite_float/float/arithmetic/
abs_squared.rs

1// Copyright © 2026 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::Float;
10use malachite_base::num::arithmetic::traits::{AbsSquared, AbsSquaredAssign, Square, SquareAssign};
11
12impl AbsSquared for Float {
13    type Output = Self;
14
15    /// Computes the squared absolute value of a [`Float`], taking it by value. For real types this
16    /// is the same as squaring: the output has the precision of the input and is rounded to
17    /// nearest. See [`Float::square`] for more details, and the `square_prec` and `square_round`
18    /// families for more control over the result.
19    ///
20    /// $$
21    /// f(x) = |x|^2 = x^2.
22    /// $$
23    ///
24    /// # Worst-case complexity
25    /// $T(n) = O(n \log n \log\log n)$
26    ///
27    /// $M(n) = O(n)$
28    ///
29    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
30    ///
31    /// # Examples
32    /// ```
33    /// use malachite_base::num::arithmetic::traits::AbsSquared;
34    /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
35    /// use malachite_float::Float;
36    ///
37    /// assert_eq!(Float::NAN.abs_squared().to_string(), "NaN");
38    /// assert_eq!(Float::INFINITY.abs_squared().to_string(), "Infinity");
39    /// assert_eq!(
40    ///     Float::NEGATIVE_INFINITY.abs_squared().to_string(),
41    ///     "Infinity"
42    /// );
43    /// assert_eq!(Float::from(4.0).abs_squared().to_string(), "16.0");
44    /// assert_eq!(Float::from(-1.5).abs_squared().to_string(), "2.0");
45    /// ```
46    #[inline]
47    fn abs_squared(self) -> Self {
48        self.square()
49    }
50}
51
52impl AbsSquared for &Float {
53    type Output = Float;
54
55    /// Computes the squared absolute value of a [`Float`], taking it by reference. For real types
56    /// this is the same as squaring: the output has the precision of the input and is rounded to
57    /// nearest. See [`Float::square`] for more details, and the `square_prec` and `square_round`
58    /// families for more control over the result.
59    ///
60    /// $$
61    /// f(x) = |x|^2 = x^2.
62    /// $$
63    ///
64    /// # Worst-case complexity
65    /// $T(n) = O(n \log n \log\log n)$
66    ///
67    /// $M(n) = O(n)$
68    ///
69    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
70    ///
71    /// # Examples
72    /// ```
73    /// use malachite_base::num::arithmetic::traits::AbsSquared;
74    /// use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity};
75    /// use malachite_float::Float;
76    ///
77    /// assert_eq!((&Float::NAN).abs_squared().to_string(), "NaN");
78    /// assert_eq!((&Float::INFINITY).abs_squared().to_string(), "Infinity");
79    /// assert_eq!(
80    ///     (&Float::NEGATIVE_INFINITY).abs_squared().to_string(),
81    ///     "Infinity"
82    /// );
83    /// assert_eq!((&Float::from(4.0)).abs_squared().to_string(), "16.0");
84    /// assert_eq!((&Float::from(-1.5)).abs_squared().to_string(), "2.0");
85    /// ```
86    #[inline]
87    fn abs_squared(self) -> Float {
88        self.square()
89    }
90}
91
92impl AbsSquaredAssign for Float {
93    /// Replaces a [`Float`] with its squared absolute value. For real types this is the same as
94    /// squaring in place.
95    ///
96    /// $$
97    /// x \gets |x|^2 = x^2.
98    /// $$
99    ///
100    /// # Worst-case complexity
101    /// $T(n) = O(n \log n \log\log n)$
102    ///
103    /// $M(n) = O(n \log n)$
104    ///
105    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
106    ///
107    /// # Examples
108    /// ```
109    /// use malachite_base::num::arithmetic::traits::AbsSquaredAssign;
110    /// use malachite_float::Float;
111    ///
112    /// let mut x = Float::from(-1.5);
113    /// x.abs_squared_assign();
114    /// assert_eq!(x.to_string(), "2.0");
115    /// ```
116    #[inline]
117    fn abs_squared_assign(&mut self) {
118        self.square_assign();
119    }
120}