malachite_base/num/arithmetic/
square.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::num::arithmetic::traits::{Square, SquareAssign};
10
11macro_rules! impl_square {
12    ($t:ident) => {
13        impl Square for $t {
14            type Output = $t;
15
16            /// Squares a number.
17            ///
18            /// $f(x) = x^2$.
19            ///
20            /// # Worst-case complexity
21            /// Constant time and additional memory.
22            ///
23            /// # Examples
24            /// See [here](super::square#square).
25            #[inline]
26            fn square(self) -> $t {
27                self * self
28            }
29        }
30
31        impl SquareAssign for $t {
32            /// Squares a number in place.
33            ///
34            /// $x \gets x^2$.
35            ///
36            /// # Worst-case complexity
37            /// Constant time and additional memory.
38            ///
39            /// # Examples
40            /// See [here](super::square#square_assign).
41            #[inline]
42            fn square_assign(&mut self) {
43                *self *= *self;
44            }
45        }
46    };
47}
48apply_to_primitive_ints!(impl_square);
49apply_to_primitive_floats!(impl_square);