1 /* e format conversion 2 * For generic printf 3 */ 4 5 #include <float.h> 6 #include <math.h> 7 #include <stdio.h> 8 9 /* 10 * These two functions are already listed in z80_crt0.hdr so we 11 * have to do a nasty kludge around them 12 */ 13 14 15 ftoe(x,prec,str)16void ftoe(x,prec,str) 17 double x ; /* number to be converted */ 18 int prec ; /* # digits after decimal place */ 19 char *str ; /* output string */ 20 { 21 double scale; /* scale factor */ 22 int i, /* counter */ 23 d, /* a digit */ 24 expon; /* exponent */ 25 26 scale = 1.0 ; /* scale = 10 ** prec */ 27 i = prec ; 28 while ( i-- ) 29 scale *= 10.0 ; 30 if ( x == 0.0 ) { 31 expon = 0 ; 32 scale *= 10.0 ; 33 } 34 else { 35 expon = prec ; 36 if ( x < 0.0 ) { 37 *str++ = '-' ; 38 x = -x ; 39 } 40 if ( x > scale ) { 41 /* need: scale<x<scale*10 */ 42 scale *= 10.0 ; 43 while ( x >= scale ) { 44 x /= 10.0 ; 45 ++expon ; 46 } 47 } 48 else { 49 while ( x < scale ) { 50 x *= 10.0 ; 51 --expon ; 52 } 53 scale *= 10.0 ; 54 } 55 /* at this point, .1*scale <= x < scale */ 56 x += 0.5 ; /* round */ 57 if ( x >= scale ) { 58 x /= 10.0 ; 59 ++expon ; 60 } 61 } 62 i = 0 ; 63 while ( i <= prec ) { 64 scale = floor( 0.5 + scale * 0.1 ) ; 65 /* now, scale <= x < 10*scale */ 66 d = ( x / scale ) ; 67 *str++ = d + '0' ; 68 x -= (d * scale) ; 69 if ( i++ ) continue ; 70 *str++ = '.' ; 71 } 72 *str++ = 'e' ; 73 if ( expon < 0 ) { *str++ = '-' ; expon = -expon ; } 74 if(expon>9) *str++ = '0' + expon/10 ; 75 *str++ = '0' + expon % 10 ; 76 *str = 0; 77 } 78