sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 1999
|
sl@0
|
3 |
* Silicon Graphics Computer Systems, Inc.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* Copyright (c) 1999
|
sl@0
|
6 |
* Boris Fomitchev
|
sl@0
|
7 |
*
|
sl@0
|
8 |
* This material is provided "as is", with absolutely no warranty expressed
|
sl@0
|
9 |
* or implied. Any use is at your own risk.
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* Permission to use or copy this software for any purpose is hereby granted
|
sl@0
|
12 |
* without fee, provided the above notices are retained on all copies.
|
sl@0
|
13 |
* Permission to modify the code and to distribute modified code is granted,
|
sl@0
|
14 |
* provided the above notices are retained, and a notice that the code was
|
sl@0
|
15 |
* modified is included with the above copyright notice.
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "stlport_prefix.h"
|
sl@0
|
20 |
|
sl@0
|
21 |
#include <cmath>
|
sl@0
|
22 |
#include <ios>
|
sl@0
|
23 |
#include <locale>
|
sl@0
|
24 |
|
sl@0
|
25 |
#if defined (__DECCXX)
|
sl@0
|
26 |
# define NDIG 400
|
sl@0
|
27 |
#else
|
sl@0
|
28 |
# define NDIG 82
|
sl@0
|
29 |
#endif
|
sl@0
|
30 |
|
sl@0
|
31 |
#if defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
32 |
# define MAXECVT 17
|
sl@0
|
33 |
# define MAXFCVT 18
|
sl@0
|
34 |
typedef double max_double_type;
|
sl@0
|
35 |
#else
|
sl@0
|
36 |
# define MAXECVT 35
|
sl@0
|
37 |
# define MAXFCVT 36
|
sl@0
|
38 |
typedef long double max_double_type;
|
sl@0
|
39 |
#endif
|
sl@0
|
40 |
|
sl@0
|
41 |
#define MAXFSIG MAXECVT
|
sl@0
|
42 |
#define MAXESIZ 5
|
sl@0
|
43 |
|
sl@0
|
44 |
#define todigit(x) ((x)+'0')
|
sl@0
|
45 |
|
sl@0
|
46 |
#if defined (_STLP_UNIX)
|
sl@0
|
47 |
|
sl@0
|
48 |
# if defined (__sun)
|
sl@0
|
49 |
# include <floatingpoint.h>
|
sl@0
|
50 |
# endif
|
sl@0
|
51 |
|
sl@0
|
52 |
# if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
|
sl@0
|
53 |
// DEC, SGI & Solaris need this
|
sl@0
|
54 |
# include <values.h>
|
sl@0
|
55 |
# include <nan.h>
|
sl@0
|
56 |
# endif
|
sl@0
|
57 |
|
sl@0
|
58 |
# if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
|
sl@0
|
59 |
defined(__FreeBSD__)
|
sl@0
|
60 |
# define USE_SPRINTF_INSTEAD
|
sl@0
|
61 |
# endif
|
sl@0
|
62 |
|
sl@0
|
63 |
# if defined( _AIX ) // JFA 3-Aug-2000
|
sl@0
|
64 |
# include <math.h>
|
sl@0
|
65 |
# include <float.h>
|
sl@0
|
66 |
# endif
|
sl@0
|
67 |
|
sl@0
|
68 |
#endif
|
sl@0
|
69 |
|
sl@0
|
70 |
#include <cstdio>
|
sl@0
|
71 |
#include <cstdlib>
|
sl@0
|
72 |
|
sl@0
|
73 |
//#if defined(_CRAY)
|
sl@0
|
74 |
//# include <stdlib.h>
|
sl@0
|
75 |
//#endif
|
sl@0
|
76 |
|
sl@0
|
77 |
#if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
|
sl@0
|
78 |
defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
|
sl@0
|
79 |
# include <float.h>
|
sl@0
|
80 |
#endif
|
sl@0
|
81 |
|
sl@0
|
82 |
#if defined(__MRC__) || defined(__SC__) || defined(_CRAY) //*TY 02/24/2000 - added support for MPW
|
sl@0
|
83 |
# include <fp.h>
|
sl@0
|
84 |
#endif
|
sl@0
|
85 |
|
sl@0
|
86 |
#if defined (__CYGWIN__)
|
sl@0
|
87 |
# include <ieeefp.h>
|
sl@0
|
88 |
#endif
|
sl@0
|
89 |
|
sl@0
|
90 |
#if defined (__MSL__)
|
sl@0
|
91 |
# include <cstdlib> // for atoi
|
sl@0
|
92 |
# include <cstdio> // for snprintf
|
sl@0
|
93 |
# include <algorithm>
|
sl@0
|
94 |
# include <cassert>
|
sl@0
|
95 |
#endif
|
sl@0
|
96 |
|
sl@0
|
97 |
#if defined (__ISCPP__)
|
sl@0
|
98 |
# include <cfloat>
|
sl@0
|
99 |
#endif
|
sl@0
|
100 |
|
sl@0
|
101 |
#include <algorithm>
|
sl@0
|
102 |
|
sl@0
|
103 |
#if defined (__DMC__)
|
sl@0
|
104 |
# define snprintf _snprintf
|
sl@0
|
105 |
#endif
|
sl@0
|
106 |
|
sl@0
|
107 |
#if defined(__hpux) && (!defined(_INCLUDE_HPUX_SOURCE) || defined(__GNUC__))
|
sl@0
|
108 |
extern "C" double erf(double);
|
sl@0
|
109 |
extern "C" double erfc(double);
|
sl@0
|
110 |
extern "C" double gamma(double); /* obsolescent */
|
sl@0
|
111 |
extern "C" double hypot(double, double);
|
sl@0
|
112 |
extern "C" int isnan(double);
|
sl@0
|
113 |
extern "C" double j0(double);
|
sl@0
|
114 |
extern "C" double j1(double);
|
sl@0
|
115 |
extern "C" double jn(int, double);
|
sl@0
|
116 |
extern "C" double lgamma(double);
|
sl@0
|
117 |
extern "C" double y0(double);
|
sl@0
|
118 |
extern "C" double y1(double);
|
sl@0
|
119 |
extern "C" double yn(int, double);
|
sl@0
|
120 |
|
sl@0
|
121 |
# define HUGE_VALF _SINFINITY
|
sl@0
|
122 |
# define INFINITY _SINFINITY
|
sl@0
|
123 |
# define NAN _SQNAN
|
sl@0
|
124 |
|
sl@0
|
125 |
# define isnan(x) _ISNAN(x)
|
sl@0
|
126 |
# define isinf(x) _ISINF(x)
|
sl@0
|
127 |
# define signbit(x) _SIGNBIT(x)
|
sl@0
|
128 |
# define isfinite(x) _ISFINITE(x)
|
sl@0
|
129 |
# define isnormal(x) _ISNORMAL(x)
|
sl@0
|
130 |
# define fpclassify(x) _FPCLASSIFY(x)
|
sl@0
|
131 |
# define isunordered(x,y) _ISUNORDERED(x,y)
|
sl@0
|
132 |
# define isgreater(x,y) _ISGREATER(x,y)
|
sl@0
|
133 |
# define isgreaterequal(x,y) _ISGREATEREQUAL(x,y)
|
sl@0
|
134 |
# define isless(x,y) _ISLESS(x,y)
|
sl@0
|
135 |
# define islessequal(x,y) _ISLESSEQUAL(x,y)
|
sl@0
|
136 |
# define islessgreater(x,y) _ISLESSGREATER(x,y)
|
sl@0
|
137 |
|
sl@0
|
138 |
# define FP_NORMAL 0
|
sl@0
|
139 |
# define FP_ZERO 1
|
sl@0
|
140 |
# define FP_INFINITE 2
|
sl@0
|
141 |
# define FP_SUBNORMAL 3
|
sl@0
|
142 |
# define FP_NAN 4
|
sl@0
|
143 |
|
sl@0
|
144 |
# define DECIMAL_DIG 17
|
sl@0
|
145 |
|
sl@0
|
146 |
# define _IS64(x) (sizeof(x) == sizeof(double))
|
sl@0
|
147 |
# define _IS32(x) (sizeof(x) == sizeof(float))
|
sl@0
|
148 |
|
sl@0
|
149 |
extern "C" {
|
sl@0
|
150 |
extern double copysign(double, double);
|
sl@0
|
151 |
extern const float _SINFINITY;
|
sl@0
|
152 |
extern const float _SQNAN;
|
sl@0
|
153 |
# if defined (_PA_RISC)
|
sl@0
|
154 |
# define _ISNAN(x) (_IS32(x)?_Isnanf(x):(isnan)(x))
|
sl@0
|
155 |
# define _ISINF(x) (_IS32(x)?_Isinff(x):_Isinf(x))
|
sl@0
|
156 |
# define _SIGNBIT(x) (_IS32(x)?_Signbitf(x):_Signbit(x))
|
sl@0
|
157 |
# define _ISFINITE(x) (_IS32(x)?_Isfinitef(x):_Isfinite(x))
|
sl@0
|
158 |
# define _ISNORMAL(x) (_IS32(x)?_Isnormalf(x):_Isnormal(x))
|
sl@0
|
159 |
# define _FPCLASSIFY(x) (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1)
|
sl@0
|
160 |
# define _ISUNORDERED(x,y) (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y))
|
sl@0
|
161 |
extern int _Signbit(double);
|
sl@0
|
162 |
extern int _Signbitf(float);
|
sl@0
|
163 |
extern int _Isnanf(float);
|
sl@0
|
164 |
extern int _Isfinite(double);
|
sl@0
|
165 |
extern int _Isfinitef(float);
|
sl@0
|
166 |
extern int _Isinf(double);
|
sl@0
|
167 |
extern int _Isinff(float);
|
sl@0
|
168 |
extern int _Isnormal(double);
|
sl@0
|
169 |
extern int _Isnormalf(float);
|
sl@0
|
170 |
extern int _Isunordered(double, double);
|
sl@0
|
171 |
extern int _Isunorderedf(float, float);
|
sl@0
|
172 |
extern int _Fpclassify(double);
|
sl@0
|
173 |
extern int _Fpclassifyf(float);
|
sl@0
|
174 |
# else
|
sl@0
|
175 |
# include "math_ia64_internal.h"
|
sl@0
|
176 |
# define _FPCLASSIFY(x) (_IS32(x)?_Fpclassf(x):_Fpclass(x))
|
sl@0
|
177 |
extern int _Fpclass(double);
|
sl@0
|
178 |
extern int _Fpclassf(float);
|
sl@0
|
179 |
# endif
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
# if !defined (_INCLUDE_XOPEN_SOURCE_EXTENDED)
|
sl@0
|
183 |
extern "C" char *fcvt(double, int, int *, int *);
|
sl@0
|
184 |
extern "C" char *ecvt(double, int, int *, int *);
|
sl@0
|
185 |
# endif
|
sl@0
|
186 |
# if !defined (_INCLUDE_HPUX_SOURCE)
|
sl@0
|
187 |
# if !defined (_LONG_DOUBLE)
|
sl@0
|
188 |
# define _LONG_DOUBLE
|
sl@0
|
189 |
typedef struct {
|
sl@0
|
190 |
uint32_t word1, word2, word3, word4;
|
sl@0
|
191 |
} long_double;
|
sl@0
|
192 |
# endif /* _LONG_DOUBLE */
|
sl@0
|
193 |
extern "C" char *_ldecvt(long_double, int, int *, int *);
|
sl@0
|
194 |
extern "C" char *_ldfcvt(long_double, int, int *, int *);
|
sl@0
|
195 |
|
sl@0
|
196 |
# endif
|
sl@0
|
197 |
#endif /* __hpux */
|
sl@0
|
198 |
|
sl@0
|
199 |
_STLP_BEGIN_NAMESPACE
|
sl@0
|
200 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
sl@0
|
201 |
|
sl@0
|
202 |
#if defined (__MWERKS__) || defined(__BEOS__)
|
sl@0
|
203 |
# define USE_SPRINTF_INSTEAD
|
sl@0
|
204 |
#endif
|
sl@0
|
205 |
|
sl@0
|
206 |
#if defined (_AIX) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
sl@0
|
207 |
// Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here
|
sl@0
|
208 |
|
sl@0
|
209 |
# if !defined(_REENTRANT) && !defined(_THREAD_SAFE) && !(defined(_POSIX_THREADS) && defined(__OpenBSD__))
|
sl@0
|
210 |
# define LOCK_CVT
|
sl@0
|
211 |
# define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign);
|
sl@0
|
212 |
# else
|
sl@0
|
213 |
static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
|
sl@0
|
214 |
# define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex);
|
sl@0
|
215 |
# define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf;
|
sl@0
|
216 |
# endif // !_REENTRANT
|
sl@0
|
217 |
#endif // _AIX || __FreeBSD__ || __NetBSD__ || __OpenBSD__
|
sl@0
|
218 |
|
sl@0
|
219 |
// Tests for infinity and NaN differ on different OSs. We encapsulate
|
sl@0
|
220 |
// these differences here.
|
sl@0
|
221 |
|
sl@0
|
222 |
#if !defined (USE_SPRINTF_INSTEAD)
|
sl@0
|
223 |
# if defined (__hpux) || defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
|
sl@0
|
224 |
defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
|
sl@0
|
225 |
static inline bool _Stl_is_nan_or_inf(double x)
|
sl@0
|
226 |
# if defined (isfinite)
|
sl@0
|
227 |
{ return !isfinite(x); }
|
sl@0
|
228 |
# else
|
sl@0
|
229 |
{ return !finite(x); }
|
sl@0
|
230 |
# endif
|
sl@0
|
231 |
static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
|
sl@0
|
232 |
static inline bool _Stl_is_inf(double x) { return isinf(x); }
|
sl@0
|
233 |
// inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; }
|
sl@0
|
234 |
static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; }
|
sl@0
|
235 |
# elif (defined (__unix) || defined (__unix__)) && \
|
sl@0
|
236 |
!defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
|
sl@0
|
237 |
!defined (_CRAY)
|
sl@0
|
238 |
static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
|
sl@0
|
239 |
static inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); }
|
sl@0
|
240 |
static inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); }
|
sl@0
|
241 |
static inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); }
|
sl@0
|
242 |
# elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 )
|
sl@0
|
243 |
static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
|
sl@0
|
244 |
static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! _isnan(x);}
|
sl@0
|
245 |
static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
|
sl@0
|
246 |
static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && x < 0 ; }
|
sl@0
|
247 |
# elif defined (_STLP_MSVC_LIB) || (defined (__MINGW32__) && !(defined(__SYMBIAN32__) && defined(__GCCXML__)) ) || defined (__BORLANDC__)
|
sl@0
|
248 |
static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
|
sl@0
|
249 |
static inline bool _Stl_is_inf(double x) {
|
sl@0
|
250 |
int fclass = _fpclass(x);
|
sl@0
|
251 |
return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
|
sl@0
|
252 |
}
|
sl@0
|
253 |
static inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; }
|
sl@0
|
254 |
static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; }
|
sl@0
|
255 |
# elif defined (__MRC__) || defined (__SC__) //*TY 02/24/2000 - added support for MPW
|
sl@0
|
256 |
static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
|
sl@0
|
257 |
static bool _Stl_is_inf(double x) { return !isfinite(x); }
|
sl@0
|
258 |
static bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); }
|
sl@0
|
259 |
static bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
|
sl@0
|
260 |
# elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
|
sl@0
|
261 |
static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
|
sl@0
|
262 |
static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); }
|
sl@0
|
263 |
static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
|
sl@0
|
264 |
static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; }
|
sl@0
|
265 |
# elif defined( _AIX ) // JFA 11-Aug-2000
|
sl@0
|
266 |
static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
|
sl@0
|
267 |
static bool _Stl_is_inf(double x) { return !finite(x); }
|
sl@0
|
268 |
// bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; }
|
sl@0
|
269 |
static bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); }
|
sl@0
|
270 |
static bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
|
sl@0
|
271 |
# elif defined (__ISCPP__)
|
sl@0
|
272 |
static inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
|
sl@0
|
273 |
static inline bool _Stl_is_inf (double x) { return _fp_isINF(x); }
|
sl@0
|
274 |
static inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; }
|
sl@0
|
275 |
static inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; }
|
sl@0
|
276 |
# elif defined (_CRAY)
|
sl@0
|
277 |
# if defined (_CRAYIEEE)
|
sl@0
|
278 |
static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
|
sl@0
|
279 |
static inline bool _Stl_is_inf(double x) { return isinf(x); }
|
sl@0
|
280 |
static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); }
|
sl@0
|
281 |
static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
|
sl@0
|
282 |
# else
|
sl@0
|
283 |
static inline bool _Stl_is_nan_or_inf(double x) { return false; }
|
sl@0
|
284 |
static inline bool _Stl_is_inf(double x) { return false; }
|
sl@0
|
285 |
static inline bool _Stl_is_neg_inf(double x) { return false; }
|
sl@0
|
286 |
static inline bool _Stl_is_neg_nan(double x) { return false; }
|
sl@0
|
287 |
# endif
|
sl@0
|
288 |
# else // nothing from above
|
sl@0
|
289 |
# define USE_SPRINTF_INSTEAD
|
sl@0
|
290 |
# endif
|
sl@0
|
291 |
#endif // !USE_SPRINTF_INSTEAD
|
sl@0
|
292 |
|
sl@0
|
293 |
#if !defined (USE_SPRINTF_INSTEAD)
|
sl@0
|
294 |
// Reentrant versions of floating-point conversion functions. The argument
|
sl@0
|
295 |
// lists look slightly different on different operating systems, so we're
|
sl@0
|
296 |
// encapsulating the differences here.
|
sl@0
|
297 |
|
sl@0
|
298 |
# if defined (__CYGWIN__) || defined(__DJGPP)
|
sl@0
|
299 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
300 |
{ return ecvtbuf(x, n, pt, sign, buf); }
|
sl@0
|
301 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
302 |
{ return fcvtbuf(x, n, pt, sign, buf); }
|
sl@0
|
303 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
304 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
305 |
{ return ecvtbuf(x, n, pt, sign, buf); }
|
sl@0
|
306 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
307 |
{ return fcvtbuf(x, n, pt, sign, buf); }
|
sl@0
|
308 |
# endif
|
sl@0
|
309 |
# elif defined (_STLP_USE_GLIBC)
|
sl@0
|
310 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
311 |
{ return buf + ecvt_r(x, n, pt, sign, buf, NDIG+2); }
|
sl@0
|
312 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
313 |
{ return buf + fcvt_r(x, n, pt, sign, buf, NDIG+2); }
|
sl@0
|
314 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
315 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
316 |
{ return buf + qecvt_r(x, n, pt, sign, buf, NDIG+2); }
|
sl@0
|
317 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
318 |
{ return buf + qfcvt_r(x, n, pt, sign, buf, NDIG+2); }
|
sl@0
|
319 |
# endif
|
sl@0
|
320 |
# elif defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
|
sl@0
|
321 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
322 |
{ return ecvt(x, n, pt, sign); }
|
sl@0
|
323 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
324 |
{ return fcvt(x, n, pt, sign); }
|
sl@0
|
325 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
326 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
327 |
{ return ecvtl(x, n, pt, sign); }
|
sl@0
|
328 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
329 |
{ return fcvtl(x, n, pt, sign); }
|
sl@0
|
330 |
# endif
|
sl@0
|
331 |
# elif defined (__sun)
|
sl@0
|
332 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
333 |
{ return econvert(x, n, pt, sign, buf); }
|
sl@0
|
334 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
335 |
{ return fconvert(x, n, pt, sign, buf); }
|
sl@0
|
336 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
337 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
338 |
{ return qeconvert(&x, n, pt, sign, buf); }
|
sl@0
|
339 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
340 |
{ return qfconvert(&x, n, pt, sign, buf); }
|
sl@0
|
341 |
# endif
|
sl@0
|
342 |
# elif defined (__DECCXX)
|
sl@0
|
343 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
344 |
{ return (ecvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
|
sl@0
|
345 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
346 |
{ return (fcvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
|
sl@0
|
347 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
348 |
// fbp : no "long double" conversions !
|
sl@0
|
349 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
350 |
{ return (ecvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0) ; }
|
sl@0
|
351 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
352 |
{ return (fcvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
|
sl@0
|
353 |
# endif
|
sl@0
|
354 |
# elif defined (__hpux)
|
sl@0
|
355 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
356 |
{ return ecvt(x, n, pt, sign); }
|
sl@0
|
357 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
358 |
{ return fcvt(x, n, pt, sign); }
|
sl@0
|
359 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
360 |
|
sl@0
|
361 |
# if defined( _REENTRANT ) && (defined(_PTHREADS_DRAFT4) || defined(PTHREAD_THREADS_MAX))
|
sl@0
|
362 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
363 |
{ return (_ldecvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
|
sl@0
|
364 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
365 |
{ return (_ldfcvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
|
sl@0
|
366 |
# else
|
sl@0
|
367 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
368 |
{ return _ldecvt(*(long_double*)&x, n, pt, sign); }
|
sl@0
|
369 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
370 |
{ return _ldfcvt(*(long_double*)&x, n, pt, sign); }
|
sl@0
|
371 |
# endif
|
sl@0
|
372 |
# endif
|
sl@0
|
373 |
# elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
|
sl@0
|
374 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
375 |
{ LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
|
sl@0
|
376 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
377 |
{ LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
|
sl@0
|
378 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
379 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
380 |
{ LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
|
sl@0
|
381 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
382 |
{ LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
|
sl@0
|
383 |
# endif
|
sl@0
|
384 |
# elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
|
sl@0
|
385 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
386 |
{ return ecvt_r(x, n, pt, sign, buf); }
|
sl@0
|
387 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
388 |
{ return fcvt_r(x, n, pt, sign, buf); }
|
sl@0
|
389 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
390 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
391 |
{ return qecvt_r(x, n, pt, sign, buf); }
|
sl@0
|
392 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
393 |
{ return qfcvt_r(x, n, pt, sign, buf); }
|
sl@0
|
394 |
# endif
|
sl@0
|
395 |
# elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
|
sl@0
|
396 |
// those guys claim _cvt functions being reentrant.
|
sl@0
|
397 |
# if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
|
sl@0
|
398 |
# define _STLP_APPEND(a, b) a##b
|
sl@0
|
399 |
# define _STLP_BUF_PARAMS , char* buf, size_t bsize
|
sl@0
|
400 |
# define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
|
sl@0
|
401 |
# else
|
sl@0
|
402 |
# define _STLP_CVT_DONT_NEED_BUF
|
sl@0
|
403 |
# define _STLP_BUF_PARAMS
|
sl@0
|
404 |
# define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
|
sl@0
|
405 |
# endif
|
sl@0
|
406 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
|
sl@0
|
407 |
{ _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
|
sl@0
|
408 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
|
sl@0
|
409 |
{ _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
|
sl@0
|
410 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
411 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
|
sl@0
|
412 |
{ _STLP_SECURE_FUN(_ecvt, (double)x, n, pt, sign); }
|
sl@0
|
413 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
|
sl@0
|
414 |
{ _STLP_SECURE_FUN(_fcvt, (double)x, n, pt, sign); }
|
sl@0
|
415 |
# endif
|
sl@0
|
416 |
# undef _STLP_SECURE_FUN
|
sl@0
|
417 |
# undef _STLP_BUF_PARAMS
|
sl@0
|
418 |
# undef _STLP_APPEND
|
sl@0
|
419 |
# elif defined (__ISCPP__)
|
sl@0
|
420 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
421 |
{ return _fp_ecvt( x, n, pt, sign, buf); }
|
sl@0
|
422 |
|
sl@0
|
423 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
424 |
{ return _fp_fcvt(x, n, pt, sign, buf); }
|
sl@0
|
425 |
|
sl@0
|
426 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
427 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
428 |
{ return _fp_ecvt( x, n, pt, sign, buf); }
|
sl@0
|
429 |
|
sl@0
|
430 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
|
sl@0
|
431 |
{ return _fp_fcvt(x, n, pt, sign, buf); }
|
sl@0
|
432 |
# endif
|
sl@0
|
433 |
# elif defined (__MRC__) || defined (__SC__) || defined (_CRAY)
|
sl@0
|
434 |
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* )
|
sl@0
|
435 |
{ return ecvt( x, n, pt, sign ); }
|
sl@0
|
436 |
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* )
|
sl@0
|
437 |
{ return fcvt(x, n, pt, sign); }
|
sl@0
|
438 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
439 |
static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* )
|
sl@0
|
440 |
{ return ecvt( x, n, pt, sign ); }
|
sl@0
|
441 |
static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* )
|
sl@0
|
442 |
{ return fcvt(x, n, pt, sign); }
|
sl@0
|
443 |
# endif
|
sl@0
|
444 |
# endif
|
sl@0
|
445 |
|
sl@0
|
446 |
# if defined (_STLP_CVT_DONT_NEED_BUF)
|
sl@0
|
447 |
# define _STLP_CVT_BUFFER(B)
|
sl@0
|
448 |
# elif !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
|
sl@0
|
449 |
# define _STLP_CVT_BUFFER(B) , B
|
sl@0
|
450 |
# else
|
sl@0
|
451 |
# define _STLP_CVT_BUFFER(B) , _STLP_ARRAY_AND_SIZE(B)
|
sl@0
|
452 |
# endif
|
sl@0
|
453 |
|
sl@0
|
454 |
# if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
|
sl@0
|
455 |
# define _STLP_BUFFER(B) B
|
sl@0
|
456 |
# else
|
sl@0
|
457 |
# define _STLP_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
|
sl@0
|
458 |
# endif
|
sl@0
|
459 |
|
sl@0
|
460 |
//----------------------------------------------------------------------
|
sl@0
|
461 |
// num_put
|
sl@0
|
462 |
|
sl@0
|
463 |
// __format_float formats a mantissa and exponent as returned by
|
sl@0
|
464 |
// one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
|
sl@0
|
465 |
// according to the specified precision and format flags. This is
|
sl@0
|
466 |
// based on doprnt but is much simpler since it is concerned only
|
sl@0
|
467 |
// with floating point input and does not consider all formats. It
|
sl@0
|
468 |
// also does not deal with blank padding, which is handled by
|
sl@0
|
469 |
// __copy_float_and_fill.
|
sl@0
|
470 |
|
sl@0
|
471 |
static size_t __format_float_scientific( __iostring& buf, const char *bp,
|
sl@0
|
472 |
int decpt, int sign, bool is_zero,
|
sl@0
|
473 |
ios_base::fmtflags flags,
|
sl@0
|
474 |
int precision, bool /* islong */)
|
sl@0
|
475 |
{
|
sl@0
|
476 |
// sign if required
|
sl@0
|
477 |
if (sign)
|
sl@0
|
478 |
buf += '-';
|
sl@0
|
479 |
else if (flags & ios_base::showpos)
|
sl@0
|
480 |
buf += '+';
|
sl@0
|
481 |
|
sl@0
|
482 |
// first digit of mantissa
|
sl@0
|
483 |
buf += *bp++;
|
sl@0
|
484 |
|
sl@0
|
485 |
// start of grouping position, grouping won't occur in scientific notation
|
sl@0
|
486 |
// as it is impossible to have something like 1234.0e04 but we return a correct
|
sl@0
|
487 |
// group position for coherency with __format_float_fixed.
|
sl@0
|
488 |
size_t __group_pos = buf.size();
|
sl@0
|
489 |
|
sl@0
|
490 |
// decimal point if required
|
sl@0
|
491 |
if (precision != 0 || flags & ios_base::showpoint) {
|
sl@0
|
492 |
buf += '.';
|
sl@0
|
493 |
}
|
sl@0
|
494 |
|
sl@0
|
495 |
// rest of mantissa
|
sl@0
|
496 |
int rz = precision;
|
sl@0
|
497 |
while (rz-- > 0 && *bp != 0)
|
sl@0
|
498 |
buf += *bp++;
|
sl@0
|
499 |
|
sl@0
|
500 |
// exponent
|
sl@0
|
501 |
char expbuf[MAXESIZ + 2];
|
sl@0
|
502 |
char *suffix = expbuf + MAXESIZ;
|
sl@0
|
503 |
*suffix = 0;
|
sl@0
|
504 |
if (!is_zero) {
|
sl@0
|
505 |
int nn = decpt - 1;
|
sl@0
|
506 |
if (nn < 0)
|
sl@0
|
507 |
nn = -nn;
|
sl@0
|
508 |
for (; nn > 9; nn /= 10)
|
sl@0
|
509 |
*--suffix = (char) todigit(nn % 10);
|
sl@0
|
510 |
*--suffix = (char) todigit(nn);
|
sl@0
|
511 |
}
|
sl@0
|
512 |
|
sl@0
|
513 |
// prepend leading zeros to exponent
|
sl@0
|
514 |
while (suffix > &expbuf[MAXESIZ - 2])
|
sl@0
|
515 |
*--suffix = '0';
|
sl@0
|
516 |
|
sl@0
|
517 |
// put in the exponent sign
|
sl@0
|
518 |
*--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
|
sl@0
|
519 |
|
sl@0
|
520 |
// put in the e
|
sl@0
|
521 |
*--suffix = flags & ios_base::uppercase ? 'E' : 'e';
|
sl@0
|
522 |
|
sl@0
|
523 |
// copy the suffix
|
sl@0
|
524 |
buf += suffix;
|
sl@0
|
525 |
return __group_pos;
|
sl@0
|
526 |
}
|
sl@0
|
527 |
|
sl@0
|
528 |
static size_t __format_float_fixed( __iostring &buf, const char *bp,
|
sl@0
|
529 |
int decpt, int sign, bool /* x */,
|
sl@0
|
530 |
ios_base::fmtflags flags,
|
sl@0
|
531 |
int precision, bool islong )
|
sl@0
|
532 |
{
|
sl@0
|
533 |
if ( sign && (decpt > -precision) && (*bp != 0) )
|
sl@0
|
534 |
buf += '-';
|
sl@0
|
535 |
else if ( flags & ios_base::showpos )
|
sl@0
|
536 |
buf += '+';
|
sl@0
|
537 |
|
sl@0
|
538 |
int k = 0;
|
sl@0
|
539 |
int maxfsig = islong ? 2*MAXFSIG : MAXFSIG;
|
sl@0
|
540 |
|
sl@0
|
541 |
// digits before decimal point
|
sl@0
|
542 |
int nnn = decpt;
|
sl@0
|
543 |
do {
|
sl@0
|
544 |
buf += ((nnn <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++));
|
sl@0
|
545 |
} while ( --nnn > 0 );
|
sl@0
|
546 |
|
sl@0
|
547 |
// start of grouping position
|
sl@0
|
548 |
size_t __group_pos = buf.size();
|
sl@0
|
549 |
|
sl@0
|
550 |
// decimal point if needed
|
sl@0
|
551 |
if ( flags & ios_base::showpoint || precision > 0 ) {
|
sl@0
|
552 |
buf += '.';
|
sl@0
|
553 |
}
|
sl@0
|
554 |
|
sl@0
|
555 |
// digits after decimal point if any
|
sl@0
|
556 |
nnn = (min) (precision, MAXFCVT);
|
sl@0
|
557 |
|
sl@0
|
558 |
while ( --nnn >= 0 ) {
|
sl@0
|
559 |
buf += (++decpt <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++);
|
sl@0
|
560 |
}
|
sl@0
|
561 |
|
sl@0
|
562 |
// trailing zeros if needed
|
sl@0
|
563 |
if ( precision > MAXFCVT ) {
|
sl@0
|
564 |
buf.append( precision - MAXFCVT, '0' );
|
sl@0
|
565 |
}
|
sl@0
|
566 |
|
sl@0
|
567 |
return __group_pos;
|
sl@0
|
568 |
}
|
sl@0
|
569 |
|
sl@0
|
570 |
static void __format_nan_or_inf(__iostring& buf, double x, ios_base::fmtflags flags)
|
sl@0
|
571 |
{
|
sl@0
|
572 |
static const char* inf[2] = { "inf", "Inf" };
|
sl@0
|
573 |
static const char* nan[2] = { "nan", "NaN" };
|
sl@0
|
574 |
const char** inf_or_nan;
|
sl@0
|
575 |
if (_Stl_is_inf(x)) { // Infinity
|
sl@0
|
576 |
inf_or_nan = inf;
|
sl@0
|
577 |
if (_Stl_is_neg_inf(x))
|
sl@0
|
578 |
buf += '-';
|
sl@0
|
579 |
else if (flags & ios_base::showpos)
|
sl@0
|
580 |
buf += '+';
|
sl@0
|
581 |
} else { // NaN
|
sl@0
|
582 |
inf_or_nan = nan;
|
sl@0
|
583 |
if (_Stl_is_neg_nan(x))
|
sl@0
|
584 |
buf += '-';
|
sl@0
|
585 |
else if (flags & ios_base::showpos)
|
sl@0
|
586 |
buf += '+';
|
sl@0
|
587 |
}
|
sl@0
|
588 |
buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
|
sl@0
|
589 |
}
|
sl@0
|
590 |
|
sl@0
|
591 |
template <class max_double_type>
|
sl@0
|
592 |
static inline size_t __format_float( __iostring &buf, const char * bp,
|
sl@0
|
593 |
int decpt, int sign, max_double_type x,
|
sl@0
|
594 |
ios_base::fmtflags flags,
|
sl@0
|
595 |
int precision, bool islong)
|
sl@0
|
596 |
{
|
sl@0
|
597 |
size_t __group_pos = 0;
|
sl@0
|
598 |
// Output of infinities and NANs does not depend on the format flags
|
sl@0
|
599 |
if (_Stl_is_nan_or_inf((double)x)) { // Infinity or NaN
|
sl@0
|
600 |
__format_nan_or_inf(buf, (double)x, flags);
|
sl@0
|
601 |
} else { // representable number
|
sl@0
|
602 |
switch (flags & ios_base::floatfield) {
|
sl@0
|
603 |
case ios_base::scientific:
|
sl@0
|
604 |
__group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0.0,
|
sl@0
|
605 |
flags, precision, islong);
|
sl@0
|
606 |
break;
|
sl@0
|
607 |
case ios_base::fixed:
|
sl@0
|
608 |
__group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
|
sl@0
|
609 |
flags, precision, islong);
|
sl@0
|
610 |
break;
|
sl@0
|
611 |
default: // g format
|
sl@0
|
612 |
// establish default precision
|
sl@0
|
613 |
if (flags & ios_base::showpoint || precision > 0) {
|
sl@0
|
614 |
if (precision == 0) precision = 1;
|
sl@0
|
615 |
} else
|
sl@0
|
616 |
precision = 6;
|
sl@0
|
617 |
|
sl@0
|
618 |
// reset exponent if value is zero
|
sl@0
|
619 |
if (x == 0)
|
sl@0
|
620 |
decpt = 1;
|
sl@0
|
621 |
|
sl@0
|
622 |
int kk = precision;
|
sl@0
|
623 |
if (!(flags & ios_base::showpoint)) {
|
sl@0
|
624 |
size_t n = strlen(bp);
|
sl@0
|
625 |
if (n < (size_t)kk)
|
sl@0
|
626 |
kk = (int)n;
|
sl@0
|
627 |
while (kk >= 1 && bp[kk-1] == '0')
|
sl@0
|
628 |
--kk;
|
sl@0
|
629 |
}
|
sl@0
|
630 |
|
sl@0
|
631 |
if (decpt < -3 || decpt > precision) {
|
sl@0
|
632 |
precision = kk - 1;
|
sl@0
|
633 |
__group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0,
|
sl@0
|
634 |
flags, precision, islong);
|
sl@0
|
635 |
} else {
|
sl@0
|
636 |
precision = kk - decpt;
|
sl@0
|
637 |
__group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
|
sl@0
|
638 |
flags, precision, islong);
|
sl@0
|
639 |
}
|
sl@0
|
640 |
break;
|
sl@0
|
641 |
} /* switch */
|
sl@0
|
642 |
} /* else is_nan_or_inf */
|
sl@0
|
643 |
return __group_pos;
|
sl@0
|
644 |
}
|
sl@0
|
645 |
|
sl@0
|
646 |
#else /* USE_SPRINTF_INSTEAD */
|
sl@0
|
647 |
|
sl@0
|
648 |
struct GroupPos {
|
sl@0
|
649 |
bool operator () (char __c) const {
|
sl@0
|
650 |
return __c == '.' ||
|
sl@0
|
651 |
__c == 'e' || __c == 'E';
|
sl@0
|
652 |
}
|
sl@0
|
653 |
};
|
sl@0
|
654 |
|
sl@0
|
655 |
// Creates a format string for sprintf()
|
sl@0
|
656 |
static int __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
|
sl@0
|
657 |
fmtbuf[0] = '%';
|
sl@0
|
658 |
int i = 1;
|
sl@0
|
659 |
|
sl@0
|
660 |
if (flags & ios_base::showpos)
|
sl@0
|
661 |
fmtbuf[i++] = '+';
|
sl@0
|
662 |
|
sl@0
|
663 |
if (flags & ios_base::showpoint)
|
sl@0
|
664 |
fmtbuf[i++] = '#';
|
sl@0
|
665 |
|
sl@0
|
666 |
fmtbuf[i++] = '.';
|
sl@0
|
667 |
fmtbuf[i++] = '*';
|
sl@0
|
668 |
|
sl@0
|
669 |
if (long_modifier)
|
sl@0
|
670 |
fmtbuf[i++] = long_modifier;
|
sl@0
|
671 |
|
sl@0
|
672 |
switch (flags & ios_base::floatfield)
|
sl@0
|
673 |
{
|
sl@0
|
674 |
case ios_base::scientific:
|
sl@0
|
675 |
fmtbuf[i++] = (flags & ios_base::uppercase) ? 'E' : 'e';
|
sl@0
|
676 |
break;
|
sl@0
|
677 |
case ios_base::fixed:
|
sl@0
|
678 |
# if defined (__FreeBSD__)
|
sl@0
|
679 |
fmtbuf[i++] = 'f';
|
sl@0
|
680 |
# else
|
sl@0
|
681 |
fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
|
sl@0
|
682 |
# endif
|
sl@0
|
683 |
break;
|
sl@0
|
684 |
default:
|
sl@0
|
685 |
fmtbuf[i++] = (flags & ios_base::uppercase) ? 'G' : 'g';
|
sl@0
|
686 |
break;
|
sl@0
|
687 |
}
|
sl@0
|
688 |
|
sl@0
|
689 |
fmtbuf[i] = 0;
|
sl@0
|
690 |
return i;
|
sl@0
|
691 |
}
|
sl@0
|
692 |
|
sl@0
|
693 |
#endif /* USE_SPRINTF_INSTEAD */
|
sl@0
|
694 |
|
sl@0
|
695 |
_STLP_DECLSPEC size_t _STLP_CALL
|
sl@0
|
696 |
__write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
|
sl@0
|
697 |
double x) {
|
sl@0
|
698 |
#if defined (USE_SPRINTF_INSTEAD)
|
sl@0
|
699 |
/* If we want 'abitrary' precision, we should use 'abitrary' buffer size
|
sl@0
|
700 |
* below. - ptr
|
sl@0
|
701 |
*/
|
sl@0
|
702 |
char static_buf[128];
|
sl@0
|
703 |
// char *static_buf = new char [128+precision];
|
sl@0
|
704 |
char fmtbuf[32];
|
sl@0
|
705 |
__fill_fmtbuf(fmtbuf, flags, 0);
|
sl@0
|
706 |
// snprintf(static_buf, 128+precision, fmtbuf, precision, x);
|
sl@0
|
707 |
# if !defined (N_PLAT_NLM)
|
sl@0
|
708 |
snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
|
sl@0
|
709 |
# else
|
sl@0
|
710 |
sprintf(static_buf, fmtbuf, precision, x);
|
sl@0
|
711 |
# endif
|
sl@0
|
712 |
buf = static_buf;
|
sl@0
|
713 |
// delete [] static_buf;
|
sl@0
|
714 |
return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
|
sl@0
|
715 |
#else
|
sl@0
|
716 |
# if !defined (_STLP_CVT_DONT_NEED_BUF)
|
sl@0
|
717 |
char cvtbuf[NDIG + 2];
|
sl@0
|
718 |
# endif
|
sl@0
|
719 |
char * bp;
|
sl@0
|
720 |
int decpt, sign;
|
sl@0
|
721 |
|
sl@0
|
722 |
switch (flags & ios_base::floatfield) {
|
sl@0
|
723 |
case ios_base::fixed:
|
sl@0
|
724 |
bp = _Stl_fcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
725 |
break;
|
sl@0
|
726 |
case ios_base::scientific :
|
sl@0
|
727 |
bp = _Stl_ecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
728 |
break;
|
sl@0
|
729 |
default :
|
sl@0
|
730 |
bp = _Stl_ecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
731 |
break;
|
sl@0
|
732 |
}
|
sl@0
|
733 |
return __format_float(buf, bp, decpt, sign, x, flags, precision, false);
|
sl@0
|
734 |
#endif
|
sl@0
|
735 |
}
|
sl@0
|
736 |
|
sl@0
|
737 |
#if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
738 |
_STLP_DECLSPEC size_t _STLP_CALL
|
sl@0
|
739 |
__write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
|
sl@0
|
740 |
long double x) {
|
sl@0
|
741 |
# if defined (USE_SPRINTF_INSTEAD)
|
sl@0
|
742 |
/* If we want 'abitrary' precision, we should use 'abitrary' buffer size
|
sl@0
|
743 |
* below. - ptr
|
sl@0
|
744 |
*/
|
sl@0
|
745 |
char static_buf[128];
|
sl@0
|
746 |
// char *static_buf = new char [128+precision];
|
sl@0
|
747 |
char fmtbuf[64];
|
sl@0
|
748 |
int i = __fill_fmtbuf(fmtbuf, flags, 'L');
|
sl@0
|
749 |
// snprintf(static_buf, 128+precision, fmtbuf, precision, x);
|
sl@0
|
750 |
# if !defined (N_PLAT_NLM)
|
sl@0
|
751 |
snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
|
sl@0
|
752 |
# else
|
sl@0
|
753 |
sprintf(static_buf, fmtbuf, precision, x);
|
sl@0
|
754 |
# endif
|
sl@0
|
755 |
// we should be able to return buf + sprintf(), but we do not trust'em...
|
sl@0
|
756 |
buf = static_buf;
|
sl@0
|
757 |
// delete [] static_buf;
|
sl@0
|
758 |
return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
|
sl@0
|
759 |
# else
|
sl@0
|
760 |
# if !defined (_STLP_CVT_DONT_NEED_BUF)
|
sl@0
|
761 |
char cvtbuf[NDIG + 2];
|
sl@0
|
762 |
# endif
|
sl@0
|
763 |
char * bp;
|
sl@0
|
764 |
int decpt, sign;
|
sl@0
|
765 |
|
sl@0
|
766 |
switch (flags & ios_base::floatfield) {
|
sl@0
|
767 |
case ios_base::fixed:
|
sl@0
|
768 |
bp = _Stl_qfcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
769 |
break;
|
sl@0
|
770 |
case ios_base::scientific:
|
sl@0
|
771 |
bp = _Stl_qecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
772 |
break;
|
sl@0
|
773 |
default :
|
sl@0
|
774 |
bp = _Stl_qecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
775 |
break;
|
sl@0
|
776 |
}
|
sl@0
|
777 |
return __format_float(buf, bp, decpt, sign, x, flags, precision, true);
|
sl@0
|
778 |
# endif /* USE_SPRINTF_INSTEAD */
|
sl@0
|
779 |
}
|
sl@0
|
780 |
#endif /* _STLP_NO_LONG_DOUBLE */
|
sl@0
|
781 |
|
sl@0
|
782 |
_STLP_DECLSPEC void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
|
sl@0
|
783 |
#if defined (USE_SPRINTF_INSTEAD)
|
sl@0
|
784 |
char cvtbuf[128];
|
sl@0
|
785 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
786 |
# if !defined (N_PLAT_NLM)
|
sl@0
|
787 |
snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
|
sl@0
|
788 |
# else
|
sl@0
|
789 |
sprintf(cvtbuf, "%Lf", __x); // check for 1234.56!
|
sl@0
|
790 |
# endif
|
sl@0
|
791 |
# else
|
sl@0
|
792 |
snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x); // check for 1234.56!
|
sl@0
|
793 |
# endif
|
sl@0
|
794 |
char *p = strchr( cvtbuf, '.' );
|
sl@0
|
795 |
if ( p == 0 ) {
|
sl@0
|
796 |
out.append( cvtbuf );
|
sl@0
|
797 |
} else {
|
sl@0
|
798 |
out.append( cvtbuf, p );
|
sl@0
|
799 |
}
|
sl@0
|
800 |
#else
|
sl@0
|
801 |
# if !defined (_STLP_CVT_DONT_NEED_BUF)
|
sl@0
|
802 |
char cvtbuf[NDIG + 2];
|
sl@0
|
803 |
# endif
|
sl@0
|
804 |
char * bp;
|
sl@0
|
805 |
int decpt, sign;
|
sl@0
|
806 |
# if !defined (_STLP_NO_LONG_DOUBLE)
|
sl@0
|
807 |
bp = _Stl_qfcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
808 |
# else
|
sl@0
|
809 |
bp = _Stl_fcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
|
sl@0
|
810 |
# endif
|
sl@0
|
811 |
|
sl@0
|
812 |
if (sign) {
|
sl@0
|
813 |
out += '-';
|
sl@0
|
814 |
}
|
sl@0
|
815 |
out.append(bp, bp + decpt);
|
sl@0
|
816 |
#endif // USE_PRINTF_INSTEAD
|
sl@0
|
817 |
}
|
sl@0
|
818 |
|
sl@0
|
819 |
|
sl@0
|
820 |
#if !defined (_STLP_NO_WCHAR_T)
|
sl@0
|
821 |
_STLP_DECLSPEC void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
|
sl@0
|
822 |
const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot)
|
sl@0
|
823 |
{
|
sl@0
|
824 |
string::const_iterator str_ite(str.begin()), str_end(str.end());
|
sl@0
|
825 |
|
sl@0
|
826 |
//First loop, check the dot char
|
sl@0
|
827 |
if (__check_dot) {
|
sl@0
|
828 |
while (str_ite != str_end) {
|
sl@0
|
829 |
if (*str_ite != '.') {
|
sl@0
|
830 |
out += ct.widen(*str_ite++);
|
sl@0
|
831 |
} else {
|
sl@0
|
832 |
out += dot;
|
sl@0
|
833 |
break;
|
sl@0
|
834 |
}
|
sl@0
|
835 |
}
|
sl@0
|
836 |
} else {
|
sl@0
|
837 |
if (str_ite != str_end) {
|
sl@0
|
838 |
out += ct.widen(*str_ite);
|
sl@0
|
839 |
}
|
sl@0
|
840 |
}
|
sl@0
|
841 |
|
sl@0
|
842 |
if (str_ite != str_end) {
|
sl@0
|
843 |
//Second loop, dot has been found, no check anymore
|
sl@0
|
844 |
while (++str_ite != str_end) {
|
sl@0
|
845 |
out += ct.widen(*str_ite);
|
sl@0
|
846 |
}
|
sl@0
|
847 |
}
|
sl@0
|
848 |
}
|
sl@0
|
849 |
|
sl@0
|
850 |
#endif
|
sl@0
|
851 |
|
sl@0
|
852 |
void _STLP_CALL
|
sl@0
|
853 |
__adjust_float_buffer(__iostring &str, char dot) {
|
sl@0
|
854 |
if ('.' != dot) {
|
sl@0
|
855 |
size_t __dot_pos = str.find('.');
|
sl@0
|
856 |
if (__dot_pos != string::npos) {
|
sl@0
|
857 |
str[__dot_pos] = dot;
|
sl@0
|
858 |
}
|
sl@0
|
859 |
}
|
sl@0
|
860 |
}
|
sl@0
|
861 |
|
sl@0
|
862 |
_STLP_MOVE_TO_STD_NAMESPACE
|
sl@0
|
863 |
_STLP_END_NAMESPACE
|
sl@0
|
864 |
|
sl@0
|
865 |
// Local Variables:
|
sl@0
|
866 |
// mode:C++
|
sl@0
|
867 |
// End:
|