your programing

isnan ()에 해당하는 Swift는 무엇입니까?

lovepro 2020. 12. 29. 08:07
반응형

isnan ()에 해당하는 Swift는 무엇입니까?


isnan()Swift 와 동등한 것은 무엇입니까? 일부 작업 결과가 유효한지 확인하고 x / 0과 같은 잘못된 항목을 삭제해야합니다. 감사합니다.


이는 FloatDouble 유형 이 모두 준수 하는 FloatingPointNumber 프로토콜에 정의되어 있습니다. 사용법은 다음과 같습니다.

let d = 3.0
let isNan = d.isNaN // False

let d = Double.NaN
let isNan = d.isNaN // True

직접 확인하는 방법을 찾고 있다면 할 수 있습니다. IEEE는 NaN! = NaN을 정의합니다. 즉, NaN을 숫자와 직접 비교하여 숫자 여부를 결정할 수 없습니다. 그러나 maybeNaN != maybeNaN. 이 조건이 참으로 평가되면 NaN을 처리하는 것입니다.

당신은해야하지만 사용하여 선호aVariable.isNaN 값이 NaN인지 확인.


참고로, 작업중인 값의 분류에 대해 확신이없는 경우 해당 FloatingPointNumber유형의 floatingPointClass속성 값을 전환 할 수 있습니다 .

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)
}

해당 값은 FloatingPointClassification 열거 형에 선언됩니다 .


받아 들여지는 대답은 작동하지만 처음 보았을 때 예제 때문에 명확하지 않았고 NaN이 "not a number".

다음은 명확하지 않은 사용자를위한 Apple의 예입니다.

여기에 이미지 설명 입력

let x = 0.0
let y = x * .infinity // y is a NaN

if y.isNan {

    print("this is NaN") // this will print
} else {

    print("this isn't Nan")
}

참조 URL : https://stackoverflow.com/questions/24351377/which-is-the-swift-equivalent-of-isnan

반응형