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

impossible to print floats with snprintf

Hello,

I am working on asset tracker from ncs 1.4.0 and on NRF9160DK
when i try to concatenate doubles with snprintf it shows me nothing and returns a huge value (several hundred thousand)

I saw this topic: but I do not have access to the settings indicated.
On another topic I saw that it was necessary to activate the FPU, but it is already the case in the options.

Can someone help me?

Best regards
Parents
  • Hi!

    Could you provide a code snippet and the corresponding output?

    The ticket you referenced is about the nRF5 SDK, and you're using NCS, which is why you don't have those configurations. 

    Note: a usual pitfall with snprintf is to design code that assumes the function returns the number of characters written to the buffer. The number returned is actually just the number of characters in the resulting string. If the resulting string is longer than size(buf) - 1 characters, the remaining characters are discarded and not stored but counted for in the functions return value. 

    The FPU should be enabled by its dependencies (CPU_HAS_FPU && ARM). 

Reply
  • Hi!

    Could you provide a code snippet and the corresponding output?

    The ticket you referenced is about the nRF5 SDK, and you're using NCS, which is why you don't have those configurations. 

    Note: a usual pitfall with snprintf is to design code that assumes the function returns the number of characters written to the buffer. The number returned is actually just the number of characters in the resulting string. If the resulting string is longer than size(buf) - 1 characters, the remaining characters are discarded and not stored but counted for in the functions return value. 

    The FPU should be enabled by its dependencies (CPU_HAS_FPU && ARM). 

Children
  • Hi !

    I am sincerely sorry but everything is back to normal. I had to change a setting which removed the bug.
    I still send you my code in case you are interested.

    the output was:
    ...
    position:; ; ;

    }

    and my code was:

    static char buffer_json[2048];
    static double longitude;
    static double latitude;
    static double altitude;
    
    int encode_JSON_data(char *const why)
    {       
            
            int buffer_length = 0;
            buffer_json[0] = '\0';
            
            char * sn = "005115541";
            
    
    
            buffer_length += snprintf(buffer_json + buffer_length, sizeof(buffer_json) - buffer_length, "{\n");
            buffer_length += snprintf(buffer_json + buffer_length, sizeof(buffer_json) - buffer_length, "  \"SN\": \"%d\",\n", sn);
            buffer_length += snprintf(buffer_json + buffer_length, sizeof(buffer_json) - buffer_length, "  \"what\": \"%s\",\n", why);
            buffer_length += snprintf(buffer_json + buffer_length, sizeof(buffer_json) - buffer_length, "  \"position\": \"%f;%f;%0.1f\"\n", latitude, longitude,altitude);
            buffer_length += snprintf(buffer_json + buffer_length, sizeof(buffer_json) - buffer_length, "}");
    
            // vérifications de base
            if ((buffer_length >= (sizeof(buffer_json) - 1)) || buffer_json[buffer_length - 1] != '}')
            {
                LOG_ERR("JSON buffer overload.");
                return 0;
            }else 
            {
                return 1;
            }
    
    }

    Thank you for your reply,

    Best regards,

    Lecozahu

Related