From 02020c7458f9cc25bfade2e5f1a32f70d2e0297e Mon Sep 17 00:00:00 2001 From: johanknol Date: Sun, 11 Nov 2001 16:31:50 +0000 Subject: [PATCH] fall back to e format if float is too big git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1567 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- device/lib/vprintf.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/device/lib/vprintf.c b/device/lib/vprintf.c index 555f4f24..ebecea35 100644 --- a/device/lib/vprintf.c +++ b/device/lib/vprintf.c @@ -28,7 +28,7 @@ #warning "this module cannot yet be use as a reentrant one" #endif -#ifdef __ds390 +#if defined(__ds390) #define USE_FLOATS 1 #endif @@ -159,12 +159,23 @@ _endasm; #define DEFAULT_FLOAT_PRECISION 6 +float output_floatE(float f, char decimals) +{ + signed char exp; + char sign = '+'; + + if (f < 0) { f = -f; sign = '-'; } + for (exp = 0; f >= 10.0; exp++) f /=10.0; + for ( ; f < 1.0; exp--) f *=10.0; + printf("%c%d.%d%fe%d\n", sign, decimals+2, decimals, f, exp); +} + static void output_float (float f, unsigned char reqWidth, signed char reqDecimals, bit left, bit zero, bit sign, bit space) { char negative=0; - long integerPart; + unsigned long integerPart; float decimalPart; char fpBuffer[128]; char fpBI=0, fpBD; @@ -176,6 +187,31 @@ static void output_float (float f, unsigned char reqWidth, 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; -- 2.47.2