c - 我如何让 printf 在 w32 上使用 'nan'?

标签 c printf nan

在开发跨平台计算器应用程序时,我刚刚注意到在 W32 上 NaN/Inf 值转换为字符串的方式与在 OSX/linux 上不同。 即。

linux/OSX 使用 nan rsp inf,而 W32 使用类似 1.#INF 的东西。 1.#QNAN.

在所有平台上获得相同表示的规范方法是什么?

我想避免使用 isnan()isinf() 手动检查值,并避免对 inf resp nan 进行硬编码 字符串表示。

(我在 What is the difference between IND and NAN numbers 中读到一些评论说真正的“罪魁祸首”是 libc 实现,而不是平台)。

最佳答案

在 C11(可能更早)中,这些表示由规范强制执行,至少部分强制执行。来自 ISO/IEC 9899:2011,7.21.6.1 fprintf 函数部分,第 8 段:

A double argument representing an infinity is converted in one of the styles [-]inf or [-]infinity — which style is implementation-defined. A double argument representing a NaN is converted in one of the styles [-]nan or [-]nan( n-char-sequence ) — which style, and the meaning of any n-char-sequence, is implementation-defined. The F conversion specifier produces INF, INFINITY, or NAN instead of inf, infinity, or nan, respectively.

除此之外,恐怕你要靠自己了。

关于c - 我如何让 printf 在 w32 上使用 'nan'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43333192/

相关文章:

c - 地址的值(value)。给出异常(exception)

c++ - 使用 double 的 sprintf 语句出错(缓冲区溢出?)

javascript - 当我们可以抛出 TypeErrors 时,为什么我们需要 NaN 值?

python - 忽略 nan 值并执行 numpy.polyval 的函数

python - 用 mode() 填充 NAN 数据不起作用-Pandas

python - 将 Quartz 2d python 演示移植到纯 Core Graphics C

c++ - array[100] = {0} 如何将整个数组设置为 0?

C: 第二次提示输入时 scanf 和 printf 错误

c - sprintf 转换在格式末尾缺少类型

c - 在任何情况下,C static 关键字应该或可以用于变量的头文件中吗?