#[repr(C)]pub struct Complex<T> {
pub re: T,
pub im: T,
}🔬This is a nightly-only experimental API. (
complex_numbers #154023)Expand description
A complex number, represented in rectangular form.
§Basic operations
A Complex<T> supports addition, subtraction, and negation when T does. The conjugation
requires that T supports negation.
#![feature(complex_numbers)]
use std::num::Complex;
let x = Complex { re: 5.0, im: 9.0 };
let y = Complex::new(2.0, 3.0);
// Scalar addition and subtraction operate on the real component.
assert_eq!(x + 1.0, Complex::new(6.0, 9.0));
assert_eq!(x - 1.0, Complex::new(4.0, 9.0));
// Complex addition and subtraction operate on each component separately.
assert_eq!(x + y, Complex::new(7.0, 12.0));
assert_eq!(x - y, Complex::new(3.0, 6.0));
// Negation occurs on both components.
// Geometrically this is a rotation by 180 degrees.
assert_eq!(-x, Complex::new(-5.0, -9.0));
// The conjugate negates only the imaginary component.
// Geometrically this is a reflection across the real axis.
assert_eq!(x.conjugate(), Complex::new(5.0, -9.0));§Complex multiplication and division of floating points
Multiplication and division follow the textbook formulas, except that they attempt to correct for overflow of intermediate steps of the calculation.
This behavior mirrors the C standard, although it does not specify exactly when the output should be corrected. Hence, the exact output may vary from platform to platform.
#![feature(complex_numbers)]
use std::num::Complex;
assert_eq!(Complex::new(1.0, 2.0) * Complex::new(3.0, 4.0), Complex::new(-5.0, 10.0));
assert_eq!(Complex::new(2.0, 11.0) / Complex::new(2.0, 1.0), Complex::new(3.0, 4.0));
// The textbook algorithm would return NaN + NaNi for these inputs.
assert_eq!(
Complex::new(1.0, 0.0) * Complex::new(f64::INFINITY, f64::INFINITY),
Complex::new(f64::INFINITY, f64::INFINITY)
);
assert_eq!(
Complex::new(1.0, 1.0) / Complex::new(f64::INFINITY, 0.0),
Complex::new(0.0, 0.0)
);§FFI compatibility
The following types are guaranteed to fully match the platform’s _Complex in terms of layout and ABI:
For any other Complex<T> ABI compatibility is not guaranteed.
Fields§
§re: T🔬This is a nightly-only experimental API. (
complex_numbers #154023)The real component.
im: T🔬This is a nightly-only experimental API. (
complex_numbers #154023)The imaginary component.
Implementations§
Source§impl<T> Complex<T>where
T: Neg<Output = T>,
impl<T> Complex<T>where
T: Neg<Output = T>,
Trait Implementations§
impl<T: Copy> Copy for Complex<T>
impl<T: Eq> Eq for Complex<T>
impl<T: PartialEq> StructuralPartialEq for Complex<T>
Auto Trait Implementations§
impl<T> Freeze for Complex<T>where
T: Freeze,
impl<T> RefUnwindSafe for Complex<T>where
T: RefUnwindSafe,
impl<T> Send for Complex<T>where
T: Send,
impl<T> Sync for Complex<T>where
T: Sync,
impl<T> Unpin for Complex<T>where
T: Unpin,
impl<T> UnsafeUnpin for Complex<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Complex<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more