Skip to content

AVR: sprintf() not working for floats #3567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shiftleftplusone opened this issue Jul 22, 2015 · 8 comments
Closed

AVR: sprintf() not working for floats #3567

shiftleftplusone opened this issue Jul 22, 2015 · 8 comments
Labels
Type: Duplicate Another item already exists for this topic Type: Wontfix Arduino has decided that it will not resolve the reported issue or implement the requested feature
Milestone

Comments

@shiftleftplusone
Copy link

for AVRs sprintf() is not working for floats (on Due it works though).
To make it work, a lib.c float patch has to be issued.
this is actually unreasonable, e.g. for the Mega.
Please enable this feature by #include or #pragma config or anything.
On very small boards one may refrain from #including it to save memory nevertheless.

@per1234
Copy link
Collaborator

per1234 commented Jul 23, 2015

You can use dtostrf() instead on AVRs. It's an avr-libc function so you need to #include <avr/dtostrf.h> to use it with non-AVR boards.

EDIT: This is a duplicate of #1719

@shiftleftplusone
Copy link
Author

no never!!
I always need sprintf() for multi-format and multiple-parameter arguments, anything else is nonsense!

e.g.;

sprintf(sbuf, "poll: %-5d - heading: %6.2f pitch: %5.2f roll: %5.2\n", dvar, fvar1, fvar2, fvar3);
Serial.print(sbuf);
sprintf(sbuf, "%02d:%02d:%02d %02d/%02d/%02d\n ",hour, minute, second, dday, dmonth, dyear );
Serial.print(sbuf);
sprintf(sbuf,"latt: %8.2f long: %8,2f\n", gps.location.lat, gps.location.lng);
Serial.print(sbuf);

With the Due it works fine, but on the Mega without lib.c float patch this becomes very big rubbish !!

@matthijskooijman
Copy link
Collaborator

Due to the way sprintf works, enabling the float support is only possible by adding a linker option, there is not any reasonable way I can think of to enable it from inside a sketch using an include or whatever.

There has been some discussion on the list about (s)printf recently, and it was clear that it is not part of the supported Arduino API, so no extensive effort will be made to more easily support it.

If you really need float support, you will need to modify the compiler options by modifying platform.txt or boards.txt. Ideally, you could add additional compiler options per sketch to simplify this, but that isn't supported yet (IIRC there is an issue about this already).

I'm closing this issue as wontfix. If you have specific ideas about how to fix this, feel free to leave another comment (or submit a pullrequest).

@matthijskooijman matthijskooijman added the Type: Wontfix Arduino has decided that it will not resolve the reported issue or implement the requested feature label Jul 23, 2015
@shiftleftplusone
Copy link
Author

I would appreciate it you please re-opened this issue!

as by copying the lib.c float patch it seems there is a way to fix this issue for AVRs and make sprintf process float formatters by AVRs.

So would it be possible to have 2 different sprintf versions, e.g.

sprintftiny() just for small AVRs w/o float support
sprintf() like for ARM/Due feat. float support
?

If there exist 2 difefrent sprintf versions maybe one could even automate this by

#ifdef __AVRTINY__  // or however tiny AVR boards are called
 #define sprintf sprintftiny
#else 
 #define sprintf sprintffull
#endif 

BTW:
do you know the lib.c float patch?

OT:
really a crap with this editor:
can't write keywords having 2 underlined '_' in it's name
and ''' don't work for [code] ... [/code]

@matthijskooijman
Copy link
Collaborator

I'm not sure what this patch is. Do you have a link?

AFAIK, avr-libc provides two sprintf libraries, both providing a sprintf function, one with float support and one without, and you can pass a flag to the linker to make it use the float-enabled version instead of the regular, float-less version.

As for the editor - use triple backticks ``` before and after a code block. I have updated your comment to apply this.

@shiftleftplusone
Copy link
Author

to the lib.c float patch I have made an upload in our forum, please look here if you wish:
http://www.mindstormsforum.de/viewtopic.php?f=70&p=67477#p67238
http://www.mindstormsforum.de/download/file.php?id=742
I'm not sure if you must be registered for download, please tell me if it doesn't work for you!

can you please make this topic "open" again" ?
How can I issue a pull request?

@matthijskooijman
Copy link
Collaborator

to the lib.c float patch I have made an upload in our forum, please look here if you wish:

Hm, I can download those, but the zip only contains a compiled libc version, without source code or an actual .patch file, so that doesn't really tell me anything. I suspect that this might be a version of avr-libc that has float support enabled by default, removing the need for a custom linker option.

I had another look at the linker options required and the printf sources in the hope that there might be some way to automatically including the float-supported printf by referencing a special symbol, but it seems that each version exports exactly the same symbols, so that won't work.

can you please make this topic "open" again" ?

Since I'm still not sure what Arduino should be changing to improve this and it's unlikely that a lot of effort will be invested in printf-related stuff, I'd rather keep it closed until it's clear that something needs changing.

One thing I could imagine being changed is to make it easier to supply custom compiler options from a sketch, but that would warrant a separate issue (which I think already exists).

How can I issue a pull request?

That's a bit of a broad question, please check out the github docs and other stuff written about this in Google.

@shiftleftplusone
Copy link
Author

yes, the lib contains ALMOST a complete lib.c version. It must be copy-and-pasted into the program folder to replace the original ones.

Well, about this sprintf issue:
I would like to get this resolved so:
I WOULD APPRECIATE VERY MUCH YOU TO RE-OPEN IT AGAIN !

Maybe other readers will have ideas about how to fix or resolve this issue.
The way it's currently -not- working is unacceptable:
The ANSI C standard has to be performed as a default (float-suppport).
Deviations from the ANSI C standard must be initiated by the user (no float support).

Thank you!

@ffissore ffissore modified the milestone: Release 1.6.6 Aug 3, 2015
@per1234 per1234 added the Type: Duplicate Another item already exists for this topic label Jul 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Duplicate Another item already exists for this topic Type: Wontfix Arduino has decided that it will not resolve the reported issue or implement the requested feature
Projects
None yet
Development

No branches or pull requests

4 participants