float to char conversion.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
float to char conversion.
I need some help on converting a float to a char value, i dont exactly need code, but the method behind it, or something else would be great!
chris
chris
Re: float to char conversion.
As Solar so wisely mentioned in your previous thread, sprintf is your friend.
It's like printf, but with an 's' in front of it. Epic.
It's like printf, but with an 's' in front of it. Epic.
C8H10N4O2 | #446691 | Trust the nodes.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: float to char conversion.
sadly to say, i do not have access to sprintf, i know of it, and i use it when programming applications, but what is going on behind the scenes is a blur to me.
is there a way i can do it with just math functions, like i did with int to char and char to int?
is there a way i can do it with just math functions, like i did with int to char and char to int?
Re: float to char conversion.
You're probably going to have to dissect the float. They're in they own little format, so that's one method, I suppose...
My 2:00am mind can only say good luck.
My 2:00am mind can only say good luck.
C8H10N4O2 | #446691 | Trust the nodes.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: float to char conversion.
this looks like it will turn out to be just a jolly week...ha...
i love programming, but this will be a tad bit....interesting...
i love programming, but this will be a tad bit....interesting...
Re: float to char conversion.
I once made a hacked together print float function. Here is the source, maybe you can work with it a little. I warn you the value it prints is not going to always be exactly correct. It's a lot easier than parsing a float.
Code: Select all
void Print_Float( float value )
{
// print the integral part
printf( "%d", (int)value );
// now get rid of the integral part
value -= ((int)value);
// print the decimal point
printf( "%c", '.' );
// now the decimal part, make sure everything is to the left
// of the decimal point.
// NOTE: You may want to make a cut off after so many tries since this
// would lock up on numbers like 1/3
while( value != (int)value )
{
value *= 10;
} // end while
// now print it
printf( "%d", (int)value );
} // end Print_Float
Re: float to char conversion.
Floats are tricky.
You see the comment about termination of the algorithm frank posted. DBL_DIG and FLT_DIG of <float.h> can help you there - if you have that available.
But even then - floats can be NaN (not a number), Inf (infinite positive or negative)...
To handle them correctly (as in the CS meaning of the word), you'd need much of the infrastructure in <fenv.h>, and some of the functions in <math.h> too (isnan(), isinf(), ...).
(Note, this is why I don't have float support in my stdio code yet.)
If it's for kernel debugging work, I'd simply print the register value as hex, and be done with it.
For reasons unrelated to printout, I would think twice about using float values in kernel space at all. More registers to save on context switch, more headaches regarding 64/80 bit mode on x86 FPU, ...
You see the comment about termination of the algorithm frank posted. DBL_DIG and FLT_DIG of <float.h> can help you there - if you have that available.
But even then - floats can be NaN (not a number), Inf (infinite positive or negative)...
To handle them correctly (as in the CS meaning of the word), you'd need much of the infrastructure in <fenv.h>, and some of the functions in <math.h> too (isnan(), isinf(), ...).
(Note, this is why I don't have float support in my stdio code yet.)
If it's for kernel debugging work, I'd simply print the register value as hex, and be done with it.
For reasons unrelated to printout, I would think twice about using float values in kernel space at all. More registers to save on context switch, more headaches regarding 64/80 bit mode on x86 FPU, ...
Every good solution is obvious once you've found it.
Re: float to char conversion.
Err, it's not entirely clear what you mean. If you just wanted to convert a float variable to a char variable, depending on the language you're working in, you can implicitly or explicitly just do it. Not that it would have much meaning afterwards, of course.cjhawley001 wrote:I need some help on converting a float to a char value, i dont exactly need code, but the method behind it, or something else would be great!
chris
I take it by the other posts that you've brought this up before and are actually trying to convert a float to a text string (c-style string in this case I take it).
Printing the whole part of the number would be easy. Printing the decimal part isn't so much. I'd recommend that you multiply the decimal part by 10 a set number of times, then remove the whole part and print it after the decimal.
I.e., say you want to print 3.333333
Convert it to an int, and print that. That will print 3. Then, subtract the int from the float leaving you with the decimal part (.333333) and print a decimal point. Now, multiply it by 10, and convert it to an int then print that. That will print another 3, since that's the next part of the decimal part. Since you can't tell how many digits there might be behind the decimal without a lot of work, you may as well just do a set number of digits like 10 in front of the decimal and 10 behind it.
Not that hard really, just not that flexible. If you want really flexible code, then working with the underlying float format might be a good choice. Not for the faint of heart though, at least if you haven't got a decent amount of experience with working with bit twiddling and the like.
I assume that the float format used in x86 is the same as used in MIPS, which I have written assembly code to manipulate. If it is, then I can say that once you've got the spec in front of you it's not so bad. It stores the number with an exponent and actual number, so figuring out how many digits to print is easier. A little work, but more flexible than the algorithm above, especially if for some reason you've got to detect special cases like infinite numbers.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: float to char conversion.
yea i was thinking about seperating it out, i just didnt know how exactly to do that, but now it is clear, thank you so much for all your help,
thank you all very much!
chris
thank you all very much!
chris
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: float to char conversion.
Why dont you use ceil() and floor() instead of typecasting to int ....
Regards
Sandeep Mathew
Regards
Sandeep Mathew