]> git.ipfire.org Git - thirdparty/glibc.git/blob - misc/efgcvt_r.c
Update.
[thirdparty/glibc.git] / misc / efgcvt_r.c
1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
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. */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <sys/param.h>
27
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)
41 #define EXP APPEND(exp, FLOAT_NAME_EXT)
42
43
44 int
45 APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
46 FLOAT_TYPE value;
47 int ndigit, *decpt, *sign;
48 char *buf;
49 size_t len;
50 {
51 int n, i;
52 int left;
53
54 if (buf == NULL)
55 {
56 __set_errno (EINVAL);
57 return -1;
58 }
59
60 left = 0;
61 if (isfinite (value))
62 {
63 *sign = signbit (value) != 0;
64 if (*sign)
65 value = -value;
66
67 if (ndigit < 0)
68 {
69 /* Rounding to the left of the decimal point. */
70 while (ndigit < 0)
71 {
72 FLOAT_TYPE new_value = value * 0.1;
73
74 if (new_value < 1.0)
75 {
76 ndigit = 0;
77 break;
78 }
79
80 value = new_value;
81 ++left;
82 ++ndigit;
83 }
84 }
85 }
86 else
87 /* Value is Inf or NaN. */
88 *sign = 0;
89
90 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
91 if (n < 0)
92 return -1;
93
94 i = 0;
95 while (i < n && isdigit (buf[i]))
96 ++i;
97 *decpt = i;
98
99 if (i == 0)
100 /* Value is Inf or NaN. */
101 return 0;
102
103 if (i < n)
104 {
105 do
106 ++i;
107 while (i < n && !isdigit (buf[i]));
108
109 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
110 {
111 /* We must not have leading zeroes. Strip them all out and
112 adjust *DECPT if necessary. */
113 --*decpt;
114 while (i < n && buf[i] == '0')
115 {
116 --*decpt;
117 ++i;
118 }
119 }
120
121 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
122 buf[n - (i - MAX (*decpt, 0))] = '\0';
123 }
124
125 if (left)
126 {
127 *decpt += left;
128 if (--len > n)
129 {
130 while (left-- > 0 && n < len)
131 buf[n++] = '0';
132 buf[n] = '\0';
133 }
134 }
135
136 return 0;
137 }
138
139 #define weak_extern2(name) weak_extern (name)
140 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
141 weak_extern2 (EXP)
142
143 int
144 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
145 FLOAT_TYPE value;
146 int ndigit, *decpt, *sign;
147 char *buf;
148 size_t len;
149 {
150 int exponent = 0;
151
152 if (isfinite (value) && value != 0.0)
153 {
154 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
155
156 if (log10_function)
157 {
158 /* Use the reasonable code if -lm is included. */
159 FLOAT_TYPE dexponent;
160 dexponent = FLOOR (LOG10 (FABS (value)));
161 value *= EXP (dexponent * -M_LN10);
162 exponent = (int) dexponent;
163 }
164 else
165 {
166 /* Slow code that doesn't require -lm functions. */
167 FLOAT_TYPE d;
168 if (value < 0.0)
169 d = -value;
170 else
171 d = value;
172 if (d < 1.0)
173 {
174 do
175 {
176 d *= 10.0;
177 --exponent;
178 }
179 while (d < 1.0);
180 }
181 else if (d >= 10.0)
182 {
183 do
184 {
185 d *= 0.1;
186 ++exponent;
187 }
188 while (d >= 10.0);
189 }
190 if (value < 0.0)
191 value = -d;
192 else
193 value = d;
194 }
195 }
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
208 if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign,
209 buf, len))
210 return -1;
211
212 *decpt += exponent;
213 return 0;
214 }