如何在 c 中产生 NaN 浮点数? [英] How to produce a NaN float in c?

查看:24
本文介绍了如何在 c 中产生 NaN 浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

float f = (float)'a';
if(f < 0){ 
}   
else if(f == 0){ 
}   
else if(f > 0){ 
}   
else{
    printf("NaN
");                                                          
}   

f 不会大于/等于/小于 0 如果它是 NaN.

f won't be greater/equal/less than 0 if it's a NaN.

但是首先如何产生这样的f呢?

But how to produce such a f in the first place?

我尝试了各种方法来生成 NaN,但都没有奏效..

I tried various ways to produce a NaN,but none work..

推荐答案

使用浮点数,0.0/0.0 不是除以零"错误;结果是 NaN.

Using floating point numbers, 0.0 / 0.0 isn't a "divide by zero" error; it results in NaN.

这个 C 程序打印 -nan:

This C program prints -nan:

#include <stdio.h>

int main()
{
    float x = 0.0 / 0.0;
    printf("%f
", x);
    return 0;
}

NaN 对计算机的外观而言,两个无效"数字为信号"和安静"NaN 保留(类似于为正无穷和负无穷保留的两个无效数字).维基百科条目 有更多关于 NaN 如何表示为 IEE 浮点数的详细信息.

In terms what NaN looks like to the computer, two "invalid" numbers are reserved for "signaling" and "quiet" NaN (similar to the two invalid numbers reserved for positive and negative infinity). The Wikipedia entry has more details about how NaN is represented as an IEE floating point number.

这篇关于如何在 c 中产生 NaN 浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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