Hi, I am looking for a function that can help me convert a float to a string. I use itoa for int to char array and that works fine, but any help on this would be great. I am using Objective C
It is C or objective C? ^^

In C you can use snprintf():

Code:
char str[32];
float f = 0.1;
snprintf(str, 32, "%f", f);

I believe it is available in libfxcg (or sprintf() at least should be).

Objective C is a whole different matter... and, as far as I am aware, unsupported on the Prizm?
Lephe wrote:
It is C or objective C? ^^

In C you can use snprintf():

Code:
char str[32];
float f = 0.1;
snprintf(str, 32, "%f", f);

I believe it is available in libfxcg (or sprintf() at least should be).

Objective C is a whole different matter... and, as far as I am aware, unsupported on the Prizm?


It is in fact C not Objective C. Whoops! And thank you very much!
libfxcg printf doesn't (currently?) support float formatting. Reasonable float formatting tends to be surprisingly difficult, because many values can't be precisely represented so doing the "right thing" is often a matter of careful rounding and truncation.

You can roll your own by repeated multiplication or division by 10 and extracting individual digits to display them, and knowing your application will probably make that simpler than general-purpose float formatting would be (since I'd guess odds are good you only need some fixed precision).
Apologies for the misinformation there. I'm considering using or implementing a version of the Grisu algorithm (the main concern being to keep it decently small). Would that be welcome as a contribution to libfxcg? No deadline there, just seeing if I can be of help. ^^
I don't see why not.
Nice, so I can get Florian Loitsch's original Grisu2 to build, it requires the double-precision ceil() function but I believe this is available in libfxcg. Took a detour to port OpenLibm to my personal setup, this should be working shortly. ^^

Since the code is going to be quite large because of a precomputed table of powers of ten (something like 8 kiB), would an option to not pull the floating-point printf be welcome? Is there any existing architecture that would allow that in libfxcg?
Easiest to ensure things get built with -ffunction-sections and linked with -gc-sections so unused functions don't get linked; nothing else should be required.
As far as I can see from the code, since _v_printf() explicitly references function pointers for every formatter, even with -ffunction-sections any use of a printf-like function will pull in the float formatting because the dependency is there. So what I mean more specifically is to separate integers/strings/etc from float formatting (if there is a need of course).
I finally got around to building Grisu2b and implementing %f, %e and %g in my library. There's an insane number of edge cases which I hopefully got covered.

Ultimately the code is going to be quite large, something around 10 kB to 15 kB, mainly because of the data tables required by the representation algorithm. (This also explains why fxlib's printf() is 18 kB on the mono series.)

I'll get myself an up-to-date PrizmSDK with libfxcg. In the meantime, do you have any policy about hosting third-party code in the repository?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement