num_valid/functions/
neg_assign.rs

1#![deny(rustdoc::broken_intra_doc_links)]
2
3//! In-place negation operations.
4//!
5//! This module provides the [`NegAssign`] trait for negating a value in place,
6//! which is more efficient than creating a new negated value for heap-allocated types.
7
8use duplicate::duplicate_item;
9use num::Complex;
10
11//-------------------------------------------------------------
12/// Compound negation and assignment.
13pub trait NegAssign {
14    /// Performs the negation.
15    fn neg_assign(&mut self);
16}
17
18#[duplicate_item(
19    T;
20    [f64];
21    [Complex<f64>];
22)]
23/// Compound negation and assignment.
24impl NegAssign for T {
25    /// Performs the negation of `self`.
26    fn neg_assign(&mut self) {
27        *self = -*self;
28    }
29}
30
31//-------------------------------------------------------------
32
33//-------------------------------------------------------------
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use num::Complex;
38
39    mod native64 {
40        use super::*;
41
42        #[test]
43        fn test_neg_assign_f64() {
44            let mut value = 1.0_f64;
45            value.neg_assign();
46            let expected_value = -1.0_f64;
47            assert_eq!(value, expected_value);
48
49            let mut value = -2.0_f64;
50            value.neg_assign();
51            let expected_value = 2.0_f64;
52            assert_eq!(value, expected_value);
53        }
54
55        #[test]
56        fn test_neg_assign_complex_f64() {
57            let mut value = Complex::new(1.0, -2.0);
58            value.neg_assign();
59            let expected_value = Complex::new(-1.0, 2.0);
60            assert_eq!(value, expected_value);
61
62            let mut value = Complex::new(-3.0, 4.0);
63            value.neg_assign();
64            let expected_value = Complex::new(3.0, -4.0);
65            assert_eq!(value, expected_value);
66        }
67    }
68
69    #[cfg(feature = "rug")]
70    mod rug53 {
71        use super::*;
72        use crate::backends::rug::validated::{ComplexRugStrictFinite, RealRugStrictFinite};
73        use rug::Float;
74        use try_create::TryNew;
75
76        const PRECISION: u32 = 53;
77
78        #[test]
79        fn test_neg_assign_real_rug() {
80            let mut value =
81                RealRugStrictFinite::<PRECISION>::try_new(Float::with_val(PRECISION, 1.0)).unwrap();
82            value.neg_assign();
83            let expected_value =
84                RealRugStrictFinite::<PRECISION>::try_new(Float::with_val(PRECISION, -1.0))
85                    .unwrap();
86            assert_eq!(value, expected_value);
87
88            let mut value =
89                RealRugStrictFinite::<PRECISION>::try_new(Float::with_val(PRECISION, -2.0))
90                    .unwrap();
91            value.neg_assign();
92            let expected_value =
93                RealRugStrictFinite::<PRECISION>::try_new(Float::with_val(PRECISION, 2.0)).unwrap();
94            assert_eq!(value, expected_value);
95        }
96
97        #[test]
98        fn test_neg_assign_complex_rug() {
99            let mut value = ComplexRugStrictFinite::<PRECISION>::try_new(rug::Complex::with_val(
100                PRECISION,
101                (
102                    Float::with_val(PRECISION, 1.0),
103                    Float::with_val(PRECISION, -2.0),
104                ),
105            ))
106            .unwrap();
107            value.neg_assign();
108            let expected_value =
109                ComplexRugStrictFinite::<PRECISION>::try_new(rug::Complex::with_val(
110                    PRECISION,
111                    (
112                        Float::with_val(PRECISION, -1.0),
113                        Float::with_val(PRECISION, 2.0),
114                    ),
115                ))
116                .unwrap();
117            assert_eq!(value, expected_value);
118
119            let mut value = ComplexRugStrictFinite::<PRECISION>::try_new(rug::Complex::with_val(
120                PRECISION,
121                (
122                    Float::with_val(PRECISION, -3.0),
123                    Float::with_val(PRECISION, 4.0),
124                ),
125            ))
126            .unwrap();
127            value.neg_assign();
128            let expected_value =
129                ComplexRugStrictFinite::<PRECISION>::try_new(rug::Complex::with_val(
130                    PRECISION,
131                    (
132                        Float::with_val(PRECISION, 3.0),
133                        Float::with_val(PRECISION, -4.0),
134                    ),
135                ))
136                .unwrap();
137            assert_eq!(value, expected_value);
138        }
139    }
140}
141//-------------------------------------------------------------