float to char conversion.

Programming, for all ages and all languages.
Post Reply
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

float to char conversion.

Post by cjhawley001 »

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
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: float to char conversion.

Post by Alboin »

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.
C8H10N4O2 | #446691 | Trust the nodes.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: float to char conversion.

Post by cjhawley001 »

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?
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: float to char conversion.

Post by Alboin »

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.
C8H10N4O2 | #446691 | Trust the nodes.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: float to char conversion.

Post by cjhawley001 »

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...
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: float to char conversion.

Post by frank »

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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: float to char conversion.

Post by Solar »

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, ...
Every good solution is obvious once you've found it.
User avatar
Telgin
Member
Member
Posts: 72
Joined: Thu Dec 20, 2007 1:45 pm

Re: float to char conversion.

Post by Telgin »

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
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.

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.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: float to char conversion.

Post by cjhawley001 »

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
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: float to char conversion.

Post by DeletedAccount »

Why dont you use ceil() and floor() instead of typecasting to int ....

Regards
Sandeep Mathew
Post Reply