C++ 1.#INF00、-1.#IND00 和 -1.#IND 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/347920/
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
What do 1.#INF00, -1.#IND00 and -1.#IND mean?
提问by Hoffmann
I'm messing around with some C code using floats, and I'm getting 1.#INF00, -1.#IND00 and -1.#IND when I try to print floats in the screen. What does those values mean?
我正在使用浮点数处理一些 C 代码,当我尝试在屏幕上打印浮点数时,我得到了 1.#INF00、-1.#IND00 和 -1.#IND。这些值是什么意思?
I believe that 1.#INF00 means positive infinity, but what about -1.#IND00 and -1.#IND? I also saw sometimes this value: 1.$NaN which is Not a Number, but what causes those strange values and how can those help me with debugging?
我相信 1.#INF00 意味着正无穷大,但是 -1.#IND00 和 -1.#IND 呢?我有时也看到这个值:1.$NaN 这不是一个数字,但是是什么导致了这些奇怪的值,这些值如何帮助我进行调试?
I'm using MinGWwhich I believe uses IEEE 754representation for float point numbers.
我正在使用MinGW,我相信它使用浮点数的IEEE 754表示。
Can someone list all those invalid values and what they mean?
有人可以列出所有这些无效值及其含义吗?
回答by mat
From IEEE floating-point exceptions in C++:
This page will answer the following questions.
- My program just printed out 1.#IND or 1.#INF (on Windows) or nan or inf (on Linux). What happened?
- How can I tell if a number is really a number and not a NaN or an infinity?
- How can I find out more details at runtime about kinds of NaNs and infinities?
- Do you have any sample code to show how this works?
- Where can I learn more?
These questions have to do with floating point exceptions. If you get some strange non-numeric output where you're expecting a number, you've either exceeded the finite limits of floating point arithmetic or you've asked for some result that is undefined. To keep things simple, I'll stick to working with the double floating point type. Similar remarks hold for float types.
Debugging 1.#IND, 1.#INF, nan, and inf
If your operation would generate a larger positive number than could be stored in a double, the operation will return 1.#INF on Windows or inf on Linux. Similarly your code will return -1.#INF or -inf if the result would be a negative number too large to store in a double. Dividing a positive number by zero produces a positive infinity and dividing a negative number by zero produces a negative infinity. Example code at the end of this page will demonstrate some operations that produce infinities.
Some operations don't make mathematical sense, such as taking the square root of a negative number. (Yes, this operation makes sense in the context of complex numbers, but a double represents a real number and so there is no double to represent the result.) The same is true for logarithms of negative numbers. Both sqrt(-1.0) and log(-1.0) would return a NaN, the generic term for a "number" that is "not a number". Windows displays a NaN as -1.#IND ("IND" for "indeterminate") while Linux displays nan. Other operations that would return a NaN include 0/0, 0*∞, and ∞/∞. See the sample code below for examples.
In short, if you get 1.#INF or inf, look for overflow or division by zero. If you get 1.#IND or nan, look for illegal operations. Maybe you simply have a bug. If it's more subtle and you have something that is difficult to compute, see Avoiding Overflow, Underflow, and Loss of Precision. That article gives tricks for computing results that have intermediate steps overflow if computed directly.
本页将回答以下问题。
- 我的程序刚刚打印出 1.#IND 或 1.#INF(在 Windows 上)或 nan 或 inf(在 Linux 上)。发生了什么?
- 如何判断一个数字是否真的是一个数字而不是 NaN 或无穷大?
- 如何在运行时找到有关 NaN 和无穷大种类的更多详细信息?
- 你有任何示例代码来展示它是如何工作的吗?
- 我在哪里可以学到更多?
这些问题与浮点异常有关。如果您在期望数字的地方得到一些奇怪的非数字输出,那么您要么超出了浮点算术的有限限制,要么要求了一些未定义的结果。为简单起见,我将坚持使用双浮点类型。类似的评论适用于浮点类型。
调试 1.#IND、1.#INF、nan 和 inf
如果您的操作生成的正数大于可以存储在双精度数中的正数,则该操作将在 Windows 上返回 1.#INF 或在 Linux 上返回 inf。同样,如果结果是一个太大而无法存储在 double 中的负数,您的代码将返回 -1.#INF 或 -inf。正数除以零产生正无穷大,负数除以零产生负无穷大。本页末尾的示例代码将演示一些产生无穷大的操作。
有些运算没有数学意义,例如取负数的平方根。(是的,这个操作在复数的上下文中是有意义的,但是 double 代表一个实数,所以没有 double 来表示结果。)对于负数的对数也是如此。sqrt(-1.0) 和 log(-1.0) 都将返回 NaN,这是“不是数字”的“数字”的通用术语。Windows 将 NaN 显示为 -1.#IND(“IND”表示“不确定”),而 Linux 显示 nan。其他会返回 NaN 的运算包括 0/0、0*∞ 和 ∞/∞。有关示例,请参阅下面的示例代码。
简而言之,如果您得到 1.#INF 或 inf,请查找溢出或除以零。如果得到 1.#IND 或 nan,请查找非法操作。也许你只是有一个错误。如果它更微妙并且您有一些难以计算的东西,请参阅避免溢出、下溢和精度损失。那篇文章给出了计算结果的技巧,如果直接计算,则中间步骤会溢出。
回答by user2426679
For anyone wondering about the difference between -1.#IND00
and -1.#IND
(which the question specifically asked, and none of the answers address):
对于任何想知道-1.#IND00
and之间的区别的人-1.#IND
(问题特别提出的问题,并且没有任何答案解决):
-1.#IND00
-1.#IND00
This specifically means a non-zero number divided by zero, e.g. 3.14 / 0
(source)
这特别意味着一个非零数除以零,例如3.14 / 0
( source)
-1.#IND
(a synonym for NaN
)
-1.#IND
(的同义词NaN
)
This means one of four things (see wikifrom source):
1) sqrt
or log
of a negative number
1)sqrt
或log
负数
2) operations where both variables are 0 or infinity, e.g. 0 / 0
2) 两个变量都为 0 或无穷大的操作,例如 0 / 0
3) operations where at least one variable is already NaN
, e.g. NaN * 5
3) 至少一个变量已经存在的操作NaN
,例如NaN * 5
4) out of range trig, e.g. arcsin(2)
4) 超出范围触发,例如 arcsin(2)
回答by Jeff
For those of you in a .NET environment the following can be a handy way to filter non-numbers out (this example is in VB.NET, but it's probably similar in C#):
对于那些在 .NET 环境中的人来说,以下可能是一种过滤非数字的简便方法(这个例子在 VB.NET 中,但在 C# 中可能类似):
If Double.IsNaN(MyVariableName) Then
MyVariableName = 0 ' Or whatever you want to do here to "correct" the situation
End If
If you try to use a variable that has a NaN value you will get the following error:
如果您尝试使用具有 NaN 值的变量,您将收到以下错误:
Value was either too large or too small for a Decimal.
十进制的值要么太大要么太小。
回答by Swiss Frank
Your question "what are they" is already answered above.
您的问题“它们是什么”已在上面回答。
As far as debugging (your second question) though, and in developing libraries where you want to check for special input values, you may find the following functions useful in Windows C++:
至于调试(您的第二个问题),以及在开发要检查特殊输入值的库时,您可能会发现以下函数在 Windows C++ 中很有用:
_isnan(), _isfinite(), and _fpclass()
_isnan()、_isfinite() 和 _fpclass()
On Linux/Unix you should find isnan(), isfinite(), isnormal(), isinf(), fpclassify() useful (and you may need to link with libm by using the compiler flag -lm).
在 Linux/Unix 上,您应该会发现 isnan()、isfinite()、isnormal()、isinf()、fpclassify() 很有用(并且您可能需要使用编译器标志 -lm 与 libm 链接)。