#[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>
Source§impl<T: Eq> Eq for Complex<T>
impl<T: Eq> Eq for Complex<T>
Source§#[doc(hidden)]fn assert_fields_are_eq(&self)
#[doc(hidden)]fn assert_fields_are_eq(&self)
🔬This is a nightly-only experimental API. (
derive_eq_internals)1.0.0 (const: unstable) · Source§#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
👎Deprecated since 1.95.0:
implementation detail of #[derive(Eq)]
impl<T: PartialEq> StructuralPartialEq for Complex<T>
Auto Trait Implementations§
impl<T> Forget for Complex<T>
impl<T> Freeze for Complex<T>where
T: Freeze,
impl<T> Move for Complex<T>where
T: Move,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Printable for T
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
🔬This is a nightly-only experimental API. (
core_io_internals)Returns a lower bound on the number of elements this container-like item
contains.
For example, an array
[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize
#[doc(hidden)]const SIZE: usize
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize
#[doc(hidden)]const ALIGN: usize
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment
#[doc(hidden)]const ALIGNMENT: Alignment
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)]const IS_ZST: bool
#[doc(hidden)]const IS_ZST: bool
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout
#[doc(hidden)]const LAYOUT: Layout
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize
#[doc(hidden)]const MAX_SLICE_LEN: usize
🔬This is a nightly-only experimental API. (
sized_type_properties)The largest safe length for a
[Self]. Read more