Page 1 of 1

Conversion from double(or float) to string ?

Posted: Thu Jul 21, 2005 12:26 pm
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' .....

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

Posted: Fri Jul 22, 2005 12:51 am
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. ;)

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

Posted: Tue Jul 26, 2005 3:54 am
by Candy
viral wrote: double pi = 3.141592 ;
You do realise that you rounded pi wrong?

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

Posted: Wed Jul 27, 2005 9:33 am
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();"

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

Posted: Wed Jul 27, 2005 12:33 pm
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?

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

Posted: Thu Jul 28, 2005 1:18 am
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).

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

Posted: Thu Jul 28, 2005 4:19 am
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.

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

Posted: Fri Jul 29, 2005 2:16 am
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..

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

Posted: Fri Jul 29, 2005 3:35 am
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

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

Posted: Fri Jul 29, 2005 1:45 pm
by viral
Hey I dint considered that things. But its ok as i dont need them right now.

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

Posted: Mon Aug 15, 2005 2:43 am
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.