Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (107 sloc) 2.48 kb
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
1 /****************************************************************
2 *
3 * The author of this software is David M. Gay.
4 *
5 * Copyright (c) 1991, 1996 by Lucent Technologies.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
12 *
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17 *
18 ***************************************************************/
19
20 /* g_fmt(buf,x) stores the closest decimal approximation to x in buf;
21 * it suffices to declare buf
22 * char buf[32];
23 */
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 extern char *dtoa(double, int, int, int *, int *, char **);
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
29 extern int g_fmt(char *, double, int);
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
30 extern void freedtoa(char*);
31 #ifdef __cplusplus
32 }
33 #endif
34
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
35 int
36 fpconv_g_fmt(char *b, double x, int precision)
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
37 {
38 register int i, k;
39 register char *s;
40 int decpt, j, sign;
41 char *b0, *s0, *se;
42
43 b0 = b;
44 #ifdef IGNORE_ZERO_SIGN
45 if (!x) {
46 *b++ = '0';
47 *b = 0;
48 goto done;
49 }
50 #endif
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
51 s = s0 = dtoa(x, 2, precision, &decpt, &sign, &se);
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
52 if (sign)
53 *b++ = '-';
54 if (decpt == 9999) /* Infinity or Nan */ {
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
55 while((*b++ = *s++));
56 /* "b" is used to calculate the return length. Decrement to exclude the
57 * Null terminator from the length */
58 b--;
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
59 goto done0;
60 }
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
61 if (decpt <= -4 || decpt > precision) {
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
62 *b++ = *s++;
63 if (*s) {
64 *b++ = '.';
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
65 while((*b = *s++))
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
66 b++;
67 }
68 *b++ = 'e';
69 /* sprintf(b, "%+.2d", decpt - 1); */
70 if (--decpt < 0) {
71 *b++ = '-';
72 decpt = -decpt;
73 }
74 else
75 *b++ = '+';
76 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10);
77 for(;;) {
78 i = decpt / k;
79 *b++ = i + '0';
80 if (--j <= 0)
81 break;
82 decpt -= i*k;
83 decpt *= 10;
84 }
85 *b = 0;
86 }
87 else if (decpt <= 0) {
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
88 *b++ = '0';
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
89 *b++ = '.';
90 for(; decpt < 0; decpt++)
91 *b++ = '0';
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
92 while((*b++ = *s++));
bb296ab @mpx Fix string length returned by g_fmt.c for |x|<1
authored
93 b--;
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
94 }
95 else {
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
96 while((*b = *s++)) {
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
97 b++;
98 if (--decpt == 0 && *s)
99 *b++ = '.';
100 }
101 for(; decpt > 0; decpt--)
102 *b++ = '0';
103 *b = 0;
104 }
105 done0:
106 freedtoa(s0);
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
107 #ifdef IGNORE_ZERO_SIGN
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
108 done:
ca42b9a @mpx Use internal dtoa/strtod for double conversion
authored
109 #endif
110 return b - b0;
23d19ba @mpx Added Netlib dtoa.c/g_fmt.c routines (20110428)
authored
111 }
Something went wrong with that request. Please try again.