tee*_*ink 77 floating-point objective-c
我有一个变量(float slope),有时在打印时会有一个nan值,因为有时会发生除0.
我试图在发生这种情况时做一个if-else.我怎样才能做到这一点?if (slope == nan)似乎不起作用.
Ste*_*non 203
两种方式,或多或少相当:
if (slope != slope) {
// handle nan here
}
Run Code Online (Sandbox Code Playgroud)
要么
#include <math.h>
...
if (isnan(slope)) {
// handle nan here
}
Run Code Online (Sandbox Code Playgroud)
(man isnan将为您提供更多信息,或者您可以在C标准中阅读所有相关信息)
或者,您可以在进行除法之前检测到分母为零(或者atan2如果您最终将atan在斜率上使用而不是进行其他计算,则使用分母).
if(isnan(slope)) {
yourtextfield.text = @"";
//so textfield value will be empty string if floatvalue is nan
}
else
{
yourtextfield.text = [NSString stringWithFormat:@"%.1f",slope];
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有用.
| 归档时间: |
|
| 查看次数: |
42754 次 |
| 最近记录: |