• AVR Freaks

Hot!XC16 and printf of floats and doubles << SOLVED >>

Author
RicksterPIC
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2015/01/11 03:04:37
  • Location: 0
  • Status: offline
2019/01/21 14:02:58 (permalink)
0

XC16 and printf of floats and doubles << SOLVED >>

I had the MPLABX “Use 64-Bit Double” enabled.
Apparently there is only one lib and it’s absolutely reliant on 32-Bit doubles.
Linker apparently doesn’t (can’t?) detect this.

Maybe a compile time check in studio.h

/////////////
// If The Next Line FAILS
// You have the MPLABX “Use 64-Bit Double” Option Enabled.
// math library is NOT compatible with 64-Bit double
typedef char check_for_32_bit_double[5 - sizeof(double)];

Thanks everyone!
~ Rick
================
PIC24FJ256GB410
XC16 v1.35, v1.36

I’m having trouble with printf of floats giving me garbage.
Googling reveals vague allusions to printf of floats being “broken” going back many years.
Does anyone know the details?
Workarounds?

Simple example


float A, B, C;

A = 0.5;
B = 0.5;
C = 0.5;

[ oops. As Crosland points out, I made a messy typo in my post printf(“Values A=%.2f, B=%.2f, C=%.2f \r\n”);]

printf(“Values A=%.2f, B=.2f, C=.2f \r\n”, A, B , C);

>>Values A=1.75, B=0.0, C=1.75

post edited by RicksterPIC - 2019/01/22 01:57:54

~ RicksterPIC
#1

13 Replies Related Threads

    crosland
    Super Member
    • Total Posts : 1575
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/21 14:42:09 (permalink)
    0
    Slightly less simple example


    float A, B, C;

    A = 0.5;
    B = 0.5;
    C = 0.5;

    printf(“Values A=%.2f, B=%.2f, C=%.2f \r\n”, A, B, C);



    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3121
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/21 15:43:28 (permalink)
    0
    Replace with:
    double A,B,C;

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    RicksterPIC
    New Member
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2015/01/11 03:04:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/21 21:05:27 (permalink)
    0
    Gort2015 -

    Thanks for the suggestion. I had already tried casting the variables to double without success.
    I tried, as you suggested, changing the variables themselves to be doubles (with no casting), but I get the same garbage results.

    I tried going back to the “legacy c library”, but that seemed to introduce additional errors - like printf crashing somehow.

    I’m really perplexed that google seems to show this issue going back more than a decade?

    ~ RicksterPIC
    #4
    andersm
    Super Member
    • Total Posts : 2583
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 00:44:47 (permalink)
    +1 (1)
    As discussed in other threads, floats are always promoted to doubles when passed to vararg functions, so casting or changing the variable type to double won't do anything. Try changing the project options to use 64-bit doubles, or change the the variables to long doubles, which are guaranteed to be 64-bit.
    #5
    RicksterPIC
    New Member
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2015/01/11 03:04:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 01:51:09 (permalink)
    0
    Ok, I << SOLVED >> it!
    My MPLABX config had “Use 64 Bit Double” checked.
    Apparently, there is only one math library that absolutely relies on sizeof(double)=4
    Perhaps they could add a check to the studio.h - this would have saved literal days of frustration.

    Put this in studio.h
    It will fail if 64-Bit double, sizeof(double)=8
    ////////////////
    // If the following line FAILS to compile.
    // You have the “64 Bit Double” option enabled.
    // This library is NOT compatible with 32-Bit double.
    typedef check_32_bit_double[5 - sizeof(double)];

    ~ RicksterPIC
    #6
    crosland
    Super Member
    • Total Posts : 1575
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 02:04:37 (permalink)
    0 (2)
    Thanks for acknowledging that the code you posted was completely wrong.
     
    You're welcome!
    #7
    RicksterPIC
    New Member
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2015/01/11 03:04:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 07:56:29 (permalink)
    0
    “You’re welcome.”

    “Your welcome” ??

    For what?
    You provide no value add to this thread other than pointing out a typo.
    *I* solved my problem. As in *me* alone.
    You did absolutely nothing to help, other than to try to bully me with your
    passive aggressive nitpicking and your need to get the last word in.

    Go back under your stone and see if you can knock that chip off your shoulder.

    “Thank you oh lord Crosland”
    post edited by RicksterPIC - 2019/01/22 08:04:57

    ~ RicksterPIC
    #8
    ravit
    Starting Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/03/28 10:33:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 09:32:55 (permalink)
    0
    RicksterPIC
    Ok, I << SOLVED >> it!
    My MPLABX config had “Use 64 Bit Double” checked.
    Apparently, there is only one math library that absolutely relies on sizeof(double)=4
    Perhaps they could add a check to the studio.h - this would have saved literal days of frustration.

    Put this in studio.h
    It will fail if 64-Bit double, sizeof(double)=8
    ////////////////
    // If the following line FAILS to compile.
    // You have the “64 Bit Double” option enabled.
    // This library is NOT compatible with 32-Bit double.
    typedef check_32_bit_double[5 - sizeof(double)];

     
    Which math library is it that depends on sizeof(double) and causing issues for you?
    I tried a plain C file containing the following code:
    #include <stdio.h>

    int main() {
      float A,B,C;

      A=0.5;
      B=0.5;
      C=0.5;

      printf("Values A=%.2f, B=%.2f, C=%.2f \n",A,B,C);
      return 0;
    }
    Compiled it using command line:
    v1.35/bin/xc16-gcc -mcpu=24FJ256GB410 -T p24FJ256GB410.gld afile.c
     
    The output of the above file is:
    Values A=0.50, B=0.50, C=0.50
     
    Can you point out the math library file so I can avoid the issue and report it to Microchip?
    #9
    RicksterPIC
    New Member
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2015/01/11 03:04:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 11:50:18 (permalink)
    0
    Interesting. Try turning on “Use 64 Bit Double” in your project config and see what result you get. I’m curious if you can reproduce my problem.

    Well, I can certainly tell you that printf (%f) and it’s cousins are problems.
    I’m assuming - perhaps incorrectly that math functions using double as parameter or return may be issues as well.

    I think the problem stems from the fact that the header file for a library function says “double” and it seems that the library was compiled with double=32 bits.
    But your project, if it’s using 64-Bit Double doesn’t see a problem with double in the header - even though they are different.
    The linker apparently doesn’t/can’t detect this.
    And parameter passing conventions are not in synch...

    “They” could eliminate some of the confusion (and my wasted time) by putting a “compile time assert” in the header files verifying that sizeof(double)=4.
    Or have two libraries.

    I wanted my doubles to have more precision than my floats.
    So I turned on 64-Bit Double not realizing that I would break things.
    I guess I will have to use “long double” for my 64-bit floating point numbers.
    post edited by RicksterPIC - 2019/01/22 18:13:50

    ~ RicksterPIC
    #10
    ravit
    Starting Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/03/28 10:33:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 12:21:20 (permalink)
    0
    So I used the "Use 64 bit double" option which translates to -fno-short-double to the compiler. And I cannot see the problem.
     /opt/microchip/xc16/v1.35/bin/xc16-gcc -mcpu=24FJ256GB410 -T p24FJ256GB410.gld afile.c -fno-short-double
    The output from simulator is:
    Values A=0.50, B=0.50, C=0.50
    #11
    RicksterPIC
    New Member
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2015/01/11 03:04:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 13:07:20 (permalink)
    0
    Hmmm. That’s curious.
    Do you have any actual hw you could try?

    ~ RicksterPIC
    #12
    ravit
    Starting Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/03/28 10:33:37
    • Location: 0
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 13:22:34 (permalink)
    0
    RicksterPIC
    Hmmm. That’s curious.
    Do you have any actual hw you could try?

    Sorry, I don't have any hardware yet. Using the simulator. I did check the program memory window and the image, they 'look' good (as in nothing appears to be wrong).
    Maybe there is something else in your code that is causing garbage output from printf. Have you tried the skimmed down version of the code that I have used? Create a brand new project with just one file with the float code and try to compile and run it?
    #13
    Nikolay_Po
    Super Member
    • Total Posts : 1859
    • Reward points : 0
    • Joined: 2012/04/01 13:49:27
    • Location: Russia, Novorossiysk
    • Status: offline
    Re: XC16 and printf of floats and doubles 2019/01/22 23:48:46 (permalink)
    0
    Hi, Rick. Try to increase stack guard size in linker options. pritnf with floating point may be memory hungry. Also ensure you're not re-enter the pintf/sprintf from other place, from an interrupt. And yes, try to replicate the problem in minimal project.
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5