【发布时间】:2019-06-18 10:00:22
【问题描述】:
如果我通过 std::cout 打印 NaN 或将其转换为字符串,标准是否说它必须是“nan”作为字符串?
是否有其他方法可以将 NaN 转换为字符串,但事实并非如此?
#include <iostream>
#include <limits>
int main() {
float x = std::numeric_limits<float>::quiet_NaN();
std::cout << x << '\n'; // Output: nan
std::cout << std::to_string(x) << '\n'; // Output: nan
printf("%f", x); // Output: nan
// possibly other variants to print x
}
【问题讨论】:
-
没有。在 Windows/VC++ 中它看起来不同。 (不幸的是,我忘记了。)但是,我找到了这个 Q/A:SO: NaN ASCII I/O with Visual C++。 (它显示了我大致记得的内容。)
-
找到与@foreknownas_463035818 相同的链接。在 VC++ 中如何完成事情可能是一个错误。不幸的是,相应的。 Hans Passant 的链接已经烂了。
-
我还询问了 std::to_string 以外的其他变体,所以不完全是重复的。
-
我认为this answer 涵盖了使用
cout(即使问题是专门关于Visual C++,我认为答案的范围更广) -
这让我抓狂微软决定在其 Connect 平台上立即中断所有指向错误报告的链接,绝对没有重定向,也没有任何指示如何找到移动的内容.在我看来,这确实说明了那里的文化。
标签: c++ floating-point nan