53

Possible Duplicates:
Objective-C - float checking for nan
Determine if NSNumber is NaN

I have a problem with NaN values in CGFloat, how can I check if the number is valid?

the only way so far that works is:

if ([[NSString stringWithFormat:@"%f", output] isEqualToString:@"nan"]) {
    output = 0;
}

which is not a nice solution at all! :) ... and I am pretty sure that there is something else I should do instead.

4

1 回答 1

152

在 math.h 中有一个用于检查数字是否为 nan inf 等的定义(我认为您可以在不导入的情况下使用它)。

isnan(myValue)

如果你遵循定义,你最终会得到

(x!=x)

还有一些其他有用的定义,例如 isinf, isnormal , isfinite , ...

于 2011-06-02T21:58:14.373 回答