Skip to content

Commit d872f24

Browse files
committed
Add c32 and c64 helpers
1 parent 14abf27 commit d872f24

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/lib.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,42 @@ pub struct Complex<T> {
9292
pub im: T,
9393
}
9494

95+
/// Alias for a [`Complex<f32>`]
9596
pub type Complex32 = Complex<f32>;
97+
98+
/// Create a new [`Complex<f32>`] with arguments that can convert [`Into<f32>`].
99+
///
100+
/// ```
101+
/// use num_complex::{c32, Complex32};
102+
/// assert_eq!(c32(1u8, 2), Complex32::new(1.0, 2.0));
103+
/// ```
104+
///
105+
/// Note: ambiguous integer literals in Rust will [default] to `i32`, which does **not** implement
106+
/// `Into<f32>`, so a call like `c32(1, 2)` will result in a type error. The example above uses a
107+
/// suffixed `1u8` to set its type, and then the `2` can be inferred as the same type.
108+
///
109+
/// [default]: https://doc.rust-lang.org/reference/expressions/literal-expr.html#integer-literal-expressions
110+
#[inline]
111+
pub fn c32<T: Into<f32>>(re: T, im: T) -> Complex32 {
112+
Complex::new(re.into(), im.into())
113+
}
114+
115+
/// Alias for a [`Complex<f64>`]
96116
pub type Complex64 = Complex<f64>;
97117

118+
/// Create a new [`Complex<f64>`] with arguments that can convert [`Into<f64>`].
119+
///
120+
/// ```
121+
/// use num_complex::{c64, Complex64};
122+
/// assert_eq!(c64(1, 2), Complex64::new(1.0, 2.0));
123+
/// ```
124+
#[inline]
125+
pub fn c64<T: Into<f64>>(re: T, im: T) -> Complex64 {
126+
Complex::new(re.into(), im.into())
127+
}
128+
98129
impl<T> Complex<T> {
99-
/// Create a new Complex
130+
/// Create a new `Complex`
100131
#[inline]
101132
pub const fn new(re: T, im: T) -> Self {
102133
Complex { re, im }

0 commit comments

Comments
 (0)