• AVR Freaks

Hot!sprintf() outputs : instead of 1 when rounding float value

Author
ondre
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2015/01/07 09:31:23
  • Location: 0
  • Status: offline
2020/01/27 05:33:43 (permalink)
0

sprintf() outputs : instead of 1 when rounding float value

Hi,
I've encountered a strange problem with sprintf() when it is supposed to round a float to the next power of ten:
void main(void)
{
 char s[20];
 float f = 9.997296;
 sprintf(s, "%6.2f", f);
}

Instead of ' 10.00' the string s now contains ' :.00'. This also happens at 99.99... or 999.99.... Every other number seems to be converted just fine.

The code is compiled for a PIC16F15356 with XC8 Version 2.10. The problem does not occur, when I switch the C standard to C90 instead of C99!

What am I doing wrong here?

Thanks for your help!

Cheers
Andre



post edited by ondre - 2020/01/27 05:35:42

Attached Image(s)

#1

2 Replies Related Threads

    davekw7x
    Entropy++
    • Total Posts : 1834
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: sprintf() outputs : instead of 1 when rounding float value 2020/01/27 09:47:52 (permalink)
    +1 (1)
    ondre
    What am I doing wrong here?


    I see nothing wrong with your code, and, in fact, I get the same results.

    Note that I always print out results through a serial connection so that I am never fooled by debugger/IDE "issues."

    Part of my PIC16F15356 test program on Curiosity HPC:

        // Initialization stuff
    #ifdef __CLANG__
        printf("\r\nCompiled on %s at %s by XC8 version %u (C99 mode)\r\n",
                __DATE__, __TIME__, __XC8_VERSION);
    #else
        printf("\r\nCompiled on %s at %s by XC8 version %u (C90 mode)\r\n",
                __DATE__, __TIME__, __XC8_VERSION);
    #endif
        
        char s[20];
        float f = 9.997296;
        sprintf(s, "%6.2f", f);
        printf("Using sprintf: <s> = <%s>\r\n", s);
        printf("Using printf : <f> = <%6.2f>\r\n", f);
        // Other stuff


    Output with C99:
    Compiled on Jan 27 2020 at 08:35:45 by XC8 version 2100 (C99 mode)
    Using sprintf: <s> = <  :.00>
    Using printf : <f> = <  :.00>

    Output with C90
    Compiled on Jan 27 2020 at 08:36:31 by XC8 version 2100 (C90 mode)
    Using sprintf: <s> = < 10.00>
    Using printf : <f> = < 10.00>
     
    Note that results were the same with optimization levels 0, 1, and 2.
     
    Bottom line: File a bug report with Microchip.

    Regards,

    Dave
    post edited by davekw7x - 2020/01/27 09:54:55

    Sometimes I just can't help myself...
    #2
    ondre
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2015/01/07 09:31:23
    • Location: 0
    • Status: offline
    Re: sprintf() outputs : instead of 1 when rounding float value 2020/01/27 10:09:39 (permalink)
    +1 (1)
    Thanks for the confirmation!
     
    My original code was outputing the string on a display with the same result. Usually I don't trust the debugger as well, especially when it comes to floats :-)
     
    Bug report has been submitted.
     
    Cheers
    Andre
    post edited by ondre - 2020/01/27 10:24:27
    #3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5