From 89298918cb5982f897831182b822f7900e91f31d Mon Sep 17 00:00:00 2001 From: "A. R. Shajii" Date: Mon, 24 Jul 2023 11:30:46 -0400 Subject: [PATCH] Add float32 constructor to complex64 --- stdlib/internal/types/complex.codon | 3 +++ test/stdlib/cmath_test.codon | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/stdlib/internal/types/complex.codon b/stdlib/internal/types/complex.codon index ba4bbedd..8e2604a2 100644 --- a/stdlib/internal/types/complex.codon +++ b/stdlib/internal/types/complex.codon @@ -300,6 +300,9 @@ class complex64: def __new__(other): return complex64(other.__complex__()) + def __new__(real: f32): + return complex64(real, f32(0.0)) + def __new__(other: complex) -> complex64: return (f32(other.real), f32(other.imag)) diff --git a/test/stdlib/cmath_test.codon b/test/stdlib/cmath_test.codon index 3a501d88..8534f82b 100644 --- a/test/stdlib/cmath_test.codon +++ b/test/stdlib/cmath_test.codon @@ -826,4 +826,17 @@ def test_complex64(): assert hash(z) assert c64(complex(z)) == z + z = c64(1.5, 2.5) + assert z.real == f32(1.5) + assert z.imag == f32(2.5) + z = c64(3.5) + assert z.real == f32(3.5) + assert z.imag == f32(0.0) + z = c64(f32(4.5), f32(5.5)) + assert z.real == f32(4.5) + assert z.imag == f32(5.5) + z = c64(f32(6.5)) + assert z.real == f32(6.5) + assert z.imag == f32(0.0) + test_complex64()