malachite_q/arithmetic/neg.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::Rational;
10use core::ops::Neg;
11use malachite_base::num::arithmetic::traits::NegAssign;
12use malachite_base::num::basic::traits::Zero;
13use malachite_base::num::logic::traits::NotAssign;
14
15impl Neg for Rational {
16 type Output = Self;
17
18 /// Negates a [`Rational`], taking it by value.
19 ///
20 /// $$
21 /// f(x) = -x.
22 /// $$
23 ///
24 /// # Worst-case complexity
25 /// Constant time and additional memory.
26 ///
27 /// # Examples
28 /// ```
29 /// use malachite_base::num::basic::traits::Zero;
30 /// use malachite_q::Rational;
31 ///
32 /// assert_eq!(-Rational::ZERO, 0);
33 /// assert_eq!((-Rational::from_signeds(22, 7)).to_string(), "-22/7");
34 /// assert_eq!((-Rational::from_signeds(-22, 7)).to_string(), "22/7");
35 /// ```
36 fn neg(mut self) -> Self {
37 if self.numerator != 0 {
38 self.sign.not_assign();
39 }
40 self
41 }
42}
43
44impl Neg for &Rational {
45 type Output = Rational;
46
47 /// Negates a [`Rational`], taking it by reference.
48 ///
49 /// $$
50 /// f(x) = -x.
51 /// $$
52 ///
53 /// # Worst-case complexity
54 /// $T(n) = O(n)$
55 ///
56 /// $M(n) = O(n)$
57 ///
58 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
59 ///
60 /// # Examples
61 /// ```
62 /// use malachite_base::num::basic::traits::Zero;
63 /// use malachite_q::Rational;
64 ///
65 /// assert_eq!(-&Rational::ZERO, 0);
66 /// assert_eq!((-&Rational::from_signeds(22, 7)).to_string(), "-22/7");
67 /// assert_eq!((-&Rational::from_signeds(-22, 7)).to_string(), "22/7");
68 /// ```
69 fn neg(self) -> Rational {
70 if self.numerator == 0 {
71 Rational::ZERO
72 } else {
73 Rational {
74 sign: !self.sign,
75 numerator: self.numerator.clone(),
76 denominator: self.denominator.clone(),
77 }
78 }
79 }
80}
81
82impl NegAssign for Rational {
83 /// Negates a [`Rational`] in place.
84 ///
85 /// $$
86 /// x \gets -x.
87 /// $$
88 ///
89 /// # Worst-case complexity
90 /// Constant time and additional memory.
91 ///
92 /// # Examples
93 /// ```
94 /// use malachite_base::num::arithmetic::traits::NegAssign;
95 /// use malachite_base::num::basic::traits::Zero;
96 /// use malachite_q::Rational;
97 ///
98 /// let mut x = Rational::ZERO;
99 /// x.neg_assign();
100 /// assert_eq!(x, 0);
101 ///
102 /// let mut x = Rational::from_signeds(22, 7);
103 /// x.neg_assign();
104 /// assert_eq!(x.to_string(), "-22/7");
105 ///
106 /// let mut x = Rational::from_signeds(-22, 7);
107 /// x.neg_assign();
108 /// assert_eq!(x.to_string(), "22/7");
109 /// ```
110 fn neg_assign(&mut self) {
111 if self.numerator != 0 {
112 self.sign.not_assign();
113 }
114 }
115}