目标C - 检查nan [英] Objective C - float checking for nan

查看:122
本文介绍了目标C - 检查nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量( float slope ),有时在打印时有一个nan值,因为有时发生0除法。



我正在尝试做一个if-else的时候发生这种情况。我怎样才能做到这一点? if(slope == nan)似乎不起作用。两种方式,或多或少相当于:

pre $如果(斜率!=斜率){
//在这里处理nan
}

或者

  #include< math.h> 
...
if(isnan(slope)){
//在这里处理nan
}

man isnan 会给你更多的信息,或者你可以阅读C标准中的所有内容)



或者,您可以在分割之前检测分母是否为零(或者如果您打算仅使用 atan2 最后在斜坡上使用 atan ,而不是进行其他计算)。

I have a variable ( float slope ) that sometimes will have a value of nan when printed out since a division by 0 sometimes happens.

I am trying to do an if-else for when that happens. How can I do that? if (slope == nan) doesn't seem to work.

解决方案

Two ways, which are more or less equivalent:

if (slope != slope) {
    // handle nan here
}

Or

#include <math.h>
...
if (isnan(slope)) {
    // handle nan here
}

(man isnan will give you more information, or you can read all about it in the C standard)

Alternatively, you could detect that the denominator is zero before you do the divide (or use atan2 if you're just going to end up using atan on the slope instead of doing some other computation).

这篇关于目标C - 检查nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆