如何在 c 中产生 NaN 浮点数?

编程入门 行业动态 更新时间:2024-10-18 14:15:07
本文介绍了如何在 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 浮点数?

本文发布于:2023-11-02 17:10:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点数   如何在   NaN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!