What float value makes sprintf_s() produce “1.#QO”?

前端 未结 4 1043
清歌不尽
清歌不尽 2020-12-06 17:09

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 rep

4条回答
  •  清歌不尽
    2020-12-06 17:25

    After a lot of fiddling around I can conclusively say that setting a 4-byte float to 0x7FFFFFFF and passing it into sprintf_s with a format specifier of %.3f is what gave me 1.#QO:

    const int bufSize = 100;
    char buf[bufSize];
    unsigned int i;
    float* f = (float*)&i;
    int retval;
    
    i = 0xFFFFFFFF;
    retval = sprintf_s(buf, bufSize, "%.3f\n", *f);
    printf("sprintf_s returned %d, converted val = %s", retval, buf); // == sprintf_s returned 7, converted val = -1.#QO
    retval = sprintf_s(buf, bufSize, "%f\n", *f);
    printf("sprintf_s returned %d, converted val = %s", retval, buf); // == sprintf_s returned 10, converted val = -1.#QNAN0
    
    i = 0x7FFFFFFF;
    retval = sprintf_s(buf, bufSize, "%.3f\n", *f);
    printf("sprintf_s returned %d, converted val = %s", retval, buf); // == sprintf_s returned 6, converted val = 1.#QO
    retval = sprintf_s(buf, bufSize, "%f\n", *f);
    printf("sprintf_s returned %d, converted val = %s", retval, buf); // == sprintf_s returned 9, converted val = 1.#QNAN0
    

    ...it seems that the %.3f format specifier was cropping the NAN result so what should have been 1.#QNAN0 was being chopped down to 1.#QO.

提交回复
热议问题