(inet_addr): Change return type to u_int32_t.
[glibc.git] / misc / efgcvt_r.c
blob7ad057fcb727f91ab2151e0fc6048ef4d654cfff
1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <math.h>
25 #include <stdlib.h>
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
34 #define APPEND(a, b) APPEND2 (a, b)
35 #define APPEND2(a, b) a##b
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 #define ISINF APPEND(isinf, FLOAT_NAME_EXT)
42 #define ISNAN APPEND(isnan, FLOAT_NAME_EXT)
45 int
46 APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
47 FLOAT_TYPE value;
48 int ndigit, *decpt, *sign;
49 char *buf;
50 size_t len;
52 int n, i;
54 if (buf == NULL)
56 __set_errno (EINVAL);
57 return -1;
60 if (!ISINF (value) && !ISNAN (value))
62 /* OK, the following is not entirely correct. -0.0 is not handled
63 correctly but glibc 2.0 does not have a portable function to
64 implement this test. */
65 *sign = value < 0.0;
66 if (*sign)
67 value = -value;
70 n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
71 if (n < 0)
72 return -1;
74 i = 0;
75 while (i < n && isdigit (buf[i]))
76 ++i;
77 *decpt = i;
79 if (i == 0)
81 /* Value is Inf or NaN. */
82 *sign = 0;
83 return 0;
86 if (i < n)
89 ++i;
90 while (i < n && !isdigit (buf[i]));
91 memmove (&buf[*decpt], &buf[i], n - i);
92 buf[n - (i - *decpt)] = 0;
95 return 0;
98 #define weak_extern2(name) weak_extern (name)
99 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
100 weak_extern2 (EXP)
103 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
104 FLOAT_TYPE value;
105 int ndigit, *decpt, *sign;
106 char *buf;
107 size_t len;
109 int exponent = 0;
111 if (!ISNAN (value) && !ISINF (value) && value != 0.0)
113 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
115 if (log10_function)
117 /* Use the reasonable code if -lm is included. */
118 FLOAT_TYPE dexponent;
119 dexponent = FLOOR (LOG10 (FABS (value)));
120 value *= EXP (dexponent * -M_LN10);
121 exponent = (int) dexponent;
123 else
125 /* Slow code that doesn't require -lm functions. */
126 FLOAT_TYPE d;
127 if (value < 0.0)
128 d = -value;
129 else
130 d = value;
131 if (d < 1.0)
135 d *= 10.0;
136 exponent--;
138 while (d < 1.0);
140 else if (d >= 10.0)
144 d *= 0.1;
145 exponent++;
147 while (d >= 10.0);
149 if (value < 0.0)
150 value = -d;
151 else
152 value = d;
156 if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign, buf, len))
157 return -1;
158 *decpt += exponent;
159 return 0;