Page 1 of 1
prinft implementation for %f
Posted: Sat Jul 14, 2007 4:31 am
by mutex
Anyone implemented %f (float) for printf??
-
Thomas
Posted: Sat Jul 14, 2007 4:55 am
by Pyrofan1
%d is for integers.
Posted: Sat Jul 14, 2007 5:01 am
by mutex
Anyway...
![Shocked :shock:](./images/smilies/icon_eek.gif)
Im interessted in decimal numbers from fpu and how you show them with printf..
-
Thomas
Posted: Sat Jul 14, 2007 5:31 am
by AndrewAPrice
thomasnilsen wrote:Anyway...
![Shocked :shock:](./images/smilies/icon_eek.gif)
Im interessted in decimal numbers from fpu and how you show them with printf..
-
Thomas
Print it as an integer, the add a '.' then subtract the integer from the float, and multiply the float until all digits are left of the decimal point, and print that as an integer. It's easier with C++ since you can just add an "operator<<(float data)" to your console class.
Posted: Sat Jul 14, 2007 9:05 am
by mutex
Hmm but a float is not stored like that??
or is a 32 bit float 10.00000256 stored like [0x0a][0x0000FF] in memory??
If its stored like that then its easy.. but thought it was different.. :-/
cheers
Thomas
Posted: Sat Jul 14, 2007 9:15 am
by frank
No floats have a special format
IEEE 754 that they use to store the number.
Posted: Sat Jul 14, 2007 9:25 am
by mutex
Yes that's what it thougt.. So how do i convert this exponent part to an integer? Cant to any exp maths without floats??
Do i need lookup tables or what?
Im a little bit confused here..
-
Thomas
Posted: Sat Jul 14, 2007 10:29 am
by frank
I think this is more of what they were saying. Note print is just a function that will take a integer and print it.
Code: Select all
void Print_Float( float value )
{
// print the integral part
print( (int)value );
// now get rid of the integral part
value -= ((int)value);
// print the decimal point
printchar( '.' );
// 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
print( (int)value );
} // end Print_Float
Something like that should work.
Posted: Sat Jul 14, 2007 10:34 am
by mutex
But that wont work.. It wont show the correct value...?
Posted: Sat Jul 14, 2007 10:39 am
by frank
Yes it will be off a little bit. I just testing it and 1/3 printed 0.33333338. But that is good enough for now don't you think.
Posted: Sat Jul 14, 2007 10:46 am
by mutex
Hmm.. i was thinking the difference would be mouch greater.. This is good enough for me. I will implement right away
Thanks alot.
-
Thomas
Posted: Sat Jul 14, 2007 4:27 pm
by Assembler