XCTAssertEqual failed: ("nan") is not equal to ("nan") in Swift

When I run the following test case, I get XCTAssertEqual failed: ("nan") is not equal to ("nan") error.

 XCTAssertEqual(0.0 / 0.0, Double.nan)

How can I fix this error?

1 answer

  • answered 2022-05-01 12:27 Duncan C

    As Joakim said, NaN is never equal to any value, including itself.

    You need to rewrite your code using isNaN:

    let someValue = 0.0 / 0.0
    XCTAssertFalse(someValue.isNaN) // Will detect that someValue is a NaN

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum