`Double(exactly: Float.nan)` returns `nil`

(Don’t ask me why I’m looking into this. :smile:)

I am running into some behavior that seems unintuitive:

Double(Float.nan)  // returns `Double.nan`
Float(Double.nan)  // returns `Float.nan`

Double(exactly: Float.nan)  // returns `nil`
Float(exactly: Double.nan)  // returns `nil`

Is this a bug, or something I’m misunderstanding about floating point?

From the init(exactly:) documentation:

In particular, passing NaN as other always results in nil .

So it's definitely not a bug. My best guess for why this is the case is that Double.nan doesn't equal Double.nan (nan isn't equal to anything), so Double.nan shouldn't be considered to be 'exactly' Float.nan.

5 Likes