prinft implementation for %f

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

prinft implementation for %f

Post by mutex »

Anyone implemented %f (float) for printf??

-
Thomas
Last edited by mutex on Sat Jul 14, 2007 5:05 am, edited 1 time in total.
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

%d is for integers.
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Post by mutex »

Anyway... :shock: Im interessted in decimal numbers from fpu and how you show them with printf..

-
Thomas
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

thomasnilsen wrote:Anyway... :shock: 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.
My OS is Perception.
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

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

Post by frank »

No floats have a special format IEEE 754 that they use to store the number.
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

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

Post 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.
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Post by mutex »

But that wont work.. It wont show the correct value...?
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post 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.
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Post 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
User avatar
Assembler
Member
Member
Posts: 30
Joined: Fri Oct 27, 2006 5:26 am
Contact:

Post by Assembler »

Systems and Computer Engineering Researcher
"Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?" -- Linus Torvalds
http://sce.carleton.ca/~maslan
Post Reply