sprintf %f and dtostrf

Hi,
I'm using a radiohead library example that uses dtostrf to write to a buffer, I then switched to sprintf with a couple of %f (float) variables. It worked great. When I commented out the dtostrf line it quit working.

Since then I read in these forums that Arduino had trouble with sprintf and floats. This must be a key to the solution? Pass this along to whomever is smart enough to fix the library? As long as you make a call to dtostrf first, then the sprintf works fine with floats.

Cheers

MtneerInTN2

It is not broken, rather support for floats has been removed in order to save memory. As you say, the workaround is first to convert the float to a string then to use that.

No I don't mean convert to a string and use that. Any reference to dtostrf "undisables" it.

In setup() I have...
//clears up an sprintf float problem (my solution MtneerInTN2 3/28/2020)
float val =3.3;
char s1[10];
dtostrf(val,6,2,s1);

and in Loop() i have
n=sprintf(radiopacket,"%6.2f,%6.2f",measuredvbat,busvoltage);

and it works fine

On AVR based Arduinos (e.g. Arduino Uno, Pro Mini, Mega), sprintf() does NOT support %f by default. It is possible to load an alternative library that does support %f. The instructions can be found by using your favorite search engine.

What Arduino are you using?

Please post a complete example program

Which Arduino are you using ?

Adafruit's Feather LoRa M0.

I'm not using a different library. A simple call to d2strf re-enables floats in sprintf.

Now that I hear it was intentionally disabled, I kinda wish I hadn't posted my "fix" since they may disable that too.

The M0 is not an AVR-based Arduino, and the library (which is completely different than for the AVR Arduinos) presumably does support %f in sprintf().

There is probably something else wrong with your code -- an excellent bet would be array bound violations. BTW we recommend to ALWAYS use snprintf().

The M0 has an ARM processor, tons of flash and 32K RAM. Odd they would disable %f....

Regards,
Ray L.

//You are right! I did use an additional library to fix the float handling on sprintf, i guess calling it once fixes it.

//example of a fail...
#include <SPI.h> // serial periphery library

void setup() {
// put your setup code here, to run once:
Serial.begin(230400);
while (!Serial);
Serial.println("sprintf test");

}

void loop() {
// put your main code here, to run repeatedly:
int n;
float m=3.3;
float b = 3.0;
char radiopacket[20];

n=sprintf(radiopacket,"%6.2f,%6.2f",m,b);
Serial.print("bm " ); Serial.println(radiopacket);

}

//example of success...
#include <SPI.h> // serial periphery library
#include <avr/dtostrf.h>

void setup() {
// put your setup code here, to run once:
Serial.begin(230400);
while (!Serial);
Serial.println("sprintf test");

//clears up an sprintf float problem (my solution MtneerInTN 3/28/2020)
float val =3.3;
char s1[10];
dtostrf(val,6,2,s1);

}

void loop() {
// put your main code here, to run repeatedly:
int n;
float m=3.3;
float b = 3.0;
char radiopacket[20];

n=sprintf(radiopacket,"%6.2f,%6.2f",m,b);
Serial.print("bm " ); Serial.println(radiopacket);

}

jremington thanks for the tip on snprintf !

jremington thanks for the tip on snprintf !

I once spent most of a day debugging a very mysterious and frustrating intermittent crash, to finally realize that I had used sprintf() with an output buffer that was too small.

sprintf(), strcpy(), etc. will happily trash a large segment of memory for you, whereas with snprintf(), in the worst case the output will be truncated.

Lesson learned.