Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Float.Extra.aboutEqual behaviour with infinities #51

Merged
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
7 changes: 2 additions & 5 deletions src/Float/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,8 @@ This value handles Infinity and NaN like so:
-}
aboutEqual : Float -> Float -> Bool
aboutEqual a b =
if isInfinite a then
isInfinite b

else if isInfinite b then
False
if isInfinite a || isInfinite b then
a == b

else
abs (a - b) <= 1.0e-5 + 1.0e-8 * abs a
Expand Down
64 changes: 58 additions & 6 deletions tests/FloatTests.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module FloatTests exposing (modByTests, testAboutEqual, testBoundaryValuesAsUnicode, testRange, testToFixedDecimalPlaces, testToFixedSignificantDigits)

import Expect exposing (FloatingPointTolerance(..))
import Float.Extra exposing (aboutEqual)
import Float.Extra
import Fuzz exposing (Fuzzer)
import List.Extra exposing (Step(..))
import Test exposing (Test, describe, fuzz, fuzz2, test)
Expand Down Expand Up @@ -155,11 +155,63 @@ testBoundaryValuesAsUnicode =

testAboutEqual : Test
testAboutEqual =
fuzz Fuzz.niceFloat "makes numbers about equal even after some operations" <|
\a ->
((a + 10 + a - 10 - a) * 2 / 2)
|> aboutEqual a
|> Expect.equal True
describe "aboutEqual should compare numbers as equal within a reasonable tolerance"
[ fuzz Fuzz.niceFloat "makes numbers about equal even after some operations" <|
\a ->
((a + 10 + a - 10 - a) * 2 / 2)
|> Float.Extra.aboutEqual a
|> Expect.equal True
, fuzz Fuzz.niceFloat "NaN is not equal to any 'nice' float" <|
\a ->
(0 / 0)
|> Float.Extra.aboutEqual a
|> Expect.equal False
, fuzz Fuzz.niceFloat "no 'nice' float is equal to NaN" <|
\a ->
a
|> Float.Extra.aboutEqual (0 / 0)
|> Expect.equal False
, test "positive infinity equals positive infinity" <|
\() ->
(1 / 0)
|> Float.Extra.aboutEqual (2 / 0)
|> Expect.equal True
, test "negative infinity equals negative infinity" <|
\() ->
(-1 / 0)
|> Float.Extra.aboutEqual (-2 / 0)
|> Expect.equal True
, test "positive infinity does not equal negative infinity" <|
\() ->
(1 / 0)
|> Float.Extra.aboutEqual (-1 / 0)
|> Expect.equal False
, test "positive infinity does not equal NaN" <|
\() ->
(1 / 0)
|> Float.Extra.aboutEqual (0 / 0)
|> Expect.equal False
, test "NaN does not equal positive infinity" <|
\() ->
(0 / 0)
|> Float.Extra.aboutEqual (1 / 0)
|> Expect.equal False
, test "negative infinity does not equal NaN" <|
\() ->
(-1 / 0)
|> Float.Extra.aboutEqual (0 / 0)
|> Expect.equal False
, test "NaN does not equal negative infinity" <|
\() ->
(0 / 0)
|> Float.Extra.aboutEqual (-1 / 0)
|> Expect.equal False
, test "NaN does not equal NaN" <|
\() ->
(0 / 0)
|> Float.Extra.aboutEqual (0 / 0)
|> Expect.equal False
]


testRange : Test
Expand Down
Loading