1 /*  **************************************************************************
2  *
3  * --- File: output.c
4  *
5  * --- Purpose: this file provides the functions output(), which outputs
6  *     the results of an expression, and my_strtod() that extends
7  *     strtod() to handle numbers written in base 2, 8, and 16.
8  *
9  * --- Copyright (C) Guido Gonzato, guido@ibogeo.df.unibo.it
10  *
11  * --- Last updated: 4 February 1999
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * ************************************************************************ */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include "gexpr.h"
35 
36 #define EPSILON 1e-38
37 
38 int output_base;
39 int decimal_pos = 10;
40 
41 static int maxpower(double, int);
42 static double topower(double, int);
43 static char *dtostr(double, char *, int);
44 
45 /* ----- */
46 
output(double f)47 void output(double f)
48 {
49   char s[200];
50 
51   dtostr(f, (char *) s, output_base);
52   puts(s);
53 
54 } /* output() */
55 
56 /* ----- */
57 
58 static char *exadigits = "0123456789abcdef";
59 
maxpower(double n,int base)60 static int maxpower(double n, int base)
61 /* return the max. power of n in base `base' */
62 {
63   int max = 0;
64 
65   while ( (n = floor(n / base)) > 0.0)
66     max++;
67 
68   return max;
69 
70 } /* maxpower() */
71 
72 /* ----- */
73 
topower(double n,int power)74 static double topower(double n, int power)
75 /* return n raised to power */
76 {
77   double tmp;
78   int sgn = (power < 0 ? -1 : 1);
79 
80   if (power == 0)
81     return(1);
82 
83   if (sgn == -1) {
84     tmp = 1. / n;
85     power = -power;
86   }
87   else
88     tmp = n;
89 
90   while (--power)
91     if (sgn == 1)
92       tmp *= n;
93     else
94       tmp /= n;
95 
96   return tmp;
97 
98 } /* topower() */
99 
100 /* ----- */
101 
dtostr(double n,char * s,int base)102 char *dtostr(double n, char *s, int base)
103 /* return the number n written in base 'base' as string */
104 {
105   int pos = 0, sign, digit, i;
106   double power, remainder, decimals, divisor, d_divisor, d_remainder;
107 
108   if (base < 2 || base > 16) {
109     s[0] = '\0';
110     return(s);
111   }
112 
113   sign = (n < 0 ? -1: 1);
114   if (sign == -1) {
115     n = -n;
116     s[pos++] = '-';
117   }
118 
119   /* now, do the integer part of the number */
120 
121   power = maxpower(n, base);
122   divisor = topower(base, power);
123   decimals = n - floor(n);
124 
125   do {
126     digit = (int) (floor(n / divisor));
127     remainder = (floor(n - digit * divisor));
128     s[pos++] = exadigits[digit];
129     n = (double) remainder;
130     divisor /= base;
131   } while (divisor >= 1);
132 
133   /* if there is a fractional part, do it */
134 
135   if (decimals > EPSILON) {
136 
137     i = 0;
138     d_divisor = topower(base, -1);
139     n = decimals;
140 
141     if (decimal_pos != 0) {
142       s[pos++] = '.';
143       do {
144         digit = (int) (floor(n / d_divisor));
145 	d_remainder = n - digit * d_divisor;
146         s[pos++] = exadigits[digit];
147         n = d_remainder;
148         d_divisor /= base;
149         i++;
150       } while ( (i < decimal_pos) && (d_remainder > EPSILON));
151     }
152 
153   } /* if decimals */
154 
155   s[pos] = '\0';
156   return s;
157 
158 } /* dtostr() */
159 
160 /* ----- */
161 
my_strtod(const char * s,char ** errptr)162 double my_strtod(const char *s, char **errptr)
163 {
164   double d = 0.0, fract = 0.0;
165   int b, base = 10, i, digit;
166   BOOL do_decimals = FALSE;
167 
168   /* check what base the number is written in */
169 
170   if (s[0] == '0')
171     switch (s[1]) {
172      case 'b': base = 2;
173       break;
174      case 'x': base = 16;
175       break;
176      case '.': base = 10;
177       break;
178      default: base = 8;
179     }
180 
181   /* now convert the number using the chosen base */
182 
183   if (base == 10)
184     d = strtod(s, errptr);
185   else {
186 
187     i = (base == 8 ? 1: 2); /* start from adequate digit */
188     b = base;
189     for (; s[i] != '\0'; i++) {
190 
191       if (s[i] == '.')
192 	do_decimals = TRUE;
193       else {
194 	if (isdigit(s[i]))
195 	  digit = s[i] - '0';
196 	else
197 	  digit = s[i] - 'a' + 10;
198 	if (digit > base - 1) { /* error! */
199 	  *errptr = (char *) &s[i];
200 	  return 0;
201 	}
202 	if (! do_decimals) /* do the integer part */
203 	  d = d * base + digit;
204 	else {
205 	  fract += (double) digit / (double) b;
206 	  b *= base;
207 	}
208       } /* else */
209 
210     } /* for */
211 
212     d = d + fract;
213     *errptr = (char *) &s[i];
214 
215   } /* else */
216 
217   return d;
218 
219 } /* my_strtod() */
220 
221 /* ----- */
222 
223 /* --- End of file output.c --- */
224