]>
Commit | Line | Data |
---|---|---|
19361cb7 | 1 | /* Compatibility functions for floating point formatting. |
67994d6f | 2 | Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc. |
19361cb7 | 3 | This file is part of the GNU C Library. |
0923c7a5 | 4 | |
19361cb7 UD |
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. | |
0923c7a5 | 9 | |
19361cb7 UD |
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. | |
0923c7a5 | 14 | |
19361cb7 UD |
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. */ | |
0923c7a5 | 19 | |
b113c12c | 20 | #include <math.h> |
0923c7a5 | 21 | #include <stdio.h> |
60478656 | 22 | #include <stdlib.h> |
67994d6f | 23 | #include <sys/param.h> |
52e9a9d1 | 24 | #include <float.h> |
1cab5444 | 25 | #include <bits/libc-lock.h> |
52e9a9d1 | 26 | |
2064087b | 27 | #ifndef FLOAT_TYPE |
1cab5444 UD |
28 | # define FLOAT_TYPE double |
29 | # define FUNC_PREFIX | |
30 | # define FLOAT_FMT_FLAG | |
31 | /* Actually we have to write (DBL_DIG + log10 (DBL_MAX_10_EXP)) but we | |
32 | don't have log10 available in the preprocessor. */ | |
b113c12c UD |
33 | # define MAXDIG (NDIGIT_MAX + 3) |
34 | # if DBL_MANT_DIG == 53 | |
35 | # define NDIGIT_MAX 17 | |
36 | # else | |
37 | /* See IEEE 854 5.6, table 2 for this formula. */ | |
38 | # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0))) | |
39 | # endif | |
2064087b RM |
40 | #endif |
41 | ||
42 | #define APPEND(a, b) APPEND2 (a, b) | |
43 | #define APPEND2(a, b) a##b | |
44 | ||
0923c7a5 | 45 | |
1cab5444 UD |
46 | #define FCVT_BUFFER APPEND (FUNC_PREFIX, fcvt_buffer) |
47 | #define ECVT_BUFFER APPEND (FUNC_PREFIX, ecvt_buffer) | |
48 | ||
49 | ||
67994d6f UD |
50 | static char FCVT_BUFFER[MAXDIG]; |
51 | static char ECVT_BUFFER[MAXDIG]; | |
1cab5444 UD |
52 | |
53 | ||
0923c7a5 | 54 | char * |
2064087b RM |
55 | APPEND (FUNC_PREFIX, fcvt) (value, ndigit, decpt, sign) |
56 | FLOAT_TYPE value; | |
0923c7a5 RM |
57 | int ndigit, *decpt, *sign; |
58 | { | |
1cab5444 UD |
59 | (void) APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, |
60 | FCVT_BUFFER, MAXDIG); | |
61 | ||
62 | return FCVT_BUFFER; | |
63 | } | |
64 | ||
65 | ||
0923c7a5 | 66 | char * |
2064087b RM |
67 | APPEND (FUNC_PREFIX, ecvt) (value, ndigit, decpt, sign) |
68 | FLOAT_TYPE value; | |
0923c7a5 RM |
69 | int ndigit, *decpt, *sign; |
70 | { | |
1cab5444 UD |
71 | (void) APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, |
72 | ECVT_BUFFER, MAXDIG); | |
73 | ||
74 | return ECVT_BUFFER; | |
0923c7a5 RM |
75 | } |
76 | ||
77 | char * | |
2064087b RM |
78 | APPEND (FUNC_PREFIX, gcvt) (value, ndigit, buf) |
79 | FLOAT_TYPE value; | |
0923c7a5 RM |
80 | int ndigit; |
81 | char *buf; | |
82 | { | |
67994d6f | 83 | sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value); |
0923c7a5 RM |
84 | return buf; |
85 | } |