+#if USE_FLOATS
+
+/* This is a very inefficient but direct approach, since we have no math
+ library yet (e.g. log()).
+ It does most of the modifiers, but has some restrictions. E.g. the
+ abs(float) shouldn't be bigger than an unsigned long (that's
+ about 4294967295), but still makes it usefull for most real-life
+ applications.
+*/
+
+#define DEFAULT_FLOAT_PRECISION 6
+
+static void output_float (float f, unsigned char reqWidth,
+ signed char reqDecimals,
+ bit left, bit zero, bit sign, bit space)
+{
+ char negative=0;
+ unsigned long integerPart;
+ float decimalPart;
+ char fpBuffer[128];
+ char fpBI=0, fpBD;
+ unsigned char minWidth, i;
+
+ // save the sign
+ if (f<0) {
+ negative=1;
+ f=-f;
+ }
+
+ if (f>0x00ffffff) {
+ // this part is from Frank van der Hulst
+ signed char exp;
+
+ for (exp = 0; f >= 10.0; exp++) f /=10.0;
+ for ( ; f < 1.0; exp--) f *=10.0;
+
+ if (negative) {
+ putchar ('-');
+ } else {
+ if (sign) {
+ putchar ('+');
+ }
+ }
+ output_float(f, 0, reqDecimals, 0, 0, 0, 0);
+ putchar ('e');
+ if (exp<0) {
+ putchar ('-');
+ exp = -exp;
+ }
+ putchar ('0'+exp/10);
+ putchar ('0'+exp%10);
+ return;
+ }
+
+ // split the float
+ integerPart=f;
+ decimalPart=f-integerPart;
+
+ // fill the buffer with the integerPart (in reversed order!)
+ while (integerPart) {
+ fpBuffer[fpBI++]='0' + integerPart%10;
+ integerPart /= 10;
+ }
+ if (!fpBI) {
+ // we need at least a 0
+ fpBuffer[fpBI++]='0';
+ }
+
+ // display some decimals as default
+ if (reqDecimals==-1)
+ reqDecimals=DEFAULT_FLOAT_PRECISION;
+
+ // fill buffer with the decimalPart (in normal order)
+ fpBD=fpBI;
+ if (i=reqDecimals /* that's an assignment */) {
+ do {
+ decimalPart *= 10.0;
+ // truncate the float
+ integerPart=decimalPart;
+ fpBuffer[fpBD++]='0' + integerPart;
+ decimalPart-=integerPart;
+ } while (--i);
+ }
+
+ minWidth=fpBI; // we need at least these
+ minWidth+=reqDecimals?reqDecimals+1:0; // maybe these
+ if (negative || sign || space)
+ minWidth++; // and maybe even this :)
+
+ if (!left && reqWidth>i) {
+ if (zero) {
+ if (negative) output_char('-');
+ else if (sign) output_char('+');
+ else if (space) output_char(' ');
+ while (reqWidth-->minWidth)
+ output_char ('0');
+ } else {
+ while (reqWidth-->minWidth)
+ output_char (' ');
+ if (negative) output_char('-');
+ else if (sign) output_char('+');
+ else if (space) output_char (' ');
+ }
+ } else {
+ if (negative) output_char('-');
+ else if (sign) output_char('+');
+ else if (space) output_char(' ');
+ }
+
+ // output the integer part
+ i=fpBI-1;
+ do {
+ output_char (fpBuffer[i]);
+ } while (i--);
+
+ // ouput the decimal part
+ if (reqDecimals) {
+ output_char ('.');
+ i=fpBI;
+ while (reqDecimals--)
+ output_char (fpBuffer[i++]);
+ }
+
+ if (left && reqWidth>minWidth) {
+ while (reqWidth-->minWidth)
+ output_char(' ');
+ }
+}
+#endif
+