C++ 浮点错误 -1.#J 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/840081/
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 does floating point error -1.#J mean?
提问by Srekel
Recently, sometimes (rarely) when we export data from our application, the export log contains float values that look like "-1.#J". I haven't been able to reproduce it so I don't know what the float looks like in binary, or how Visual Studio displays it.
最近,有时(很少)当我们从应用程序导出数据时,导出日志包含看起来像“-1.#J”的浮点值。我无法重现它,所以我不知道二进制中的浮点数是什么样的,也不知道 Visual Studio 如何显示它。
I tried looking at the source code for printf, but didn't find anything (not 100% sure I looked at the right version though...).
我试着查看 printf 的源代码,但没有找到任何东西(虽然我不是 100% 确定我查看了正确的版本......)。
I've tried googling but google throws away any #, it seems. And I can't find any lists of float errors.
我试过谷歌搜索,但似乎谷歌扔掉了任何#。而且我找不到任何浮动错误列表。
回答by Tobi
It can be either negative infinity or NaN (not a number). Due to the formatting on the field printf does not differentiate between them.
它可以是负无穷大或 NaN(不是数字)。由于字段 printf 的格式不区分它们。
I tried the following code in Visual Studio 2008:
我在 Visual Studio 2008 中尝试了以下代码:
double a = 0.0;
printf("%.3g\n", 1.0 / a); // +inf
printf("%.3g\n", -1.0 / a); // -inf
printf("%.3g\n", a / a); // NaN
which results in the following output:
这导致以下输出:
1.#J
-1.#J
-1.#J
removing the .3 formatting specifier gives:
删除 .3 格式说明符给出:
1.#INF
-1.#INF
-1.#IND
so it's clear 0/0 gives NaN and -1/0 gives negative infinity (NaN, -inf and +inf are the only "erroneous" floating point numbers, if I recall correctly)
所以很明显 0/0 给出 NaN 而 -1/0 给出负无穷大(NaN,-inf 和 +inf 是唯一的“错误”浮点数,如果我没记错的话)