Annotation of gforth/ecvt.c, revision 1.4

1.4     ! pazsan      1: /* cheap ecvt replacement */
        !             2: 
1.1       pazsan      3: #include <math.h>
                      4: 
                      5: #define MAXCONV 0x40
                      6: char scratch[MAXCONV];
                      7: 
                      8: char* ecvt(double x, int len, int* exp, int* sign)
                      9: {
                     10:    int i, j;
                     11:    double z;
                     12:    
                     13:    if(len > (MAXCONV-1)) len = MAXCONV-1;
                     14:    
                     15:    if(x<0)
                     16:      {
                     17:        *sign = 1;
                     18:        x = -x;
                     19:      }
                     20:    else
                     21:      {
                     22:        *sign = 0;
                     23:      }
1.3       pazsan     24: 
1.1       pazsan     25:    if(x==0)
1.3       pazsan     26:        *exp=-1;
                     27:    else
                     28:      *exp=(int)floor(log10(x));
1.1       pazsan     29:    x = x / pow10((double)*exp);
                     30:    
                     31:    *exp += 1;
                     32:    
                     33:    for(i=0; i < len; i++)
                     34:      {
                     35:        z=floor(x);
                     36:        scratch[i]='0'+(char)((int)z);
                     37:        x = (x-z)*10;
                     38:      }
                     39:    
                     40:    if((x >= 5) && i)
                     41:      {
                     42:        for(j=i-1; j>=0; j--)
                     43:          {
                     44:             if(scratch[j]!='9')
                     45:               {
                     46:                  scratch[j]+=1; break;
                     47:               }
                     48:             else
                     49:               {
                     50:                  scratch[j]='0';
                     51:               }
                     52:          }
                     53:        if(j==0)
                     54:          {
                     55:             scratch[0]='1';
                     56:             *exp += 1;
                     57:          }
                     58:      }
                     59:    
                     60:    scratch[i]='\0';
                     61:    
                     62:    return scratch;
                     63: }
                     64: 
                     65: #ifdef TEST
                     66: int main(int argc, char ** argv)
                     67: {
                     68:    int a, b;
                     69:    char * conv=ecvt(PI*1e10,20,&a,&b);
                     70:    
                     71:    printf("ecvt Test: %f -> %s, %d, %d\n",PI,conv,a,b);
                     72: }
                     73: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>