Annotation of gforth/engine/ecvt.c, revision 1.4

1.3       anton       1: /* cheap ecvt replacement
                      2: 
                      3:   Copyright (C) 1998 Free Software Foundation, Inc.
                      4: 
                      5:   This file is part of Gforth.
                      6: 
                      7:   Gforth is free software; you can redistribute it and/or
                      8:   modify it under the terms of the GNU General Public License
                      9:   as published by the Free Software Foundation; either version 2
                     10:   of the License, or (at your option) any later version.
                     11: 
                     12:   This program is distributed in the hope that it will be useful,
                     13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15:   GNU General Public License for more details.
                     16: 
                     17:   You should have received a copy of the GNU General Public License
                     18:   along with this program; if not, write to the Free Software
                     19:   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: */
1.1       anton      21: 
1.2       pazsan     22: #include <stdio.h>
1.1       anton      23: #include "config.h"
                     24: #include <math.h>
                     25: extern double floor(double);
                     26: extern double pow10(double);
                     27: 
                     28: #define MAXCONV 0x40
                     29: char scratch[MAXCONV];
                     30: 
                     31: char* ecvt(double x, int len, int* exp, int* sign)
                     32: {
                     33:    int i, j;
                     34:    double z;
                     35:    
                     36:    if(len > (MAXCONV-1)) len = MAXCONV-1;
                     37:    
                     38:    if(x<0)
                     39:      {
                     40:        *sign = 1;
                     41:        x = -x;
                     42:      }
                     43:    else
                     44:      {
                     45:        *sign = 0;
                     46:      }
                     47: 
                     48:    if(x==0)
                     49:        *exp=-1;
                     50:    else
                     51:      *exp=(int)floor(log10(x));
                     52:    x = x / pow10((double)*exp);
                     53:    
                     54:    *exp += 1;
                     55:    
                     56:    for(i=0; i < len; i++)
                     57:      {
                     58:        z=floor(x);
                     59:        scratch[i]='0'+(char)((int)z);
                     60:        x = (x-z)*10;
                     61:      }
                     62:    
                     63:    if((x >= 5) && i)
                     64:      {
                     65:        for(j=i-1; j>=0; j--)
                     66:          {
                     67:             if(scratch[j]!='9')
                     68:               {
                     69:                  scratch[j]+=1; break;
                     70:               }
                     71:             else
                     72:               {
                     73:                  scratch[j]='0';
                     74:               }
                     75:          }
1.2       pazsan     76:        if(j<0)
1.1       anton      77:          {
                     78:             scratch[0]='1';
                     79:             *exp += 1;
                     80:          }
                     81:      }
                     82:    
                     83:    scratch[i]='\0';
                     84:    
                     85:    return scratch;
                     86: }
                     87: 
                     88: #ifdef TEST
                     89: int main(int argc, char ** argv)
                     90: {
                     91:    int a, b;
1.2       pazsan     92:    char * conv=ecvt(9e0,20,&a,&b);
1.1       anton      93:    
1.2       pazsan     94:    printf("ecvt Test: %f -> %s, %d, %d\n",9e0,conv,a,b);
1.1       anton      95: }
                     96: #endif

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