Custom printf - Floats
Posted: Thu Apr 28, 2011 7:34 pm
Due to there being no standard C library, I have started to write the functions I need, such as printf (for x86).
Mostly, I can get this to work, but I am stuck trying to figure out how to handle the printing of floating point value (eg. printf("%f", floatnum);)
I have done this:
However, this is not accurate enough. If I try to print 3.1415, it should print as 3.141500, instead it prints 3.141499. I would like this to be as accurate as the real printf.
I thought that I may be able to get the double and break it into it's compenents (sign, exponent and mantissa), but I don't think I can get the bits out of a double.
I don't want someone to do the work for me, but I do need a point in the right direction.
Thanks for your help.
Mostly, I can get this to work, but I am stuck trying to figure out how to handle the printing of floating point value (eg. printf("%f", floatnum);)
I have done this:
Code: Select all
fnum = va_arg(arglist, double);
printf("%d", (int)fnum); /* Print the integer part, truncate the fraction */
putchar('.'); /* Decimal point */
if(fnum<0) /* If negative, remove the sign */
fnum = -fnum;
fnum = (fnum - (int)fnum) * 1000000; /* Get numbers after decimal point */
printf("%d", (int)fnum);
I thought that I may be able to get the double and break it into it's compenents (sign, exponent and mantissa), but I don't think I can get the bits out of a double.
I don't want someone to do the work for me, but I do need a point in the right direction.
Thanks for your help.