Conversion from double(or float) to string ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
viral

Conversion from double(or float) to string ?

Post by viral »

hi..
I am trying to make a routine like ltoa() which converts a 'double or float' into a string (char *str), but i am stuck. Can any1 know how can it be done..

e.g. double pi = 3.141592 ;
char str[100];

double_to_string(str, pi) ;
printf(" %s", str);

And this should print '3.141592' .....
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Conversion from double(or float) to string ?

Post by Solar »

Try this submission to uclibc for inspiration. Honor the copyright, though.

That's the result of a Google for "double to string C". You might find PD / GPL'ed solutions out there, I didn't bother to look beyond the first hit. ;)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Conversion from double(or float) to string ?

Post by Candy »

viral wrote: double pi = 3.141592 ;
You do realise that you rounded pi wrong?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Conversion from double(or float) to string ?

Post by Pype.Clicker »

iirc, PI (aswell as e, for instance) can be obtained from the FPU, no ? there should be something like "floadpi" or we could at least have "fload (-1); farcos();"
viral

Re:Conversion from double(or float) to string ?

Post by viral »

Thnx for the link......
BTW I need the function to get string from float and not to find pi value.. But still I want to know how can I get values of PI, e (2.71 i guess ) etc directly from FPU. I also need sin() and cos() function... how to code them?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Conversion from double(or float) to string ?

Post by Solar »

For FPU functions, check the Intel manuals. sin() and cos() should be available from the FPU, too. As to how to write such functions in software, try to find a copy of Cody & Waite's "Software Manual for the Elementary Functions" to get the background, and / or C.J. Plauger's "The C Standard Library" for an implementation example (which is under copyright though).
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Conversion from double(or float) to string ?

Post by Candy »

Solar wrote: ... try to find a copy of Cody & Waite's "Software Manual for the Elementary Functions" to get the background...
It's a book that's near impossible to find and if you find one for sale, it'll be $160+. Even though it never cost more than $50 new, it will cost you loads more second-hand.

If you need to just convert a floating point number to decimal, first decide where the split between whole and partial is, then write the number down. It's complex, just try to get it to work first.
viral

Re:Conversion from double(or float) to string ?

Post by viral »

Thnx again...
Problem solved. I broke the float as integer and decimal and than by some for loops and some manipulation it worked..
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Conversion from double(or float) to string ?

Post by Solar »

...and the advanced students are now asked to add special cases for NAN or infinite values, exponential notation for extremely large or small values etc.

:D
Every good solution is obvious once you've found it.
viral

Re:Conversion from double(or float) to string ?

Post by viral »

Hey I dint considered that things. But its ok as i dont need them right now.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Conversion from double(or float) to string ?

Post by Candy »

Solar wrote: ... exponential notation for extremely large or small values etc.
For reference, they're called denormal and are only applicable to small values. They denote that the value stored has no implicit leading zero and that the exponent is the maximum at the lower side. There are extra leading zeroes (possibly) in the mantissa, which decreases the precision but increases the range.

NaN are results that are invalid, but you must be able to pass some to a function for further calculation, so you can speed up some code paths. They're called QNaNs, which are Quiet. Others instantly call an exception, SNaNs (S for Signalling). The third types (infinicies) usually return another type of infinite or zero.

For practical application, NaN returns a QNaN and in case of an SNaN as input an exception, infinite returns 0 (or a value close enough to 0 to be considered equivalent as far as I can tell) and denormals return -1 times themselves (since the derivate is close enough to -1 at that place, the return value is the inverse).

Modern standards figure that denormals aren't used so you might as well throw them away. They just ignore them.
Post Reply