Annotation of researchv10no/cmd/f2c/cds.c, revision 1.1.1.1

1.1       root        1: /****************************************************************
                      2: Copyright 1990 by AT&T Bell Laboratories and Bellcore.
                      3: 
                      4: Permission to use, copy, modify, and distribute this software
                      5: and its documentation for any purpose and without fee is hereby
                      6: granted, provided that the above copyright notice appear in all
                      7: copies and that both that the copyright notice and this
                      8: permission notice and warranty disclaimer appear in supporting
                      9: documentation, and that the names of AT&T Bell Laboratories or
                     10: Bellcore or any of their entities not be used in advertising or
                     11: publicity pertaining to distribution of the software without
                     12: specific, written prior permission.
                     13: 
                     14: AT&T and Bellcore disclaim all warranties with regard to this
                     15: software, including all implied warranties of merchantability
                     16: and fitness.  In no event shall AT&T or Bellcore be liable for
                     17: any special, indirect or consequential damages or any damages
                     18: whatsoever resulting from loss of use, data or profits, whether
                     19: in an action of contract, negligence or other tortious action,
                     20: arising out of or in connection with the use or performance of
                     21: this software.
                     22: ****************************************************************/
                     23: 
                     24: /* Put strings representing decimal floating-point numbers
                     25:  * into canonical form: always have a decimal point or
                     26:  * exponent field; if using an exponent field, have the
                     27:  * number before it start with a digit and decimal point
                     28:  * (if the number has more than one digit); only have an
                     29:  * exponent field if it saves space.
                     30:  *
                     31:  * Arrange that the return value, rv, satisfies rv[0] == '-' || rv[-1] == '-' .
                     32:  */
                     33: 
                     34: #include "sysdep.h"
                     35: 
                     36:  char *
                     37: cds(s, z0)
                     38:  char *s, *z0;
                     39: {
                     40:        int ea, esign, et, i, k, nd = 0, sign = 0, tz;
                     41:        char c, *z;
                     42:        char ebuf[24];
                     43:        long ex = 0;
                     44:        static char etype[Table_size], *db;
                     45:        static int dblen = 64;
                     46: 
                     47:        if (!db) {
                     48:                etype['E'] = 1;
                     49:                etype['e'] = 1;
                     50:                etype['D'] = 1;
                     51:                etype['d'] = 1;
                     52:                etype['+'] = 2;
                     53:                etype['-'] = 3;
                     54:                db = Alloc(dblen);
                     55:                }
                     56: 
                     57:        while((c = *s++) == '0');
                     58:        if (c == '-')
                     59:                { sign = 1; c = *s++; }
                     60:        else if (c == '+')
                     61:                c = *s++;
                     62:        k = strlen(s) + 2;
                     63:        if (k >= dblen) {
                     64:                do dblen <<= 1;
                     65:                        while(k >= dblen);
                     66:                free(db);
                     67:                db = Alloc(dblen);
                     68:                }
                     69:        if (etype[(unsigned char)c] >= 2)
                     70:                while(c == '0') c = *s++;
                     71:        tz = 0;
                     72:        while(c >= '0' && c <= '9') {
                     73:                if (c == '0')
                     74:                        tz++;
                     75:                else {
                     76:                        if (nd)
                     77:                                for(; tz; --tz)
                     78:                                        db[nd++] = '0';
                     79:                        else
                     80:                                tz = 0;
                     81:                        db[nd++] = c;
                     82:                        }
                     83:                c = *s++;
                     84:                }
                     85:        ea = -tz;
                     86:        if (c == '.') {
                     87:                while((c = *s++) >= '0' && c <= '9') {
                     88:                        if (c == '0')
                     89:                                tz++;
                     90:                        else {
                     91:                                if (tz) {
                     92:                                        ea += tz;
                     93:                                        if (nd)
                     94:                                                for(; tz; --tz)
                     95:                                                        db[nd++] = '0';
                     96:                                        else
                     97:                                                tz = 0;
                     98:                                        }
                     99:                                db[nd++] = c;
                    100:                                ea++;
                    101:                                }
                    102:                        }
                    103:                }
                    104:        if (et = etype[(unsigned char)c]) {
                    105:                esign = et == 3;
                    106:                c = *s++;
                    107:                if (et == 1) {
                    108:                        if(etype[(unsigned char)c] > 1) {
                    109:                                if (c == '-')
                    110:                                        esign = 1;
                    111:                                c = *s++;
                    112:                                }
                    113:                        }
                    114:                while(c >= '0' && c <= '9') {
                    115:                        ex = 10*ex + (c - '0');
                    116:                        c = *s++;
                    117:                        }
                    118:                if (esign)
                    119:                        ex = -ex;
                    120:                }
                    121:        /* debug */ if (c)
                    122:        /* debug*/      Fatal("unexpected character in cds");
                    123:        ex -= ea;
                    124:        if (!nd) {
                    125:                if (!z0)
                    126:                        z0 = mem(4,0);
                    127:                strcpy(z0, "-0.");
                    128:                sign = 0;
                    129:                }
                    130:        else if (ex > 2 || ex + nd < -2) {
                    131:                sprintf(ebuf, "%ld", ex + nd - 1);
                    132:                k = strlen(ebuf) + nd + 3;
                    133:                if (nd > 1)
                    134:                        k++;
                    135:                if (!z0)
                    136:                        z0 = mem(k,0);
                    137:                z = z0;
                    138:                *z++ = '-';
                    139:                *z++ = *db;
                    140:                if (nd > 1) {
                    141:                        *z++ = '.';
                    142:                        for(k = 1; k < nd; k++)
                    143:                                *z++ = db[k];
                    144:                        }
                    145:                *z++ = 'e';
                    146:                strcpy(z, ebuf);
                    147:                }
                    148:        else {
                    149:                k = (int)(ex + nd);
                    150:                i = nd + 3;
                    151:                if (k < 0)
                    152:                        i -= k;
                    153:                else if (ex > 0)
                    154:                        i += ex;
                    155:                if (!z0)
                    156:                        z0 = mem(i,0);
                    157:                z = z0;
                    158:                *z++ = '-';
                    159:                if (ex >= 0) {
                    160:                        for(k = 0; k < nd; k++)
                    161:                                *z++ = db[k];
                    162:                        while(--ex >= 0)
                    163:                                *z++ = '0';
                    164:                        *z++ = '.';
                    165:                        }
                    166:                else {
                    167:                        for(i = 0; i < k;)
                    168:                                *z++ = db[i++];
                    169:                        *z++ = '.';
                    170:                        while(++k <= 0)
                    171:                                *z++ = '0';
                    172:                        while(i < nd)
                    173:                                *z++ = db[i++];
                    174:                        }
                    175:                *z = 0;
                    176:                }
                    177:        return sign ? z0 : z0+1;
                    178:        }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.