]> 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
27 #ifndef FLOAT_TYPE
28 #define FLOAT_TYPE double
29 #define FUNC_PREFIX
30 #define FLOAT_FMT_FLAG
31 #define FLOAT_NAME_EXT
32 #endif
33
34 #define APPEND(a, b) APPEND2 (a, b)
35 #define APPEND2(a, b) a##b
36
37 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
38 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
39 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
40 #define EXP APPEND(exp, FLOAT_NAME_EXT)
41
42
43 int
44 APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
45 FLOAT_TYPE value;
46 int ndigit, *decpt, *sign;
47 char *buf;
48 size_t len;
49 {
50 int n, i;
51
52 if (buf == NULL)
53 {
54 __set_errno (EINVAL);
55 return -1;
56 }
57
58 if (isfinite (value))
59 {
60 *sign = signbit (value) != 0;
61 if (*sign)
62 value = -value;
63 }
64 else
65 /* Value is Inf or NaN. */
66 *sign = 0;
67
68 if (ndigit <= 0)
69 {
70 if (len > 0)
71 buf[0] = '\0';
72 *decpt = 0;
73 return 0;
74 }
75
76 n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
77 if (n < 0)
78 return -1;
79
80 i = 0;
81 while (i < n && isdigit (buf[i]))
82 ++i;
83 *decpt = i;
84
85 if (i == 0)
86 /* Value is Inf or NaN. */
87 return 0;
88
89 if (i < n)
90 {
91 do
92 ++i;
93 while (i < n && !isdigit (buf[i]));
94 memmove (&buf[*decpt], &buf[i], n - i);
95 buf[n - (i - *decpt)] = '\0';
96 }
97
98 return 0;
99 }
100
101 #define weak_extern2(name) weak_extern (name)
102 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
103 weak_extern2 (EXP)
104
105 int
106 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
107 FLOAT_TYPE value;
108 int ndigit, *decpt, *sign;
109 char *buf;
110 size_t len;
111 {
112 int exponent = 0;
113
114 if (isfinite (value) && value != 0.0)
115 {
116 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
117
118 if (log10_function)
119 {
120 /* Use the reasonable code if -lm is included. */
121 FLOAT_TYPE dexponent;
122 dexponent = FLOOR (LOG10 (FABS (value)));
123 value *= EXP (dexponent * -M_LN10);
124 exponent = (int) dexponent;
125 }
126 else
127 {
128 /* Slow code that doesn't require -lm functions. */
129 FLOAT_TYPE d;
130 if (value < 0.0)
131 d = -value;
132 else
133 d = value;
134 if (d < 1.0)
135 {
136 do
137 {
138 d *= 10.0;
139 exponent--;
140 }
141 while (d < 1.0);
142 }
143 else if (d >= 10.0)
144 {
145 do
146 {
147 d *= 0.1;
148 exponent++;
149 }
150 while (d >= 10.0);
151 }
152 if (value < 0.0)
153 value = -d;
154 else
155 value = d;
156 }
157 }
158
159 if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign, buf, len))
160 return -1;
161 *decpt += exponent;
162 return 0;
163 }