From e03807f296f23ccf091831797f474fa006d55856 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 15:31:10 +0200 Subject: [PATCH 1/2] impl from_real --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 661b67b..f7e8c08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,6 +134,14 @@ impl Complex { } } +impl Complex { + /// Create a new `Complex` from a real number. + #[inline] + pub fn from_real(re: T) -> Self { + Self::new(re, T::zero()) + } +} + impl Complex { /// Returns the imaginary unit. /// From 648072c4b03c8c1563a6b39ecda7aa2e2040c013 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 15:31:27 +0200 Subject: [PATCH 2/2] tests --- src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f7e8c08..3f31052 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1680,6 +1680,25 @@ pub(crate) mod test { pub const _nan_neg1i: Complex64 = Complex::new(f64::NAN, -1.0); pub const _nan_nani: Complex64 = Complex::new(f64::NAN, f64::NAN); + #[test] + fn test_from_real() { + for re in [ + -3.123, + -1.0, + 0.0, + 1.0, + 5.67, + f64::INFINITY, + f64::NEG_INFINITY, + ] { + assert_eq!(Complex::new(re, 0.0), Complex::from_real(re)); + } + { + let x = Complex::from_real(f64::NAN); + assert!(x.re.is_nan() && x.im.is_zero()); + } + } + #[test] fn test_consts() { // check our constants are what Complex::new creates