Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ShiftPointer(pointer **int, shift int) {
panic("not implemented")
}

// IsComplexEqual compares two complex numbers and determines if they are equal.
// IsComplexEqual compares two complex numbers and determines if they belong to the same eps-neighborhood.
func IsComplexEqual(a, b complex128) bool {
panic("not implemented")
}
Expand Down
11 changes: 11 additions & 0 deletions solution/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package digest

import "math"

func IsComplexEqual(a, b complex128) bool {
return isFloatEqual(real(a), real(b)) && isFloatEqual(imag(a), imag(b))
}

func isFloatEqual(a, b float64) bool {
return a == b || math.Abs(a-b) < 1e-6
}
12 changes: 12 additions & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ func TestIsComplexEqual(t *testing.T) {
b: complex(-42, math.Inf(1)),
expected: false,
},
{
name: "+inf +inf real equal with precision",
a: complex(math.Inf(1), 1),
b: complex(math.Inf(1), 1.000000001),
expected: true,
},
{
name: "+inf +inf imag equal with precision",
a: complex(1, math.Inf(1)),
b: complex(1.000000001, math.Inf(1)),
expected: true,
},
}

for _, tc := range testCases {
Expand Down