如何在 Objective-C (iOS) 中检查 NaN 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6220798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:12:14  来源:igfitidea点击:

How to check for NaN value in Objective-C (iOS)

iosobjective-cxcodecgfloat

提问by Ondrej Rafaj

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

可能的重复项:
Objective-C - 浮点检查 nan
确定 NSNumber 是否为 NaN

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

我对 CGFloat 中的 NaN 值有问题,如何检查数字是否有效?

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.

这根本不是一个好的解决方案!:) ...而且我很确定我应该做其他事情。

回答by thomas

There is a define for checking if a number is nan inf etc in math.h (you can use it without import I think).

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

isnan(myValue)

if you follow the define you will end up with

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

(x!=x)

there are also some other useful defines like isinf, isnormal, isfinite, ...

也有像isinf,其他一些有用的定义isnormalISFINITE,...