First public contribution.
3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #include "stlport_prefix.h"
25 #if defined (__DECCXX)
31 #if defined (_STLP_NO_LONG_DOUBLE)
34 typedef double max_double_type;
38 typedef long double max_double_type;
41 #define MAXFSIG MAXECVT
44 #define todigit(x) ((x)+'0')
46 #if defined (_STLP_UNIX)
49 # include <floatingpoint.h>
52 # if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
53 // DEC, SGI & Solaris need this
58 # if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
60 # define USE_SPRINTF_INSTEAD
63 # if defined( _AIX ) // JFA 3-Aug-2000
74 //# include <stdlib.h>
77 #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
78 defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
82 #if defined(__MRC__) || defined(__SC__) || defined(_CRAY) //*TY 02/24/2000 - added support for MPW
86 #if defined (__CYGWIN__)
91 # include <cstdlib> // for atoi
92 # include <cstdio> // for snprintf
97 #if defined (__ISCPP__)
103 #if defined (__DMC__)
104 # define snprintf _snprintf
107 #if defined(__hpux) && (!defined(_INCLUDE_HPUX_SOURCE) || defined(__GNUC__))
108 extern "C" double erf(double);
109 extern "C" double erfc(double);
110 extern "C" double gamma(double); /* obsolescent */
111 extern "C" double hypot(double, double);
112 extern "C" int isnan(double);
113 extern "C" double j0(double);
114 extern "C" double j1(double);
115 extern "C" double jn(int, double);
116 extern "C" double lgamma(double);
117 extern "C" double y0(double);
118 extern "C" double y1(double);
119 extern "C" double yn(int, double);
121 # define HUGE_VALF _SINFINITY
122 # define INFINITY _SINFINITY
125 # define isnan(x) _ISNAN(x)
126 # define isinf(x) _ISINF(x)
127 # define signbit(x) _SIGNBIT(x)
128 # define isfinite(x) _ISFINITE(x)
129 # define isnormal(x) _ISNORMAL(x)
130 # define fpclassify(x) _FPCLASSIFY(x)
131 # define isunordered(x,y) _ISUNORDERED(x,y)
132 # define isgreater(x,y) _ISGREATER(x,y)
133 # define isgreaterequal(x,y) _ISGREATEREQUAL(x,y)
134 # define isless(x,y) _ISLESS(x,y)
135 # define islessequal(x,y) _ISLESSEQUAL(x,y)
136 # define islessgreater(x,y) _ISLESSGREATER(x,y)
140 # define FP_INFINITE 2
141 # define FP_SUBNORMAL 3
144 # define DECIMAL_DIG 17
146 # define _IS64(x) (sizeof(x) == sizeof(double))
147 # define _IS32(x) (sizeof(x) == sizeof(float))
150 extern double copysign(double, double);
151 extern const float _SINFINITY;
152 extern const float _SQNAN;
153 # if defined (_PA_RISC)
154 # define _ISNAN(x) (_IS32(x)?_Isnanf(x):(isnan)(x))
155 # define _ISINF(x) (_IS32(x)?_Isinff(x):_Isinf(x))
156 # define _SIGNBIT(x) (_IS32(x)?_Signbitf(x):_Signbit(x))
157 # define _ISFINITE(x) (_IS32(x)?_Isfinitef(x):_Isfinite(x))
158 # define _ISNORMAL(x) (_IS32(x)?_Isnormalf(x):_Isnormal(x))
159 # define _FPCLASSIFY(x) (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1)
160 # define _ISUNORDERED(x,y) (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y))
161 extern int _Signbit(double);
162 extern int _Signbitf(float);
163 extern int _Isnanf(float);
164 extern int _Isfinite(double);
165 extern int _Isfinitef(float);
166 extern int _Isinf(double);
167 extern int _Isinff(float);
168 extern int _Isnormal(double);
169 extern int _Isnormalf(float);
170 extern int _Isunordered(double, double);
171 extern int _Isunorderedf(float, float);
172 extern int _Fpclassify(double);
173 extern int _Fpclassifyf(float);
175 # include "math_ia64_internal.h"
176 # define _FPCLASSIFY(x) (_IS32(x)?_Fpclassf(x):_Fpclass(x))
177 extern int _Fpclass(double);
178 extern int _Fpclassf(float);
182 # if !defined (_INCLUDE_XOPEN_SOURCE_EXTENDED)
183 extern "C" char *fcvt(double, int, int *, int *);
184 extern "C" char *ecvt(double, int, int *, int *);
186 # if !defined (_INCLUDE_HPUX_SOURCE)
187 # if !defined (_LONG_DOUBLE)
188 # define _LONG_DOUBLE
190 uint32_t word1, word2, word3, word4;
192 # endif /* _LONG_DOUBLE */
193 extern "C" char *_ldecvt(long_double, int, int *, int *);
194 extern "C" char *_ldfcvt(long_double, int, int *, int *);
199 _STLP_BEGIN_NAMESPACE
200 _STLP_MOVE_TO_PRIV_NAMESPACE
202 #if defined (__MWERKS__) || defined(__BEOS__)
203 # define USE_SPRINTF_INSTEAD
206 #if defined (_AIX) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
207 // Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here
209 # if !defined(_REENTRANT) && !defined(_THREAD_SAFE) && !(defined(_POSIX_THREADS) && defined(__OpenBSD__))
211 # define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign);
213 static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
214 # define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex);
215 # define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf;
216 # endif // !_REENTRANT
217 #endif // _AIX || __FreeBSD__ || __NetBSD__ || __OpenBSD__
219 // Tests for infinity and NaN differ on different OSs. We encapsulate
220 // these differences here.
222 #if !defined (USE_SPRINTF_INSTEAD)
223 # if defined (__hpux) || defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
224 defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
225 static inline bool _Stl_is_nan_or_inf(double x)
226 # if defined (isfinite)
227 { return !isfinite(x); }
229 { return !finite(x); }
231 static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
232 static inline bool _Stl_is_inf(double x) { return isinf(x); }
233 // inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; }
234 static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; }
235 # elif (defined (__unix) || defined (__unix__)) && \
236 !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
238 static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
239 static inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); }
240 static inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); }
241 static inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); }
242 # elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 )
243 static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
244 static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! _isnan(x);}
245 static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
246 static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && x < 0 ; }
247 # elif defined (_STLP_MSVC_LIB) || (defined (__MINGW32__) && !(defined(__SYMBIAN32__) && defined(__GCCXML__)) ) || defined (__BORLANDC__)
248 static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
249 static inline bool _Stl_is_inf(double x) {
250 int fclass = _fpclass(x);
251 return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
253 static inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; }
254 static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; }
255 # elif defined (__MRC__) || defined (__SC__) //*TY 02/24/2000 - added support for MPW
256 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
257 static bool _Stl_is_inf(double x) { return !isfinite(x); }
258 static bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); }
259 static bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
260 # elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
261 static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
262 static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); }
263 static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
264 static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; }
265 # elif defined( _AIX ) // JFA 11-Aug-2000
266 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
267 static bool _Stl_is_inf(double x) { return !finite(x); }
268 // bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; }
269 static bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); }
270 static bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
271 # elif defined (__ISCPP__)
272 static inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
273 static inline bool _Stl_is_inf (double x) { return _fp_isINF(x); }
274 static inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; }
275 static inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; }
276 # elif defined (_CRAY)
277 # if defined (_CRAYIEEE)
278 static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
279 static inline bool _Stl_is_inf(double x) { return isinf(x); }
280 static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); }
281 static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
283 static inline bool _Stl_is_nan_or_inf(double x) { return false; }
284 static inline bool _Stl_is_inf(double x) { return false; }
285 static inline bool _Stl_is_neg_inf(double x) { return false; }
286 static inline bool _Stl_is_neg_nan(double x) { return false; }
288 # else // nothing from above
289 # define USE_SPRINTF_INSTEAD
291 #endif // !USE_SPRINTF_INSTEAD
293 #if !defined (USE_SPRINTF_INSTEAD)
294 // Reentrant versions of floating-point conversion functions. The argument
295 // lists look slightly different on different operating systems, so we're
296 // encapsulating the differences here.
298 # if defined (__CYGWIN__) || defined(__DJGPP)
299 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
300 { return ecvtbuf(x, n, pt, sign, buf); }
301 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
302 { return fcvtbuf(x, n, pt, sign, buf); }
303 # if !defined (_STLP_NO_LONG_DOUBLE)
304 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
305 { return ecvtbuf(x, n, pt, sign, buf); }
306 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
307 { return fcvtbuf(x, n, pt, sign, buf); }
309 # elif defined (_STLP_USE_GLIBC)
310 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
311 { return buf + ecvt_r(x, n, pt, sign, buf, NDIG+2); }
312 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
313 { return buf + fcvt_r(x, n, pt, sign, buf, NDIG+2); }
314 # if !defined (_STLP_NO_LONG_DOUBLE)
315 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
316 { return buf + qecvt_r(x, n, pt, sign, buf, NDIG+2); }
317 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
318 { return buf + qfcvt_r(x, n, pt, sign, buf, NDIG+2); }
320 # elif defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
321 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
322 { return ecvt(x, n, pt, sign); }
323 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
324 { return fcvt(x, n, pt, sign); }
325 # if !defined (_STLP_NO_LONG_DOUBLE)
326 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
327 { return ecvtl(x, n, pt, sign); }
328 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
329 { return fcvtl(x, n, pt, sign); }
331 # elif defined (__sun)
332 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
333 { return econvert(x, n, pt, sign, buf); }
334 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
335 { return fconvert(x, n, pt, sign, buf); }
336 # if !defined (_STLP_NO_LONG_DOUBLE)
337 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
338 { return qeconvert(&x, n, pt, sign, buf); }
339 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
340 { return qfconvert(&x, n, pt, sign, buf); }
342 # elif defined (__DECCXX)
343 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
344 { return (ecvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
345 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
346 { return (fcvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
347 # if !defined (_STLP_NO_LONG_DOUBLE)
348 // fbp : no "long double" conversions !
349 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
350 { return (ecvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0) ; }
351 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
352 { return (fcvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
354 # elif defined (__hpux)
355 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
356 { return ecvt(x, n, pt, sign); }
357 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
358 { return fcvt(x, n, pt, sign); }
359 # if !defined (_STLP_NO_LONG_DOUBLE)
361 # if defined( _REENTRANT ) && (defined(_PTHREADS_DRAFT4) || defined(PTHREAD_THREADS_MAX))
362 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
363 { return (_ldecvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
364 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
365 { return (_ldfcvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
367 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
368 { return _ldecvt(*(long_double*)&x, n, pt, sign); }
369 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
370 { return _ldfcvt(*(long_double*)&x, n, pt, sign); }
373 # elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
374 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
375 { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
376 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
377 { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
378 # if !defined (_STLP_NO_LONG_DOUBLE)
379 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
380 { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
381 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
382 { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
384 # elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
385 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
386 { return ecvt_r(x, n, pt, sign, buf); }
387 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
388 { return fcvt_r(x, n, pt, sign, buf); }
389 # if !defined (_STLP_NO_LONG_DOUBLE)
390 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
391 { return qecvt_r(x, n, pt, sign, buf); }
392 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
393 { return qfcvt_r(x, n, pt, sign, buf); }
395 # elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
396 // those guys claim _cvt functions being reentrant.
397 # if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
398 # define _STLP_APPEND(a, b) a##b
399 # define _STLP_BUF_PARAMS , char* buf, size_t bsize
400 # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
402 # define _STLP_CVT_DONT_NEED_BUF
403 # define _STLP_BUF_PARAMS
404 # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
406 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
407 { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
408 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
409 { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
410 # if !defined (_STLP_NO_LONG_DOUBLE)
411 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
412 { _STLP_SECURE_FUN(_ecvt, (double)x, n, pt, sign); }
413 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
414 { _STLP_SECURE_FUN(_fcvt, (double)x, n, pt, sign); }
416 # undef _STLP_SECURE_FUN
417 # undef _STLP_BUF_PARAMS
419 # elif defined (__ISCPP__)
420 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
421 { return _fp_ecvt( x, n, pt, sign, buf); }
423 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
424 { return _fp_fcvt(x, n, pt, sign, buf); }
426 # if !defined (_STLP_NO_LONG_DOUBLE)
427 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
428 { return _fp_ecvt( x, n, pt, sign, buf); }
430 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
431 { return _fp_fcvt(x, n, pt, sign, buf); }
433 # elif defined (__MRC__) || defined (__SC__) || defined (_CRAY)
434 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* )
435 { return ecvt( x, n, pt, sign ); }
436 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* )
437 { return fcvt(x, n, pt, sign); }
438 # if !defined (_STLP_NO_LONG_DOUBLE)
439 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* )
440 { return ecvt( x, n, pt, sign ); }
441 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* )
442 { return fcvt(x, n, pt, sign); }
446 # if defined (_STLP_CVT_DONT_NEED_BUF)
447 # define _STLP_CVT_BUFFER(B)
448 # elif !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
449 # define _STLP_CVT_BUFFER(B) , B
451 # define _STLP_CVT_BUFFER(B) , _STLP_ARRAY_AND_SIZE(B)
454 # if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
455 # define _STLP_BUFFER(B) B
457 # define _STLP_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
460 //----------------------------------------------------------------------
463 // __format_float formats a mantissa and exponent as returned by
464 // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
465 // according to the specified precision and format flags. This is
466 // based on doprnt but is much simpler since it is concerned only
467 // with floating point input and does not consider all formats. It
468 // also does not deal with blank padding, which is handled by
469 // __copy_float_and_fill.
471 static size_t __format_float_scientific( __iostring& buf, const char *bp,
472 int decpt, int sign, bool is_zero,
473 ios_base::fmtflags flags,
474 int precision, bool /* islong */)
479 else if (flags & ios_base::showpos)
482 // first digit of mantissa
485 // start of grouping position, grouping won't occur in scientific notation
486 // as it is impossible to have something like 1234.0e04 but we return a correct
487 // group position for coherency with __format_float_fixed.
488 size_t __group_pos = buf.size();
490 // decimal point if required
491 if (precision != 0 || flags & ios_base::showpoint) {
497 while (rz-- > 0 && *bp != 0)
501 char expbuf[MAXESIZ + 2];
502 char *suffix = expbuf + MAXESIZ;
508 for (; nn > 9; nn /= 10)
509 *--suffix = (char) todigit(nn % 10);
510 *--suffix = (char) todigit(nn);
513 // prepend leading zeros to exponent
514 while (suffix > &expbuf[MAXESIZ - 2])
517 // put in the exponent sign
518 *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
521 *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
528 static size_t __format_float_fixed( __iostring &buf, const char *bp,
529 int decpt, int sign, bool /* x */,
530 ios_base::fmtflags flags,
531 int precision, bool islong )
533 if ( sign && (decpt > -precision) && (*bp != 0) )
535 else if ( flags & ios_base::showpos )
539 int maxfsig = islong ? 2*MAXFSIG : MAXFSIG;
541 // digits before decimal point
544 buf += ((nnn <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++));
545 } while ( --nnn > 0 );
547 // start of grouping position
548 size_t __group_pos = buf.size();
550 // decimal point if needed
551 if ( flags & ios_base::showpoint || precision > 0 ) {
555 // digits after decimal point if any
556 nnn = (min) (precision, MAXFCVT);
558 while ( --nnn >= 0 ) {
559 buf += (++decpt <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++);
562 // trailing zeros if needed
563 if ( precision > MAXFCVT ) {
564 buf.append( precision - MAXFCVT, '0' );
570 static void __format_nan_or_inf(__iostring& buf, double x, ios_base::fmtflags flags)
572 static const char* inf[2] = { "inf", "Inf" };
573 static const char* nan[2] = { "nan", "NaN" };
574 const char** inf_or_nan;
575 if (_Stl_is_inf(x)) { // Infinity
577 if (_Stl_is_neg_inf(x))
579 else if (flags & ios_base::showpos)
583 if (_Stl_is_neg_nan(x))
585 else if (flags & ios_base::showpos)
588 buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
591 template <class max_double_type>
592 static inline size_t __format_float( __iostring &buf, const char * bp,
593 int decpt, int sign, max_double_type x,
594 ios_base::fmtflags flags,
595 int precision, bool islong)
597 size_t __group_pos = 0;
598 // Output of infinities and NANs does not depend on the format flags
599 if (_Stl_is_nan_or_inf((double)x)) { // Infinity or NaN
600 __format_nan_or_inf(buf, (double)x, flags);
601 } else { // representable number
602 switch (flags & ios_base::floatfield) {
603 case ios_base::scientific:
604 __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0.0,
605 flags, precision, islong);
607 case ios_base::fixed:
608 __group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
609 flags, precision, islong);
612 // establish default precision
613 if (flags & ios_base::showpoint || precision > 0) {
614 if (precision == 0) precision = 1;
618 // reset exponent if value is zero
623 if (!(flags & ios_base::showpoint)) {
624 size_t n = strlen(bp);
627 while (kk >= 1 && bp[kk-1] == '0')
631 if (decpt < -3 || decpt > precision) {
633 __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0,
634 flags, precision, islong);
636 precision = kk - decpt;
637 __group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
638 flags, precision, islong);
642 } /* else is_nan_or_inf */
646 #else /* USE_SPRINTF_INSTEAD */
649 bool operator () (char __c) const {
651 __c == 'e' || __c == 'E';
655 // Creates a format string for sprintf()
656 static int __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
660 if (flags & ios_base::showpos)
663 if (flags & ios_base::showpoint)
670 fmtbuf[i++] = long_modifier;
672 switch (flags & ios_base::floatfield)
674 case ios_base::scientific:
675 fmtbuf[i++] = (flags & ios_base::uppercase) ? 'E' : 'e';
677 case ios_base::fixed:
678 # if defined (__FreeBSD__)
681 fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
685 fmtbuf[i++] = (flags & ios_base::uppercase) ? 'G' : 'g';
693 #endif /* USE_SPRINTF_INSTEAD */
695 _STLP_DECLSPEC size_t _STLP_CALL
696 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
698 #if defined (USE_SPRINTF_INSTEAD)
699 /* If we want 'abitrary' precision, we should use 'abitrary' buffer size
702 char static_buf[128];
703 // char *static_buf = new char [128+precision];
705 __fill_fmtbuf(fmtbuf, flags, 0);
706 // snprintf(static_buf, 128+precision, fmtbuf, precision, x);
707 # if !defined (N_PLAT_NLM)
708 snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
710 sprintf(static_buf, fmtbuf, precision, x);
713 // delete [] static_buf;
714 return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
716 # if !defined (_STLP_CVT_DONT_NEED_BUF)
717 char cvtbuf[NDIG + 2];
722 switch (flags & ios_base::floatfield) {
723 case ios_base::fixed:
724 bp = _Stl_fcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
726 case ios_base::scientific :
727 bp = _Stl_ecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
730 bp = _Stl_ecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
733 return __format_float(buf, bp, decpt, sign, x, flags, precision, false);
737 #if !defined (_STLP_NO_LONG_DOUBLE)
738 _STLP_DECLSPEC size_t _STLP_CALL
739 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
741 # if defined (USE_SPRINTF_INSTEAD)
742 /* If we want 'abitrary' precision, we should use 'abitrary' buffer size
745 char static_buf[128];
746 // char *static_buf = new char [128+precision];
748 int i = __fill_fmtbuf(fmtbuf, flags, 'L');
749 // snprintf(static_buf, 128+precision, fmtbuf, precision, x);
750 # if !defined (N_PLAT_NLM)
751 snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
753 sprintf(static_buf, fmtbuf, precision, x);
755 // we should be able to return buf + sprintf(), but we do not trust'em...
757 // delete [] static_buf;
758 return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
760 # if !defined (_STLP_CVT_DONT_NEED_BUF)
761 char cvtbuf[NDIG + 2];
766 switch (flags & ios_base::floatfield) {
767 case ios_base::fixed:
768 bp = _Stl_qfcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
770 case ios_base::scientific:
771 bp = _Stl_qecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
774 bp = _Stl_qecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
777 return __format_float(buf, bp, decpt, sign, x, flags, precision, true);
778 # endif /* USE_SPRINTF_INSTEAD */
780 #endif /* _STLP_NO_LONG_DOUBLE */
782 _STLP_DECLSPEC void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
783 #if defined (USE_SPRINTF_INSTEAD)
785 # if !defined (_STLP_NO_LONG_DOUBLE)
786 # if !defined (N_PLAT_NLM)
787 snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
789 sprintf(cvtbuf, "%Lf", __x); // check for 1234.56!
792 snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x); // check for 1234.56!
794 char *p = strchr( cvtbuf, '.' );
796 out.append( cvtbuf );
798 out.append( cvtbuf, p );
801 # if !defined (_STLP_CVT_DONT_NEED_BUF)
802 char cvtbuf[NDIG + 2];
806 # if !defined (_STLP_NO_LONG_DOUBLE)
807 bp = _Stl_qfcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
809 bp = _Stl_fcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
815 out.append(bp, bp + decpt);
816 #endif // USE_PRINTF_INSTEAD
820 #if !defined (_STLP_NO_WCHAR_T)
821 _STLP_DECLSPEC void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
822 const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot)
824 string::const_iterator str_ite(str.begin()), str_end(str.end());
826 //First loop, check the dot char
828 while (str_ite != str_end) {
829 if (*str_ite != '.') {
830 out += ct.widen(*str_ite++);
837 if (str_ite != str_end) {
838 out += ct.widen(*str_ite);
842 if (str_ite != str_end) {
843 //Second loop, dot has been found, no check anymore
844 while (++str_ite != str_end) {
845 out += ct.widen(*str_ite);
853 __adjust_float_buffer(__iostring &str, char dot) {
855 size_t __dot_pos = str.find('.');
856 if (__dot_pos != string::npos) {
857 str[__dot_pos] = dot;
862 _STLP_MOVE_TO_STD_NAMESPACE