update from main archive 960911
[glibc.git] / misc / efgcvt_r.c
blobcbd5c271ba8a22fc554e1847d38674cd7267a708
1 /* [efg]cvt -- compatibility functions for floating point formatting,
2 reentrant versions.
3 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <math.h>
26 #include <stdlib.h>
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
35 #define APPEND(a, b) APPEND2 (a, b)
36 #define APPEND2(a, b) a##b
38 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
39 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
40 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
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;
50 int n, i;
52 if (buf == NULL)
54 errno = EINVAL;
55 return -1;
58 *sign = value < 0.0;
59 if (*sign)
60 value = - value;
62 n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
63 if (n < 0)
64 return -1;
66 i = 0;
67 while (i < n && isdigit (buf[i]))
68 ++i;
69 *decpt = i;
71 ++i;
72 while (! isdigit (buf[i]));
73 memmove (&buf[i - *decpt], buf, n - (i - *decpt));
75 return 0;
78 #define weak_extern2(name) weak_extern (name)
79 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
81 int
82 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
83 FLOAT_TYPE value;
84 int ndigit, *decpt, *sign;
85 char *buf;
86 size_t len;
88 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
90 if (log10_function)
92 /* Use the reasonable code if -lm is included. */
93 ndigit -= (int) FLOOR (LOG10 (FABS (value)));
94 if (ndigit < 0)
95 ndigit = 0;
97 else
99 /* Slow code that doesn't require -lm functions. */
100 FLOAT_TYPE d;
101 for (d = value < 0.0 ? - value : value;
102 ndigit > 0 && d >= 10.0;
103 d *= 0.1)
104 --ndigit;
107 return APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len);