From 7198a0971a0d270d0c17def2dffb30af3c826eeb Mon Sep 17 00:00:00 2001 From: "A. R. Shajii" Date: Mon, 31 Jul 2023 13:02:29 -0400 Subject: [PATCH] Fix complex __bool__() --- stdlib/internal/types/complex.codon | 4 ++-- test/stdlib/cmath_test.codon | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/stdlib/internal/types/complex.codon b/stdlib/internal/types/complex.codon index 8e2604a2..9e803190 100644 --- a/stdlib/internal/types/complex.codon +++ b/stdlib/internal/types/complex.codon @@ -23,7 +23,7 @@ class complex: return self def __bool__(self) -> bool: - return self.real != 0.0 and self.imag != 0.0 + return self.real != 0.0 or self.imag != 0.0 def __pos__(self) -> complex: return self @@ -313,7 +313,7 @@ class complex64: return complex(float(self.real), float(self.imag)) def __bool__(self) -> bool: - return self.real != f32(0.0) and self.imag != f32(0.0) + return self.real != f32(0.0) or self.imag != f32(0.0) def __pos__(self) -> complex64: return self diff --git a/test/stdlib/cmath_test.codon b/test/stdlib/cmath_test.codon index 8534f82b..834a6d73 100644 --- a/test/stdlib/cmath_test.codon +++ b/test/stdlib/cmath_test.codon @@ -796,6 +796,21 @@ def test_cmath_testcases(): test_cmath_testcases() +@test +def test_complex_bool(): + z = complex(0, 0) + assert not bool(z) + z = complex(1, 0) + assert bool(z) + z = complex(0, -1) + assert bool(z) + z = complex(1, -1) + assert bool(z) + + +test_complex_bool() + + @test def test_complex64(): c64 = complex64 @@ -839,4 +854,14 @@ def test_complex64(): assert z.real == f32(6.5) assert z.imag == f32(0.0) + z = c64(0, 0) + assert not bool(z) + z = c64(1, 0) + assert bool(z) + z = c64(0, -1) + assert bool(z) + z = c64(1, -1) + assert bool(z) + + test_complex64()