Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c - What float value makes sprintf_s() produce "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);

where:

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

I'm compiling with MS Visual Studio 2008.

[Edit 2] A bit more digging shows 0xFFFFFFFF gives -1.#QO:

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

..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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"-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'.

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

QNaN is quiet NaN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...