Skip to content

Commit

Permalink
Make equality between Complex and other numbers exact (#14309)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Feb 21, 2024
1 parent 35b8878 commit 0a2203b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions spec/std/complex_spec.cr
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/complex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Complex

# :ditto:
def ==(other : Number)
self == other.to_c
@real == other && @imag.zero?
end

# :ditto:
Expand Down Expand Up @@ -307,7 +307,7 @@ struct Number
end

def ==(other : Complex)
to_c == other
other == self
end

def cis : Complex
Expand Down

0 comments on commit 0a2203b

Please sign in to comment.