* support/regression/ports/mcs51-xstack-auto/spec.mk: forgot -I(...)/mcs51
[fw/sdcc] / device / lib / printf_large.c
index e0a24ab7eea50816de9b7d10d6217f0eaee76e66..ba43fd546cb52ccbf93a8765cf46e7d3c0b022e5 100644 (file)
@@ -165,7 +165,7 @@ static int output_float (float f, unsigned char reqWidth,
                          BOOL left, BOOL zero, BOOL sign, BOOL space)
 #endif
 {
-  char negative=0;
+  BOOL negative=0;
   unsigned long integerPart;
   float decimalPart;
   char fpBuffer[128];
@@ -211,7 +211,7 @@ static int output_float (float f, unsigned char reqWidth,
 
   // split the float
   integerPart=f;
-  decimalPart=f-(float)integerPart;
+  decimalPart=f-integerPart;
 
   // fill the buffer with the integerPart (in reversed order!)
   while (integerPart) {
@@ -229,14 +229,19 @@ static int output_float (float f, unsigned char reqWidth,
 
   // fill buffer with the decimalPart (in normal order)
   fpBD=fpBI;
-  if (i=reqDecimals /* that's an assignment */) {
-    do {
+
+  for (i=reqDecimals; i>1; i--) {
       decimalPart *= 10.0;
       // truncate the float
       integerPart=decimalPart;
       fpBuffer[fpBD++]='0' + integerPart;
-      decimalPart-=(float)integerPart;
-    } while (--i);
+      decimalPart-=integerPart;
+  }
+  if (i) {
+    decimalPart *= 10.0;
+    // truncate the float
+    integerPart = decimalPart + 0.5;
+    fpBuffer[fpBD++] = '0' + integerPart;
   }
 
   minWidth=fpBI; // we need at least these