Skip to main content

rug/complex/
mod.rs

1// Copyright © 2016–2026 Trevor Spiteri
2
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License and
14// a copy of the GNU General Public License along with this program. If not, see
15// <https://www.gnu.org/licenses/>.
16
17/*!
18Multi-precision complex numbers with correct rounding.
19
20This module provides support for complex numbers of type
21[`Complex`][crate::Complex].
22*/
23
24pub(crate) mod arith;
25pub(crate) mod big;
26mod borrow;
27#[cfg(feature = "borsh")]
28mod borsh;
29#[cfg(feature = "num-complex")]
30mod casts;
31mod cmp;
32#[cfg(feature = "num-traits")]
33mod impl_num_traits;
34mod mini;
35mod ord;
36#[cfg(feature = "serde")]
37mod serde;
38mod small;
39#[cfg(test)]
40mod tests;
41mod traits;
42
43pub use crate::complex::big::ParseComplexError;
44pub use crate::complex::borrow::BorrowComplex;
45pub use crate::complex::mini::MiniComplex;
46pub use crate::complex::ord::OrdComplex;
47#[allow(deprecated)]
48pub use crate::complex::small::SmallComplex;
49
50/**
51The `Prec` trait is used to specify the precision of the real and imaginary
52parts of a [`Complex`][crate::Complex] number.
53
54This trait is implememented for [`u32`] and for <code>[(][tuple][u32][], [u32][][)][tuple]</code>.
55
56# Examples
57
58```rust
59use rug::Complex;
60let c1 = Complex::new(32);
61assert_eq!(c1.prec(), (32, 32));
62let c2 = Complex::new((32, 64));
63assert_eq!(c2.prec(), (32, 64));
64```
65
66[`Complex`]: `crate::Complex`
67*/
68pub trait Prec {
69    /// Returns the precision for the real and imaginary parts.
70    ///
71    /// # Examples
72    ///
73    /// ```rust
74    /// use rug::complex::Prec;
75    /// assert_eq!(Prec::prec(24), (24, 24));
76    /// assert_eq!(Prec::prec((24, 53)), (24, 53));
77    /// ```
78    fn prec(self) -> (u32, u32);
79}
80
81impl Prec for u32 {
82    #[inline]
83    fn prec(self) -> (u32, u32) {
84        (self, self)
85    }
86}
87
88impl Prec for (u32, u32) {
89    #[inline]
90    fn prec(self) -> (u32, u32) {
91        self
92    }
93}
94
95/**
96The `Prec64` trait is used to specify the precision of the real and imaginary
97parts of a [`Complex`][crate::Complex] number.
98
99This trait is implememented for [`u64`] and for <code>[(][tuple][u64][], [u64][][)][tuple]</code>.
100
101# Examples
102
103```rust
104use rug::Complex;
105let c1 = Complex::new_64(32);
106assert_eq!(c1.prec_64(), (32, 32));
107let c2 = Complex::new_64((32, 64));
108assert_eq!(c2.prec_64(), (32, 64));
109```
110
111[`Complex`]: `crate::Complex`
112*/
113pub trait Prec64 {
114    /// Returns the precision for the real and imaginary parts.
115    ///
116    /// # Examples
117    ///
118    /// ```rust
119    /// use rug::complex::Prec64;
120    /// assert_eq!(Prec64::prec(24), (24, 24));
121    /// assert_eq!(Prec64::prec((24, 53)), (24, 53));
122    /// ```
123    fn prec(self) -> (u64, u64);
124}
125
126impl Prec64 for u64 {
127    #[inline]
128    fn prec(self) -> (u64, u64) {
129        (self, self)
130    }
131}
132
133impl Prec64 for (u64, u64) {
134    #[inline]
135    fn prec(self) -> (u64, u64) {
136        self
137    }
138}