I see it too, so puzzling. Will investigate and post back with any findings and a schedule for a fix.
Iām doing local builds with 0.4.4 and the following is yielding āā
char logmsg[256];
sprintf(logmsg,"%.2f", 3.26f);
was there a regression or did I misread the above comments?
There must be a regression. I get the same behavior.
Iāll be sure to add a unit test to our CI build for this so it doesnāt slip by again.
Iām sorry this managed to work itās way out of the 0.4.4 release. Iāve definitely fixed this and added an automated test so this shouldnāt happen again. The issue is here - https://github.com/spark/firmware/issues/576
The fix is in develop now, and will be rolled out with 0.4.5 Wednesday next week (2 Sept.)
For anyone looking for a workaround, you can replace
float value = 1.0;
sprintf(buf, "it's %f", value);
```
with
```
float value = 1.0
sprintf(buf, "it's %s", String(value).c_str());
```
(change `%f` to `%s` and explicitly convert the float to a string before passing to `sprintf`)
Thanks @mdma. From a memory perspective would that method be preferable to using sprintf?
Not really preferable, but doesnāt do any harm. On the photon memory fragmentation is less of an issue due to the relatively large heap. Just consider it a workaround until the 0.4.5 release.
Cheers
Another alternative, not very pretty though
float temp = dht.getTempCelcius();
int temp1 = (temp - (int)temp) * 100;
sprintf("%0d.%d", (int)temp, temp1);
@mdma - I tried your workaround and it worked fine with the small number in your example. I tried a larger number and it gave a value I wasnāt expecting :
float myfloat=340000.0;
sprintf(spark_var2, "%s", String(myfloat).c_str());
returns :
4294965149.4294483648
Ah yeah - thereās an overflow issue. You can avoid that by reducing the number of digits of precision required.
float myfloat=340000.0;
sprintf(spark_var2, "%s", String(myfloat, 2).c_str());
This issue https://github.com/spark/firmware/issues/563 addresses that - fix is in develop and will be released with 0.4.5.
sprintf is working well with floats using the develop branch. Thanks!
@mdma - any news on rolling out 0.4.5 to the cloud compiler ?
We are trying to get it out today!
You just made my weekend.
Jason
Was it released ? I cant find an announcement.
Yup, itās the new default in the web ide
It Works!!!
Thank you!
Works great - thanks @mdma