]>
Commit | Line | Data |
---|---|---|
19361cb7 | 1 | /* Compatibility functions for floating point formatting, reentrant versions. |
1420df2c | 2 | Copyright (C) 1995,1996,1997,1998,1999,2000,2001 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 | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
19361cb7 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
19361cb7 | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
60478656 RM |
19 | |
20 | #include <errno.h> | |
1420df2c | 21 | #include <float.h> |
60478656 RM |
22 | #include <stdio.h> |
23 | #include <string.h> | |
24 | #include <ctype.h> | |
25 | #include <math.h> | |
1d8dc429 | 26 | #include <stdlib.h> |
da2d1bc5 | 27 | #include <sys/param.h> |
60478656 | 28 | |
2064087b | 29 | #ifndef FLOAT_TYPE |
67994d6f UD |
30 | # define FLOAT_TYPE double |
31 | # define FUNC_PREFIX | |
32 | # define FLOAT_FMT_FLAG | |
33 | # define FLOAT_NAME_EXT | |
b113c12c UD |
34 | # if DBL_MANT_DIG == 53 |
35 | # define NDIGIT_MAX 17 | |
f9a2a636 UD |
36 | # elif DBL_MANT_DIG == 24 |
37 | # define NDIGIT_MAX 9 | |
38 | # elif DBL_MANT_DIG == 56 | |
39 | # define NDIGIT_MAX 18 | |
b113c12c | 40 | # else |
f9a2a636 UD |
41 | /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a |
42 | compile time constant here, so we cannot use it. */ | |
43 | # error "NDIGIT_MAX must be precomputed" | |
b113c12c UD |
44 | # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0))) |
45 | # endif | |
2064087b RM |
46 | #endif |
47 | ||
48 | #define APPEND(a, b) APPEND2 (a, b) | |
49 | #define APPEND2(a, b) a##b | |
50 | ||
51 | #define FLOOR APPEND(floor, FLOAT_NAME_EXT) | |
52 | #define FABS APPEND(fabs, FLOAT_NAME_EXT) | |
53 | #define LOG10 APPEND(log10, FLOAT_NAME_EXT) | |
1f205a47 | 54 | #define EXP APPEND(exp, FLOAT_NAME_EXT) |
2064087b RM |
55 | |
56 | ||
60478656 | 57 | int |
2064087b RM |
58 | APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) |
59 | FLOAT_TYPE value; | |
60478656 RM |
60 | int ndigit, *decpt, *sign; |
61 | char *buf; | |
c2216480 | 62 | size_t len; |
60478656 | 63 | { |
2a068d20 UD |
64 | ssize_t n; |
65 | ssize_t i; | |
1eb687d0 | 66 | int left; |
60478656 RM |
67 | |
68 | if (buf == NULL) | |
69 | { | |
c4029823 | 70 | __set_errno (EINVAL); |
60478656 RM |
71 | return -1; |
72 | } | |
73 | ||
1eb687d0 | 74 | left = 0; |
1f205a47 UD |
75 | if (isfinite (value)) |
76 | { | |
77 | *sign = signbit (value) != 0; | |
78 | if (*sign) | |
79 | value = -value; | |
da2d1bc5 UD |
80 | |
81 | if (ndigit < 0) | |
82 | { | |
83 | /* Rounding to the left of the decimal point. */ | |
1eb687d0 UD |
84 | while (ndigit < 0) |
85 | { | |
86 | FLOAT_TYPE new_value = value * 0.1; | |
87 | ||
88 | if (new_value < 1.0) | |
89 | { | |
90 | ndigit = 0; | |
91 | break; | |
92 | } | |
da2d1bc5 | 93 | |
1eb687d0 UD |
94 | value = new_value; |
95 | ++left; | |
96 | ++ndigit; | |
97 | } | |
da2d1bc5 | 98 | } |
1f205a47 | 99 | } |
07b51ba5 UD |
100 | else |
101 | /* Value is Inf or NaN. */ | |
102 | *sign = 0; | |
103 | ||
67994d6f UD |
104 | n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX), |
105 | value); | |
b9d3d9f7 | 106 | /* Check for a too small buffer. */ |
2a068d20 | 107 | if (n >= (ssize_t) len) |
60478656 RM |
108 | return -1; |
109 | ||
110 | i = 0; | |
111 | while (i < n && isdigit (buf[i])) | |
112 | ++i; | |
113 | *decpt = i; | |
1f205a47 UD |
114 | |
115 | if (i == 0) | |
07b51ba5 UD |
116 | /* Value is Inf or NaN. */ |
117 | return 0; | |
1f205a47 UD |
118 | |
119 | if (i < n) | |
120 | { | |
121 | do | |
122 | ++i; | |
123 | while (i < n && !isdigit (buf[i])); | |
da2d1bc5 | 124 | |
b8ce6e3e | 125 | if (*decpt == 1 && buf[0] == '0' && value != 0.0) |
da2d1bc5 UD |
126 | { |
127 | /* We must not have leading zeroes. Strip them all out and | |
128 | adjust *DECPT if necessary. */ | |
129 | --*decpt; | |
130 | while (i < n && buf[i] == '0') | |
131 | { | |
132 | --*decpt; | |
133 | ++i; | |
134 | } | |
135 | } | |
136 | ||
137 | memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i); | |
138 | buf[n - (i - MAX (*decpt, 0))] = '\0'; | |
1f205a47 | 139 | } |
60478656 | 140 | |
1eb687d0 UD |
141 | if (left) |
142 | { | |
143 | *decpt += left; | |
144 | if (--len > n) | |
145 | { | |
146 | while (left-- > 0 && n < len) | |
147 | buf[n++] = '0'; | |
148 | buf[n] = '\0'; | |
149 | } | |
150 | } | |
151 | ||
60478656 RM |
152 | return 0; |
153 | } | |
154 | ||
155 | int | |
2064087b RM |
156 | APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len) |
157 | FLOAT_TYPE value; | |
60478656 RM |
158 | int ndigit, *decpt, *sign; |
159 | char *buf; | |
c2216480 | 160 | size_t len; |
60478656 | 161 | { |
1f205a47 | 162 | int exponent = 0; |
0676b5fd | 163 | |
1f205a47 | 164 | if (isfinite (value) && value != 0.0) |
4d585333 | 165 | { |
ff8ac383 UD |
166 | /* Slow code that doesn't require -lm functions. */ |
167 | FLOAT_TYPE d; | |
168 | FLOAT_TYPE f = 1.0; | |
169 | if (value < 0.0) | |
170 | d = -value; | |
1f205a47 | 171 | else |
ff8ac383 UD |
172 | d = value; |
173 | if (d < 1.0) | |
1f205a47 | 174 | { |
ff8ac383 | 175 | do |
1f205a47 | 176 | { |
ff8ac383 UD |
177 | f *= 10.0; |
178 | --exponent; | |
1f205a47 | 179 | } |
ff8ac383 UD |
180 | while (d * f < 1.0); |
181 | ||
182 | value *= f; | |
183 | } | |
184 | else if (d >= 10.0) | |
185 | { | |
186 | do | |
1f205a47 | 187 | { |
ff8ac383 UD |
188 | f *= 10; |
189 | ++exponent; | |
1f205a47 | 190 | } |
ff8ac383 UD |
191 | while (d >= f * 10.0); |
192 | ||
193 | value /= f; | |
1f205a47 | 194 | } |
4d585333 | 195 | } |
da2d1bc5 UD |
196 | else if (value == 0.0) |
197 | /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0. | |
198 | This could be changed to -1 if we want to return 0. */ | |
199 | exponent = 0; | |
200 | ||
201 | if (ndigit <= 0 && len > 0) | |
202 | { | |
203 | buf[0] = '\0'; | |
204 | *decpt = 1; | |
205 | *sign = isfinite (value) ? signbit (value) != 0 : 0; | |
206 | } | |
207 | else | |
e22206f3 UD |
208 | if (APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1, |
209 | decpt, sign, buf, len)) | |
da2d1bc5 | 210 | return -1; |
4d585333 | 211 | |
1f205a47 UD |
212 | *decpt += exponent; |
213 | return 0; | |
60478656 | 214 | } |