This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Having a hard time printing floats that are less than 1 with many digits behind the decimal point using NRF_LOG_FLOAT.

Here is the code I'm using to test it out:

static float number   = 0.5678905;

NRF_LOG_INFO("Float number: "NRF_LOG_FLOAT_MARKER"\n\r",  NRF_LOG_FLOAT(number));

And this is what is printed out:

:INFO:Float number: 0.56

The float is reduced to only two decimal points precision.

If I try changing the macro from

#define NRF_LOG_FLOAT_MARKER "%d.%02d"

to

#define NRF_LOG_FLOAT_MARKER "%10d.%08d"

It prints this:

:INFO:Float number:          0.00000056

So, changing the number behind the decimal point in the macro only moves the value, and fills the remaining space with 0's (or empty spaces if I leave out the zero from the macro definition)

Does anyone know how I can remedy this?

Parents
  • I figured it out.

    I had to change the factor in this macro as well, from

    #define NRF_LOG_FLOAT(val) (int32_t)(val),                                 \
                           (int32_t)(((val > 0) ? (val) - (int32_t)(val)       \
                                                : (int32_t)(val) - (val))*100)
    

    to

    #define NRF_LOG_FLOAT(val) (int32_t)(val),                                      \
                           (int32_t)(((val > 0) ? (val) - (int32_t)(val)            \
                                                : (int32_t)(val) - (val))*100000000)
    

    to give the float the correct precision.

    Now it prints:

    :INFO:Float number:          0.56789052
    
Reply
  • I figured it out.

    I had to change the factor in this macro as well, from

    #define NRF_LOG_FLOAT(val) (int32_t)(val),                                 \
                           (int32_t)(((val > 0) ? (val) - (int32_t)(val)       \
                                                : (int32_t)(val) - (val))*100)
    

    to

    #define NRF_LOG_FLOAT(val) (int32_t)(val),                                      \
                           (int32_t)(((val > 0) ? (val) - (int32_t)(val)            \
                                                : (int32_t)(val) - (val))*100000000)
    

    to give the float the correct precision.

    Now it prints:

    :INFO:Float number:          0.56789052
    
Children
Related