diff --git a/README.md b/README.md index fb52a20..3e4b62a 100644 --- a/README.md +++ b/README.md @@ -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") } diff --git a/solution/main.go b/solution/main.go new file mode 100644 index 0000000..2f7c280 --- /dev/null +++ b/solution/main.go @@ -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 +} diff --git a/tests/main_test.go b/tests/main_test.go index 324612c..dd0e26c 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -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 {