什么浮点值使 sprintf_s() 产生“1.#QO"? [英] What float value makes sprintf_s() produce "1.#QO"?

查看:19
本文介绍了什么浮点值使 sprintf_s() 产生“1.#QO"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(遗留的嵌入式 c)代码,它通过一些 sprintf 调用生成一个 .csv 文件.偶尔我会看到 1.#QO 的值.我试过用应该给出负无穷大、正无穷大和 NaN 的条件来重现这些值,但它们似乎都没有给我神奇的 1.#QO 结果.那么是什么产生了这个价值?

I have some (legacy embedded c) code which produces a .csv file by means of some sprintf calls. Occasionally I see values of 1.#QO. I've tried reproducing those values with conditions which should give negative infinity, positive infinity and NaN but none of them appear to give me the magical 1.#QO result. So what is it that produces that value?

...是的,我知道产生该值的数学显然有问题,但了解它的含义将有助于调试工作.

...and yes, I know there's obviously something going wrong in the maths which produce that value, but understanding what it means would assist in the debugging effort.

[Edit 1] 进行转换的实际行是:

The actual line which does the conversion is:

sprintf_s(txt, CSV_HEADER_SIZE, "%.3f", value);

哪里:

#define CSV_HEADER_SIZE (100)
char txt[CSV_HEADER_SIZE];

我正在使用 MS Visual Studio 2008 进行编译.

I'm compiling with MS Visual Studio 2008.

[Edit 2] 进一步挖掘显示 0xFFFFFFFF 给出 -1.#QO:

A bit more digging shows 0xFFFFFFFF gives -1.#QO:

unsigned int i = 0xFFFFFFFF;
float* f = (float*)&i;
printf("%.3f", *f); // gives -1.#QO

..并在 Visual Studio 调试器中查看将其扩展为 -1.#QNAN00 所以看起来这可能是 NaN 的 Microsoft 特定表示?

..and looking at that in the Visual Studio debugger expands it to -1.#QNAN00 so it looks like this is probably a Microsoft-specific representation of NaN?

推荐答案

"-1.#QO" 小数点后三位四舍五入"后为-1.#QNAN".N 舍入为 O 为​​ 'A' >= '5' 和 'N' + 1 == 'O'.

"-1.#QO" is "-1.#QNAN" after "rounding" for 3 places after the decimal. The N rounds to an O as 'A' >= '5' and 'N' + 1 == 'O'.

这与您的调试器显示-1.#QNAN00"的原因类似,因为它打印了 7 个位置并在末尾添加了填充零.

This is similarly why your debugger shows "-1.#QNAN00", as it prints with 7 places and adds padding zeros to the end.

QNaN 是 安静的 NaN.

这篇关于什么浮点值使 sprintf_s() 产生“1.#QO"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆