]> git.ipfire.org Git - thirdparty/glibc.git/blame - misc/efgcvt_r.c
update from main archive
[thirdparty/glibc.git] / misc / efgcvt_r.c
CommitLineData
60478656 1/* [efg]cvt -- compatibility functions for floating point formatting,
2064087b 2 reentrant versions.
4d585333 3Copyright (C) 1995, 1996 Free Software Foundation, Inc.
60478656
RM
4This file is part of the GNU C Library.
5
6The GNU C Library is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public License as
8published by the Free Software Foundation; either version 2 of the
9License, or (at your option) any later version.
10
11The GNU C Library is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with the GNU C Library; see the file COPYING.LIB. If
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21#include <errno.h>
22#include <stdio.h>
23#include <string.h>
24#include <ctype.h>
25#include <math.h>
1d8dc429 26#include <stdlib.h>
60478656 27
2064087b
RM
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
42
60478656 43int
2064087b
RM
44APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
45 FLOAT_TYPE value;
60478656
RM
46 int ndigit, *decpt, *sign;
47 char *buf;
c2216480 48 size_t len;
60478656
RM
49{
50 int n, i;
51
52 if (buf == NULL)
53 {
c4029823 54 __set_errno (EINVAL);
60478656
RM
55 return -1;
56 }
57
58 *sign = value < 0.0;
59 if (*sign)
60 value = - value;
61
2064087b 62 n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
60478656
RM
63 if (n < 0)
64 return -1;
65
66 i = 0;
67 while (i < n && isdigit (buf[i]))
68 ++i;
69 *decpt = i;
70 do
71 ++i;
72 while (! isdigit (buf[i]));
73 memmove (&buf[i - *decpt], buf, n - (i - *decpt));
74
75 return 0;
76}
77
2064087b
RM
78#define weak_extern2(name) weak_extern (name)
79weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
4d585333 80
60478656 81int
2064087b
RM
82APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
83 FLOAT_TYPE value;
60478656
RM
84 int ndigit, *decpt, *sign;
85 char *buf;
c2216480 86 size_t len;
60478656 87{
2064087b 88 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
0676b5fd 89
0e3426bb 90 if (log10_function)
4d585333
RM
91 {
92 /* Use the reasonable code if -lm is included. */
2064087b 93 ndigit -= (int) FLOOR (LOG10 (FABS (value)));
4d585333
RM
94 if (ndigit < 0)
95 ndigit = 0;
96 }
97 else
98 {
99 /* Slow code that doesn't require -lm functions. */
2064087b 100 FLOAT_TYPE d;
4d585333
RM
101 for (d = value < 0.0 ? - value : value;
102 ndigit > 0 && d >= 10.0;
103 d *= 0.1)
104 --ndigit;
105 }
106
2064087b 107 return APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len);
60478656 108}