Move the sources to trunk
[opencv] / apps / cvenv / EiC / stdClib / eicfftoa.c
1 /* eicfftoa.c
2  *
3  *      (C) Copyright Dec 29 1995, Edmond J. Breen.
4  *                 ALL RIGHTS RESERVED.
5  * This code may be copied for personal, non-profit use only.
6  *
7  */
8
9
10
11 /*#define DBL_MIN_EXP -100 */
12
13 #include <math.h>
14 #include <limits.h>
15 #include <float.h>
16
17 #include "stdliblocal.h"
18
19 char *fftoa(double d, char *str, int p, char type, int trunc)
20 {
21     /*
22      * Convert a floating-point number into a
23      * formatted 'f','e' or 'E' type string.
24      * (c) Edmond J.Breen, March 1995.
25      * where:  'p'  represents the precision
26      *                i.e. the number of digits
27      *                that will be placed after
28      *                the decimal point.
29      *                A precision of 0 suppresses
30      *                the decimal point.
31      *         'type' is either 'f', 'e' or 'E'.
32      *                'f' ->  [-]mmm.dddd
33      *                'e' ->  [-]m.dddde+xx
34      *                      | [-]m.dddde-xx
35      *                'E' ->  [-]m.ddddE+xx
36      *                      | [-]m.ddddE-xx
37      *         'trunc' if trunc != 0 then trailing
38      *                 zeros will be removed.
39      *                 i.e. 'g' or 'G' format.
40      */
41     double M;
42     int i, j, width, spt, dec, prec;
43     static char *s;
44
45     prec = p;
46     i = 0;
47     if (d < 0)
48         str[0] = '-', s = &str[1], d = -d;
49     else
50         s = str;
51     if (d >= 1) {               /* collect integer part */
52         int k, c;
53         double D = floor(d);
54         d -= D;
55         do {
56             j = fmod(D,10.0);
57             s[i++] = j + '0';
58             D -= j;
59         } while ((D /= 10) >= 1);
60         /* now reverse the string */
61         for (k = i, j = 0, k--; j < k; j++, k--) {
62             c = s[j];
63             s[j] = s[k];
64             s[k] = c;
65         }
66     }
67     dec = i;
68     if (!dec) {                 /* check for numbers less than 1 */
69         if (type != 'f') {      /* assume 'e' or 'E' format */
70             if (d != 0) {       /* d == d might be a safety check? */
71                 while ((d *= 10) < 1 && dec > DBL_MIN_EXP)
72                     --dec;
73                 --dec;
74             }
75             if (d >= 1)
76                 prec++, d /= 10;
77         } else
78             s[i++] = '0';
79     }
80     if (dec < 0)
81         width = prec;
82     else if (type == 'f')
83         width = prec + (dec > 0 ? dec : 1);
84     else
85         width = prec + 1;
86
87     M = DBL_EPSILON/(FLT_RADIX*2.0); /* precision value */
88     while(1) {  /* now collect fraction */
89         d *= 10;
90         j = (int) d;
91         d -= j;
92         M *=10;
93         if((i < width) && (d >= M && d <= (1-M)))
94             s[i++] = (char) j + '0';
95         else
96             break;
97     } 
98     if(d>=0.5)
99         j++;
100     s[i++] = (char) j + '0';
101     while(i<=width)
102         s[i++] = '0';
103     
104     /* Have to round see ANSI176,13.9.5.2 */
105     if (!strround(s, width + 1)) /* watch for over flow */
106         s[0] = '1', width++, dec++;
107
108     if (p != 0) {               /* add in decimal point */
109         if (type == 'f') {
110             if (dec > 0)
111                 spt = dec;
112             else
113                 spt = 1;
114         } else
115             spt = 1;
116         for (i = width; i > spt; i--)
117             s[i] = s[i - 1];
118         s[spt] = '.';
119     } else
120         width--;
121
122     if (trunc) {                /* remove trailing zeros */
123         while (width && s[width] == '0')
124             width--;
125         if (s[width] == '.')
126             width--;
127     }
128     if (type != 'f') {          /* add in exponent */
129         s[++width] = type;
130         if (dec >= 0) {
131             s[++width] = '+';
132             if (dec != 0)
133                 dec = dec - 1;
134             if (dec < 10)
135                 s[++width] = '0';
136         } else if (dec < 0) {
137             s[++width] = '-';
138             if (dec > -10)
139                 s[++width] = '0';
140             dec = -dec;
141         }
142         itoa(dec, &s[++width], 10);
143     } else
144         s[width + 1] = 0;
145     return str;
146 }
147
148
149
150
151
152
153
154