From 0a2203bf2ee01035d68c460359219e61e412d0ac Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Thu, 22 Feb 2024 02:14:38 +0800 Subject: [PATCH] Make equality between `Complex` and other numbers exact (#14309) --- spec/std/complex_spec.cr | 11 +++++++++++ src/complex.cr | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spec/std/complex_spec.cr b/spec/std/complex_spec.cr index fe0f1ac628ac..65add18f8533 100644 --- a/spec/std/complex_spec.cr +++ b/spec/std/complex_spec.cr @@ -1,6 +1,9 @@ require "spec" require "complex" require "../support/number" +{% unless flag?(:wasm32) %} + require "big" +{% end %} # exact equality, including component signs private def assert_complex_eq(z1 : Complex, z2 : Complex, *, file = __FILE__, line = __LINE__) @@ -49,6 +52,10 @@ describe "Complex" do c = 4.2 (a == b).should be_true (a == c).should be_false + + {% unless flag?(:wasm32) %} + (a == BigDecimal.new(53, 1)).should be_false + {% end %} end it "number == complex" do @@ -57,6 +64,10 @@ describe "Complex" do c = Complex.new(7.2, 0) (a == b).should be_true (a == c).should be_false + + {% unless flag?(:wasm32) %} + (BigDecimal.new(72, 1) == c).should be_false + {% end %} end end diff --git a/src/complex.cr b/src/complex.cr index bbe7eb54e921..90c3397e06ce 100644 --- a/src/complex.cr +++ b/src/complex.cr @@ -37,7 +37,7 @@ struct Complex # :ditto: def ==(other : Number) - self == other.to_c + @real == other && @imag.zero? end # :ditto: @@ -307,7 +307,7 @@ struct Number end def ==(other : Complex) - to_c == other + other == self end def cis : Complex