哪个是isnan()的快速对等物?

时间:2022-04-27 21:23:49

Which is the equivalent of isnan() in Swift ? I need to check if some operation results are valid and delete those invalid like x/0 Thanks

这相当于Swift的isnan() ?我需要检查一些操作结果是否有效,删除x/0等无效的,谢谢

1 个解决方案

#1


102  

It's defined in the FloatingPointNumber protocol, which both the Float and Double types conform to. Usage is as follows:

它是在FloatingPointNumber协议中定义的,浮动类型和双类型都遵循该协议。用法如下:

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

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

If you're looking for a way to make this check yourself, you can. IEEE defines that NaN != NaN, meaning you can't directly compare NaN to a number to determine its is-a-number-ness. However, you can check that maybeNaN != maybeNaN. If this condition evaluates as true, you're dealing with NaN.

如果你正在寻找一种方法来做这个检查,你可以。IEEE定义了NaN != NaN,这意味着不能直接将NaN与一个数字进行比较来确定它的is-a-number性。但是,您可以检查一下maybeNaN != maybeNaN。如果这个条件的值为true,那么你就是在处理NaN。

Although you should prefer using aVariable.isNaN to determine if a value is NaN.

虽然您应该更喜欢使用可更改的。确定一个值是否为NaN。


As a bit of a side note, if you're less sure about the classification of the value you're working with, you can switch over value of your FloatingPointNumber conforming type's floatingPointClass property.

顺便说一句,如果您不太确定要处理的值的分类,那么可以切换到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)
}

Its values are declared in the FloatingPointClassification enum.

它的值在FloatingPointClassification enum中声明。

#1


102  

It's defined in the FloatingPointNumber protocol, which both the Float and Double types conform to. Usage is as follows:

它是在FloatingPointNumber协议中定义的,浮动类型和双类型都遵循该协议。用法如下:

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

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

If you're looking for a way to make this check yourself, you can. IEEE defines that NaN != NaN, meaning you can't directly compare NaN to a number to determine its is-a-number-ness. However, you can check that maybeNaN != maybeNaN. If this condition evaluates as true, you're dealing with NaN.

如果你正在寻找一种方法来做这个检查,你可以。IEEE定义了NaN != NaN,这意味着不能直接将NaN与一个数字进行比较来确定它的is-a-number性。但是,您可以检查一下maybeNaN != maybeNaN。如果这个条件的值为true,那么你就是在处理NaN。

Although you should prefer using aVariable.isNaN to determine if a value is NaN.

虽然您应该更喜欢使用可更改的。确定一个值是否为NaN。


As a bit of a side note, if you're less sure about the classification of the value you're working with, you can switch over value of your FloatingPointNumber conforming type's floatingPointClass property.

顺便说一句,如果您不太确定要处理的值的分类,那么可以切换到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)
}

Its values are declared in the FloatingPointClassification enum.

它的值在FloatingPointClassification enum中声明。