Blob


1 /*
2 * The authors of this software are Rob Pike and Ken Thompson.
3 * Copyright (c) 2002 by Lucent Technologies.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose without fee is hereby granted, provided that this entire notice
6 * is included in all copies of any software which is or includes a copy
7 * or modification of this software and in all copies of the supporting
8 * documentation for such software.
9 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
11 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13 */
14 #include <stdio.h>
15 #include <math.h>
16 #include <float.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22 #include <fmt.h>
23 #include "plan9.h"
24 #include "fmt.h"
25 #include "fmtdef.h"
27 enum
28 {
29 FDIGIT = 30,
30 FDEFLT = 6,
31 NSIGNIF = 17
32 };
34 /*
35 * first few powers of 10, enough for about 1/2 of the
36 * total space for doubles.
37 */
38 static double pows10[] =
39 {
40 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
41 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
42 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
43 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
44 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
45 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
46 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
47 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
48 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
49 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
50 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
51 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
52 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
53 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
54 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
55 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
56 };
58 static double
59 pow10(int n)
60 {
61 double d;
62 int neg;
64 neg = 0;
65 if(n < 0){
66 if(n < DBL_MIN_10_EXP){
67 return 0.;
68 }
69 neg = 1;
70 n = -n;
71 }else if(n > DBL_MAX_10_EXP){
72 return HUGE_VAL;
73 }
74 if(n < (int)(sizeof(pows10)/sizeof(pows10[0])))
75 d = pows10[n];
76 else{
77 d = pows10[sizeof(pows10)/sizeof(pows10[0]) - 1];
78 for(;;){
79 n -= sizeof(pows10)/sizeof(pows10[0]) - 1;
80 if(n < (int)(sizeof(pows10)/sizeof(pows10[0]))){
81 d *= pows10[n];
82 break;
83 }
84 d *= pows10[sizeof(pows10)/sizeof(pows10[0]) - 1];
85 }
86 }
87 if(neg){
88 return 1./d;
89 }
90 return d;
91 }
93 static int
94 xadd(char *a, int n, int v)
95 {
96 char *b;
97 int c;
99 if(n < 0 || n >= NSIGNIF)
100 return 0;
101 for(b = a+n; b >= a; b--) {
102 c = *b + v;
103 if(c <= '9') {
104 *b = c;
105 return 0;
107 *b = '0';
108 v = 1;
110 *a = '1'; /* overflow adding */
111 return 1;
114 static int
115 xsub(char *a, int n, int v)
117 char *b;
118 int c;
120 for(b = a+n; b >= a; b--) {
121 c = *b - v;
122 if(c >= '0') {
123 *b = c;
124 return 0;
126 *b = '9';
127 v = 1;
129 *a = '9'; /* underflow subtracting */
130 return 1;
133 static void
134 xdtoa(Fmt *fmt, char *s2, double f)
136 char s1[NSIGNIF+10];
137 double g, h;
138 int e, d, i, n;
139 int c1, c2, c3, c4, ucase, sign, chr, prec;
141 prec = FDEFLT;
142 if(fmt->flags & FmtPrec)
143 prec = fmt->prec;
144 if(prec > FDIGIT)
145 prec = FDIGIT;
146 if(__isNaN(f)) {
147 strcpy(s2, "NaN");
148 return;
150 if(__isInf(f, 1)) {
151 strcpy(s2, "+Inf");
152 return;
154 if(__isInf(f, -1)) {
155 strcpy(s2, "-Inf");
156 return;
158 sign = 0;
159 if(f < 0) {
160 f = -f;
161 sign++;
163 ucase = 0;
164 chr = fmt->r;
165 if(isupper(chr)) {
166 ucase = 1;
167 chr = tolower(chr);
170 e = 0;
171 g = f;
172 if(g != 0) {
173 frexp(f, &e);
174 e = e * .301029995664;
175 if(e >= -150 && e <= +150) {
176 d = 0;
177 h = f;
178 } else {
179 d = e/2;
180 h = f * pow10(-d);
182 g = h * pow10(d-e);
183 while(g < 1) {
184 e--;
185 g = h * pow10(d-e);
187 while(g >= 10) {
188 e++;
189 g = h * pow10(d-e);
193 /*
194 * convert NSIGNIF digits and convert
195 * back to get accuracy.
196 */
197 for(i=0; i<NSIGNIF; i++) {
198 d = g;
199 s1[i] = d + '0';
200 g = (g - d) * 10;
202 s1[i] = 0;
204 /*
205 * try decimal rounding to eliminate 9s
206 */
207 c2 = prec + 1;
208 if(chr == 'f')
209 c2 += e;
210 if(c2 >= NSIGNIF-2) {
211 strcpy(s2, s1);
212 d = e;
213 s1[NSIGNIF-2] = '0';
214 s1[NSIGNIF-1] = '0';
215 sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
216 g = strtod(s1, nil);
217 if(g == f)
218 goto found;
219 if(xadd(s1, NSIGNIF-3, 1)) {
220 e++;
221 sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
223 g = strtod(s1, nil);
224 if(g == f)
225 goto found;
226 strcpy(s1, s2);
227 e = d;
230 /*
231 * convert back so s1 gets exact answer
232 */
233 for(;;) {
234 sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
235 g = strtod(s1, nil);
236 if(f > g) {
237 if(xadd(s1, NSIGNIF-1, 1))
238 e--;
239 continue;
241 if(f < g) {
242 if(xsub(s1, NSIGNIF-1, 1))
243 e++;
244 continue;
246 break;
249 found:
250 /*
251 * sign
252 */
253 d = 0;
254 i = 0;
255 if(sign)
256 s2[d++] = '-';
257 else if(fmt->flags & FmtSign)
258 s2[d++] = '+';
259 else if(fmt->flags & FmtSpace)
260 s2[d++] = ' ';
262 /*
263 * copy into final place
264 * c1 digits of leading '0'
265 * c2 digits from conversion
266 * c3 digits of trailing '0'
267 * c4 digits after '.'
268 */
269 c1 = 0;
270 c2 = prec + 1;
271 c3 = 0;
272 c4 = prec;
273 switch(chr) {
274 default:
275 if(xadd(s1, c2, 5))
276 e++;
277 break;
278 case 'g':
279 /*
280 * decide on 'e' of 'f' style convers
281 */
282 if(xadd(s1, c2, 5))
283 e++;
284 if(e >= -5 && e <= prec) {
285 c1 = -e - 1;
286 c4 = prec - e;
287 chr = 'h'; // flag for 'f' style
289 break;
290 case 'f':
291 if(xadd(s1, c2+e, 5))
292 e++;
293 c1 = -e;
294 if(c1 > prec)
295 c1 = c2;
296 c2 += e;
297 break;
300 /*
301 * clean up c1 c2 and c3
302 */
303 if(c1 < 0)
304 c1 = 0;
305 if(c2 < 0)
306 c2 = 0;
307 if(c2 > NSIGNIF) {
308 c3 = c2-NSIGNIF;
309 c2 = NSIGNIF;
312 /*
313 * copy digits
314 */
315 while(c1 > 0) {
316 if(c1+c2+c3 == c4)
317 s2[d++] = '.';
318 s2[d++] = '0';
319 c1--;
321 while(c2 > 0) {
322 if(c2+c3 == c4)
323 s2[d++] = '.';
324 s2[d++] = s1[i++];
325 c2--;
327 while(c3 > 0) {
328 if(c3 == c4)
329 s2[d++] = '.';
330 s2[d++] = '0';
331 c3--;
334 /*
335 * strip trailing '0' on g conv
336 */
337 if(fmt->flags & FmtSharp) {
338 if(0 == c4)
339 s2[d++] = '.';
340 } else
341 if(chr == 'g' || chr == 'h') {
342 for(n=d-1; n>=0; n--)
343 if(s2[n] != '0')
344 break;
345 for(i=n; i>=0; i--)
346 if(s2[i] == '.') {
347 d = n;
348 if(i != n)
349 d++;
350 break;
353 if(chr == 'e' || chr == 'g') {
354 if(ucase)
355 s2[d++] = 'E';
356 else
357 s2[d++] = 'e';
358 c1 = e;
359 if(c1 < 0) {
360 s2[d++] = '-';
361 c1 = -c1;
362 } else
363 s2[d++] = '+';
364 if(c1 >= 100) {
365 s2[d++] = c1/100 + '0';
366 c1 = c1%100;
368 s2[d++] = c1/10 + '0';
369 s2[d++] = c1%10 + '0';
371 s2[d] = 0;
374 static int
375 floatfmt(Fmt *fmt, double f)
377 char s[FDIGIT+10];
379 xdtoa(fmt, s, f);
380 fmt->flags &= FmtWidth|FmtLeft;
381 __fmtcpy(fmt, s, strlen(s), strlen(s));
382 return 0;
385 int
386 __efgfmt(Fmt *f)
388 double d;
390 d = va_arg(f->args, double);
391 return floatfmt(f, d);