Conversion from double(or float) to string ?
Conversion from double(or float) to string ?
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' .....
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 ?
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.
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.
Re:Conversion from double(or float) to string ?
You do realise that you rounded pi wrong?viral wrote: double pi = 3.141592 ;
- Pype.Clicker
- 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 ?
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 ?
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?
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 ?
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.
Re:Conversion from double(or float) to string ?
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.Solar wrote: ... try to find a copy of Cody & Waite's "Software Manual for the Elementary Functions" to get the background...
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 ?
Thnx again...
Problem solved. I broke the float as integer and decimal and than by some for loops and some manipulation it worked..
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 ?
...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.
Every good solution is obvious once you've found it.
Re:Conversion from double(or float) to string ?
Hey I dint considered that things. But its ok as i dont need them right now.
Re:Conversion from double(or float) to string ?
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.Solar wrote: ... exponential notation for extremely large or small values etc.
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.