]>
Commit | Line | Data |
---|---|---|
19361cb7 | 1 | /* Compatibility functions for floating point formatting, reentrant versions. |
07b51ba5 | 2 | Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
19361cb7 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
60478656 RM |
19 | |
20 | #include <errno.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <ctype.h> | |
24 | #include <math.h> | |
1d8dc429 | 25 | #include <stdlib.h> |
da2d1bc5 | 26 | #include <sys/param.h> |
60478656 | 27 | |
2064087b RM |
28 | #ifndef FLOAT_TYPE |
29 | #define FLOAT_TYPE double | |
30 | #define FUNC_PREFIX | |
31 | #define FLOAT_FMT_FLAG | |
32 | #define FLOAT_NAME_EXT | |
33 | #endif | |
34 | ||
35 | #define APPEND(a, b) APPEND2 (a, b) | |
36 | #define APPEND2(a, b) a##b | |
37 | ||
38 | #define FLOOR APPEND(floor, FLOAT_NAME_EXT) | |
39 | #define FABS APPEND(fabs, FLOAT_NAME_EXT) | |
40 | #define LOG10 APPEND(log10, FLOAT_NAME_EXT) | |
1f205a47 | 41 | #define EXP APPEND(exp, FLOAT_NAME_EXT) |
2064087b RM |
42 | |
43 | ||
60478656 | 44 | int |
2064087b RM |
45 | APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) |
46 | FLOAT_TYPE value; | |
60478656 RM |
47 | int ndigit, *decpt, *sign; |
48 | char *buf; | |
c2216480 | 49 | size_t len; |
60478656 RM |
50 | { |
51 | int n, i; | |
52 | ||
53 | if (buf == NULL) | |
54 | { | |
c4029823 | 55 | __set_errno (EINVAL); |
60478656 RM |
56 | return -1; |
57 | } | |
58 | ||
1f205a47 UD |
59 | if (isfinite (value)) |
60 | { | |
61 | *sign = signbit (value) != 0; | |
62 | if (*sign) | |
63 | value = -value; | |
da2d1bc5 UD |
64 | |
65 | if (ndigit < 0) | |
66 | { | |
67 | /* Rounding to the left of the decimal point. */ | |
68 | for (i = ndigit; i < 0; i++) | |
69 | value *= 0.1; | |
70 | ||
71 | ndigit = 0; | |
72 | } | |
1f205a47 | 73 | } |
07b51ba5 UD |
74 | else |
75 | /* Value is Inf or NaN. */ | |
76 | *sign = 0; | |
77 | ||
2064087b | 78 | n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value); |
60478656 RM |
79 | if (n < 0) |
80 | return -1; | |
81 | ||
82 | i = 0; | |
83 | while (i < n && isdigit (buf[i])) | |
84 | ++i; | |
85 | *decpt = i; | |
1f205a47 UD |
86 | |
87 | if (i == 0) | |
07b51ba5 UD |
88 | /* Value is Inf or NaN. */ |
89 | return 0; | |
1f205a47 UD |
90 | |
91 | if (i < n) | |
92 | { | |
93 | do | |
94 | ++i; | |
95 | while (i < n && !isdigit (buf[i])); | |
da2d1bc5 UD |
96 | |
97 | if (*decpt == 1 && buf[0] == '0') | |
98 | { | |
99 | /* We must not have leading zeroes. Strip them all out and | |
100 | adjust *DECPT if necessary. */ | |
101 | --*decpt; | |
102 | while (i < n && buf[i] == '0') | |
103 | { | |
104 | --*decpt; | |
105 | ++i; | |
106 | } | |
107 | } | |
108 | ||
109 | memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i); | |
110 | buf[n - (i - MAX (*decpt, 0))] = '\0'; | |
1f205a47 | 111 | } |
60478656 RM |
112 | |
113 | return 0; | |
114 | } | |
115 | ||
2064087b RM |
116 | #define weak_extern2(name) weak_extern (name) |
117 | weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS) | |
1f205a47 | 118 | weak_extern2 (EXP) |
4d585333 | 119 | |
60478656 | 120 | int |
2064087b RM |
121 | APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len) |
122 | FLOAT_TYPE value; | |
60478656 RM |
123 | int ndigit, *decpt, *sign; |
124 | char *buf; | |
c2216480 | 125 | size_t len; |
60478656 | 126 | { |
1f205a47 | 127 | int exponent = 0; |
0676b5fd | 128 | |
1f205a47 | 129 | if (isfinite (value) && value != 0.0) |
4d585333 | 130 | { |
1f205a47 UD |
131 | FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10; |
132 | ||
133 | if (log10_function) | |
134 | { | |
135 | /* Use the reasonable code if -lm is included. */ | |
136 | FLOAT_TYPE dexponent; | |
137 | dexponent = FLOOR (LOG10 (FABS (value))); | |
138 | value *= EXP (dexponent * -M_LN10); | |
139 | exponent = (int) dexponent; | |
140 | } | |
141 | else | |
142 | { | |
143 | /* Slow code that doesn't require -lm functions. */ | |
144 | FLOAT_TYPE d; | |
145 | if (value < 0.0) | |
146 | d = -value; | |
147 | else | |
148 | d = value; | |
149 | if (d < 1.0) | |
150 | { | |
151 | do | |
152 | { | |
153 | d *= 10.0; | |
da2d1bc5 | 154 | --exponent; |
1f205a47 UD |
155 | } |
156 | while (d < 1.0); | |
157 | } | |
158 | else if (d >= 10.0) | |
159 | { | |
160 | do | |
161 | { | |
162 | d *= 0.1; | |
da2d1bc5 | 163 | ++exponent; |
1f205a47 UD |
164 | } |
165 | while (d >= 10.0); | |
166 | } | |
167 | if (value < 0.0) | |
168 | value = -d; | |
169 | else | |
170 | value = d; | |
171 | } | |
4d585333 | 172 | } |
da2d1bc5 UD |
173 | else if (value == 0.0) |
174 | /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0. | |
175 | This could be changed to -1 if we want to return 0. */ | |
176 | exponent = 0; | |
177 | ||
178 | if (ndigit <= 0 && len > 0) | |
179 | { | |
180 | buf[0] = '\0'; | |
181 | *decpt = 1; | |
182 | *sign = isfinite (value) ? signbit (value) != 0 : 0; | |
183 | } | |
184 | else | |
185 | if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign, | |
186 | buf, len)) | |
187 | return -1; | |
4d585333 | 188 | |
1f205a47 UD |
189 | *decpt += exponent; |
190 | return 0; | |
60478656 | 191 | } |