It is defined in the protocol FloatingPointNumber , which both Float and Double match. The following is used:
let d = 3.0 let isNan = d.isNaN // False let d = Double.NaN let isNan = d.isNaN // True
If you are looking for a way to do this check yourself, you can. IEEE determines that NaN! = NaN, that is, you cannot directly compare NaN with a number to determine its value-number. However, you can verify that maybeNaN != maybeNaN . If this condition evaluates to true, you are dealing with NaN.
Although you need to use aVariable.isNaN to determine if the value is NaN.
As a quick note, if you are less sure about the classification of the value you are working with, you can switch the value of your FloatingPointNumber corresponding floatingPointClass type.
let noClueWhatThisIs: Double = // ... switch noClueWhatThisIs.floatingPointClass { case .SignalingNaN: print(FloatingPointClassification.SignalingNaN) case .QuietNaN: print(FloatingPointClassification.QuietNaN) case .NegativeInfinity: print(FloatingPointClassification.NegativeInfinity) case .NegativeNormal: print(FloatingPointClassification.NegativeNormal) case .NegativeSubnormal: print(FloatingPointClassification.NegativeSubnormal) case .NegativeZero: print(FloatingPointClassification.NegativeZero) case .PositiveZero: print(FloatingPointClassification.PositiveZero) case .PositiveSubnormal: print(FloatingPointClassification.PositiveSubnormal) case .PositiveNormal: print(FloatingPointClassification.PositiveNormal) case .PositiveInfinity: print(FloatingPointClassification.PositiveInfinity) }
Its values ββare declared in the FloatingPointClassification listing.
Mick MacCallum Jun 22 '14 at 12:51 on 2014-06-22 12:51
source share