0

Is this behavior expected?

let k = [0.0, .nan]
let r = k.map {
    [$0.isNaN,
     $0 == .nan, // Why this is ALWAYS false?
     $0 == $0]   // Why this is NOT ALWAYS true?
}

If yes, why?

Hamish
  • 78,605
  • 19
  • 187
  • 280
user3441734
  • 16,722
  • 2
  • 40
  • 59
  • Compare [Which is the Swift equivalent of isnan()?](http://stackoverflow.com/q/24351377/2976878) – a comparison with `.nan` always returns false, including `Double.nan == .nan` (which actually breaks the semantic requirement of `==` being an equivalence relation). – Hamish May 05 '17 at 10:47
  • 3
    This is how the equality with `NaN` is defined in IEEE 754 – Sulthan May 05 '17 at 10:49
  • There actual question here is why is .nan == .nan false. As in the case `$0 == .nan` when `$0` is `0.0` we expect that to not be equivalent to `.nan`. When it is .nan how would you define two `.nan's` being equivalent, just because it is not a number doesn't mean it is the same not a number. In the second case when `$0` is `0.0` it is true, when `$0` is `.nan` it is false for the same reason as previously stated. – Neil Horton May 05 '17 at 10:49

0 Answers0