sl@0: /* sl@0: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. sl@0: * sl@0: * Copyright (c) 1999 sl@0: * Silicon Graphics Computer Systems, Inc. sl@0: * sl@0: * Copyright (c) 1999 sl@0: * Boris Fomitchev sl@0: * sl@0: * This material is provided "as is", with absolutely no warranty expressed sl@0: * or implied. Any use is at your own risk. sl@0: * sl@0: * Permission to use or copy this software for any purpose is hereby granted sl@0: * without fee, provided the above notices are retained on all copies. sl@0: * Permission to modify the code and to distribute modified code is granted, sl@0: * provided the above notices are retained, and a notice that the code was sl@0: * modified is included with the above copyright notice. sl@0: * sl@0: */ sl@0: sl@0: # include "stlport_prefix.h" sl@0: sl@0: # ifdef __DECCXX sl@0: #define NDIG 400 sl@0: # else sl@0: #define NDIG 82 sl@0: # endif sl@0: sl@0: # ifdef _STLP_NO_LONG_DOUBLE sl@0: # define MAXECVT 17 sl@0: # define MAXFCVT 18 sl@0: typedef double max_double_type; sl@0: # else sl@0: # define MAXECVT 35 sl@0: # define MAXFCVT 36 sl@0: typedef long double max_double_type; sl@0: # endif sl@0: sl@0: #define MAXFSIG MAXECVT sl@0: #define MAXESIZ 5 sl@0: sl@0: #define todigit(x) ((x)+'0') sl@0: sl@0: # include sl@0: sl@0: # ifdef _STLP_UNIX sl@0: sl@0: # if defined (__sun) sl@0: # include sl@0: # endif sl@0: sl@0: //# if !(defined(_STLP_USE_GLIBC) || defined(__FreeBSD__) || defined(__NetBSD__) || defined (_AIX) || defined(__MVS__) || defined (__OS400__) || defined (__QNXNTO__) || defined (__APPLE__) || defined (__DJGPP)) sl@0: # if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) sl@0: // DEC, SGI & Solaris need this sl@0: # include sl@0: # include sl@0: # endif sl@0: sl@0: # if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) sl@0: # define USE_SPRINTF_INSTEAD sl@0: # endif sl@0: sl@0: # if defined( _AIX ) // JFA 3-Aug-2000 sl@0: # include sl@0: # include sl@0: # endif sl@0: sl@0: # endif sl@0: sl@0: # include sl@0: sl@0: //#if defined(_CRAY) sl@0: //# include sl@0: //#endif sl@0: sl@0: #if defined (_MSC_VER) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) sl@0: # include sl@0: #endif sl@0: sl@0: #if defined(__MRC__) || defined(__SC__) || defined(_CRAY) //*TY 02/24/2000 - added support for MPW sl@0: # include sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #if defined( __MSL__ ) sl@0: # include // for atoi sl@0: # include // for snprintf sl@0: # include sl@0: # include sl@0: #endif sl@0: sl@0: #if defined (__ISCPP__) sl@0: # include sl@0: #endif sl@0: sl@0: sl@0: # include "num_put.h" sl@0: # include sl@0: sl@0: sl@0: #if defined(__hpux) && !defined(_INCLUDE_HPUX_SOURCE) sl@0: extern "C" double erf(double); sl@0: extern "C" double erfc(double); sl@0: extern "C" double gamma(double); /* obsolescent */ sl@0: extern "C" double hypot(double, double); sl@0: extern "C" int isnan(double); sl@0: extern "C" double j0(double); sl@0: extern "C" double j1(double); sl@0: extern "C" double jn(int, double); sl@0: extern "C" double lgamma(double); sl@0: extern "C" double y0(double); sl@0: extern "C" double y1(double); sl@0: extern "C" double yn(int, double); sl@0: sl@0: # define HUGE_VALF _SINFINITY sl@0: # define INFINITY _SINFINITY sl@0: # define NAN _SQNAN sl@0: sl@0: # define isnan(x) _ISNAN(x) sl@0: # define isinf(x) _ISINF(x) sl@0: # define signbit(x) _SIGNBIT(x) sl@0: # define isfinite(x) _ISFINITE(x) sl@0: # define isnormal(x) _ISNORMAL(x) sl@0: # define fpclassify(x) _FPCLASSIFY(x) sl@0: # define isunordered(x,y) _ISUNORDERED(x,y) sl@0: # define isgreater(x,y) _ISGREATER(x,y) sl@0: # define isgreaterequal(x,y) _ISGREATEREQUAL(x,y) sl@0: # define isless(x,y) _ISLESS(x,y) sl@0: # define islessequal(x,y) _ISLESSEQUAL(x,y) sl@0: # define islessgreater(x,y) _ISLESSGREATER(x,y) sl@0: sl@0: # define FP_NORMAL 0 sl@0: # define FP_ZERO 1 sl@0: # define FP_INFINITE 2 sl@0: # define FP_SUBNORMAL 3 sl@0: # define FP_NAN 4 sl@0: sl@0: # define DECIMAL_DIG 17 sl@0: sl@0: # define _IS64(x) (sizeof(x)==sizeof(double)) sl@0: # define _IS32(x) (sizeof(x)==sizeof(float)) sl@0: sl@0: extern "C" { sl@0: extern double copysign(double, double); sl@0: extern const float _SINFINITY; sl@0: extern const float _SQNAN; sl@0: # ifdef _PA_RISC sl@0: # define _ISNAN(x) (_IS32(x)?_Isnanf(x):(isnan)(x)) sl@0: # define _ISINF(x) (_IS32(x)?_Isinff(x):_Isinf(x)) sl@0: # define _SIGNBIT(x) (_IS32(x)?_Signbitf(x):_Signbit(x)) sl@0: # define _ISFINITE(x) (_IS32(x)?_Isfinitef(x):_Isfinite(x)) sl@0: # define _ISNORMAL(x) (_IS32(x)?_Isnormalf(x):_Isnormal(x)) sl@0: # define _FPCLASSIFY(x) (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1) sl@0: # define _ISUNORDERED(x,y) (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y)) sl@0: extern int _Signbit(double); sl@0: extern int _Signbitf(float); sl@0: extern int _Isnanf(float); sl@0: extern int _Isfinite(double); sl@0: extern int _Isfinitef(float); sl@0: extern int _Isinf(double); sl@0: extern int _Isinff(float); sl@0: extern int _Isnormal(double); sl@0: extern int _Isnormalf(float); sl@0: extern int _Isunordered(double, double); sl@0: extern int _Isunorderedf(float, float); sl@0: extern int _Fpclassify(double); sl@0: extern int _Fpclassifyf(float); sl@0: # else sl@0: # include "math_ia64_internal.h" sl@0: # define _FPCLASSIFY(x) (_IS32(x)?_Fpclassf(x):_Fpclass(x)) sl@0: extern int _Fpclass(double); sl@0: extern int _Fpclassf(float); sl@0: # endif sl@0: } sl@0: sl@0: #ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED sl@0: extern "C" char *fcvt(double, int, int *, int *); sl@0: extern "C" char *ecvt(double, int, int *, int *); sl@0: #endif sl@0: #ifndef _INCLUDE_HPUX_SOURCE sl@0: # ifndef _LONG_DOUBLE sl@0: # define _LONG_DOUBLE sl@0: typedef struct { sl@0: uint32_t word1, word2, word3, word4; sl@0: } long_double; sl@0: # endif /* _LONG_DOUBLE */ sl@0: extern "C" char *_ldecvt(long_double, int, int *, int *); sl@0: extern "C" char *_ldfcvt(long_double, int, int *, int *); sl@0: sl@0: #endif sl@0: sl@0: #endif /* __hpux */ sl@0: sl@0: _STLP_BEGIN_NAMESPACE sl@0: sl@0: #if defined (__MWERKS__) || defined(__BEOS__) sl@0: # define USE_SPRINTF_INSTEAD sl@0: #endif sl@0: sl@0: # if defined (_AIX) sl@0: // Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here sl@0: # ifdef _REENTRANT sl@0: static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER; sl@0: # define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex); sl@0: # define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf; sl@0: # else sl@0: # define LOCK_CVT sl@0: # define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign); sl@0: # endif sl@0: # endif sl@0: sl@0: // Tests for infinity and NaN differ on different OSs. We encapsulate sl@0: // these differences here. sl@0: sl@0: #ifdef USE_SPRINTF_INSTEAD sl@0: sl@0: #elif defined (__hpux) || defined (__DJGPP) || ( defined(_STLP_USE_GLIBC) && ! defined (__MSL__) ) sl@0: # if defined (isfinite) sl@0: inline bool _Stl_is_nan_or_inf(double x) { return !isfinite(x); } sl@0: # else sl@0: inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); } sl@0: # endif sl@0: inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); } sl@0: inline bool _Stl_is_inf(double x) { return isinf(x); } sl@0: // inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; } sl@0: inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; } sl@0: #elif defined(__unix) && !defined(__FreeBSD__) && !defined(__NetBSD__) \ sl@0: && !defined(__APPLE__) && !defined(__DJGPP) && !defined(__osf__) \ sl@0: && !defined(_CRAY) sl@0: inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); } sl@0: inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); } sl@0: inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); } sl@0: inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); } sl@0: # elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 ) sl@0: inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); } sl@0: inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! _isnan(x);} sl@0: inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; } sl@0: inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && x < 0 ; } sl@0: #elif defined (_MSC_VER) || (defined (__MINGW32__) && !defined (__SYMBIAN32__)) || defined (__BORLANDC__) sl@0: inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); } sl@0: inline bool _Stl_is_inf(double x) { sl@0: int fclass = _fpclass(x); sl@0: return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF; sl@0: } sl@0: inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; } sl@0: inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; } sl@0: #elif defined(__MRC__) || defined(__SC__) //*TY 02/24/2000 - added support for MPW sl@0: bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); } sl@0: bool _Stl_is_inf(double x) { return !isfinite(x); } sl@0: bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); } sl@0: bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); } sl@0: #elif /* defined (__FreeBSD__) */ ( defined (__GNUC__) && defined (__APPLE__) ) sl@0: inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); } sl@0: inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); } sl@0: inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; } sl@0: inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; } sl@0: #elif defined( _AIX ) // JFA 11-Aug-2000 sl@0: bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); } sl@0: bool _Stl_is_inf(double x) { return !finite(x); } sl@0: // bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; } sl@0: bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); } sl@0: bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); } sl@0: #elif defined (__ISCPP__) sl@0: inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); } sl@0: inline bool _Stl_is_inf (double x) { return _fp_isINF(x); } sl@0: inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; } sl@0: inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; } sl@0: #elif defined(_CRAY) sl@0: #if defined(_CRAYIEEE) sl@0: inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); } sl@0: inline bool _Stl_is_inf(double x) { return isinf(x); } sl@0: inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); } sl@0: inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); } sl@0: #else sl@0: inline bool _Stl_is_nan_or_inf(double x) { return false; } sl@0: inline bool _Stl_is_inf(double x) { return false; } sl@0: inline bool _Stl_is_neg_inf(double x) { return false; } sl@0: inline bool _Stl_is_neg_nan(double x) { return false; } sl@0: #endif sl@0: #elif ! defined (USE_SPRINTF_INSTEAD) sl@0: # define USE_SPRINTF_INSTEAD sl@0: #endif sl@0: sl@0: sl@0: # ifndef USE_SPRINTF_INSTEAD sl@0: // Reentrant versions of floating-point conversion functions. The argument sl@0: // lists look slightly different on different operating systems, so we're sl@0: // encapsulating the differences here. sl@0: sl@0: #if defined (__CYGWIN__) || defined(__DJGPP) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvtbuf(x, n, pt, sign, buf); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvtbuf(x, n, pt, sign, buf); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvtbuf(x, n, pt, sign, buf); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvtbuf(x, n, pt, sign, buf); } sl@0: # endif sl@0: #elif defined (_STLP_USE_GLIBC) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + ecvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + fcvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + qecvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + qfcvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: # endif sl@0: #elif defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvt(x, n, pt, sign); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvt(x, n, pt, sign); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvtl(x, n, pt, sign); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvtl(x, n, pt, sign); } sl@0: # endif sl@0: #elif defined (__sun) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return econvert(x, n, pt, sign, buf); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fconvert(x, n, pt, sign, buf); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qeconvert(&x, n, pt, sign, buf); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qfconvert(&x, n, pt, sign, buf); } sl@0: # endif sl@0: #elif defined (__DECCXX) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return (ecvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return (fcvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: // fbp : no "long double" conversions ! sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (ecvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0) ; } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (fcvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0); } sl@0: # endif sl@0: #elif defined (__hpux) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvt(x, n, pt, sign); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvt(x, n, pt, sign); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: sl@0: # if defined( _REENTRANT ) && (defined(_PTHREADS_DRAFT4) || defined(PTHREAD_THREADS_MAX)) sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (_ldecvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (_ldfcvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); } sl@0: # else sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _ldecvt(*(long_double*)&x, n, pt, sign); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _ldfcvt(*(long_double*)&x, n, pt, sign); } sl@0: # endif sl@0: # endif sl@0: #elif defined (_AIX) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) } sl@0: # endif sl@0: #elif defined (__unix) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__APPLE__) && !defined(_CRAY) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvt_r(x, n, pt, sign, buf); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvt_r(x, n, pt, sign, buf); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qecvt_r(x, n, pt, sign, buf); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qfcvt_r(x, n, pt, sign, buf); } sl@0: # endif sl@0: #elif defined (_MSC_VER) || defined (__MINGW32__) || defined (__BORLANDC__) sl@0: // those guys claim _cvt functions being reentrant. sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) { return _ecvt(x, n, pt, sign); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) { return _fcvt(x, n, pt, sign); } sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) { return _ecvt((double)x, n, pt, sign); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) { return _fcvt((double)x, n, pt, sign); } sl@0: # endif sl@0: #elif defined (__ISCPP__) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_ecvt( x, n, pt, sign, buf); } sl@0: sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_fcvt(x, n, pt, sign, buf); } sl@0: sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_ecvt( x, n, pt, sign, buf); } sl@0: sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_fcvt(x, n, pt, sign, buf); } sl@0: # endif sl@0: #elif defined (__MRC__) || defined(__SC__) || defined(_CRAY) sl@0: inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* ) sl@0: { return ecvt( x, n, pt, sign ); } sl@0: inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* ) sl@0: { return fcvt(x, n, pt, sign); } sl@0: #ifndef _STLP_NO_LONG_DOUBLE sl@0: inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* ) sl@0: { return ecvt( x, n, pt, sign ); } sl@0: inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* ) sl@0: { return fcvt(x, n, pt, sign); } sl@0: #endif sl@0: #endif sl@0: sl@0: sl@0: //---------------------------------------------------------------------- sl@0: // num_put sl@0: sl@0: // Helper functions for _M_do_put_float sl@0: sl@0: // __format_float formats a mantissa and exponent as returned by sl@0: // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r) sl@0: // according to the specified precision and format flags. This is sl@0: // based on doprnt but is much simpler since it is concerned only sl@0: // with floating point input and does not consider all formats. It sl@0: // also does not deal with blank padding, which is handled by sl@0: // __copy_float_and_fill. sl@0: sl@0: void __format_float_scientific(char * buf, const char * bp, sl@0: int decpt, int sign, bool is_zero, sl@0: ios_base::fmtflags flags, sl@0: int precision, bool /* islong */) sl@0: { sl@0: char * suffix; sl@0: char expbuf[MAXESIZ + 2]; sl@0: // sign if required sl@0: if (sign) sl@0: *buf++ = '-'; sl@0: else if (flags & ios_base::showpos) sl@0: *buf++ = '+'; sl@0: sl@0: // first digit of mantissa sl@0: *buf++ = *bp++; sl@0: sl@0: // decimal point if required sl@0: if (precision != 0 || flags & ios_base::showpoint) sl@0: *buf++ = '.'; sl@0: // rest of mantissa sl@0: int rz = precision; sl@0: while (rz-- > 0 && *bp != 0) sl@0: *buf++ = *bp++; sl@0: sl@0: // exponent sl@0: *(suffix = &expbuf[MAXESIZ]) = 0; sl@0: if (!is_zero) { sl@0: int nn = decpt - 1; sl@0: if (nn < 0) sl@0: nn = -nn; sl@0: for (; nn > 9; nn /= 10) sl@0: *--suffix = (char) todigit(nn % 10); sl@0: *--suffix = (char) todigit(nn); sl@0: } sl@0: sl@0: // prepend leading zeros to exponent sl@0: while (suffix > &expbuf[MAXESIZ - 2]) sl@0: *--suffix = '0'; sl@0: sl@0: // put in the exponent sign sl@0: *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-'); sl@0: sl@0: // put in the e sl@0: *--suffix = flags & ios_base::uppercase ? 'E' : 'e'; sl@0: sl@0: // copy the suffix sl@0: strcpy(buf, suffix); sl@0: } sl@0: sl@0: static inline sl@0: void __flush_static_buf(string &buf, sl@0: char *sbeginbuf, char *sendbuf, char *&scurpos) { sl@0: if (scurpos == sendbuf) { sl@0: buf.append(sbeginbuf, sendbuf); sl@0: scurpos = sbeginbuf; sl@0: } sl@0: } sl@0: sl@0: void __format_float_fixed(string &buf, const char * bp, sl@0: int decpt, int sign, bool /* x */, sl@0: ios_base::fmtflags flags, sl@0: int precision, bool islong ) sl@0: { sl@0: char static_buf[128]; sl@0: char *sbuf = static_buf; sl@0: char *sendbuf = static_buf + sizeof(static_buf); sl@0: sl@0: if (sign && decpt > -precision && *bp != 0) sl@0: *sbuf++ = '-'; sl@0: else if (flags & ios_base::showpos) sl@0: *sbuf++ = '+'; sl@0: sl@0: int rzero = 0; sl@0: int nn = decpt; sl@0: int k = 0; sl@0: int maxfsig = islong ? 2*MAXFSIG : MAXFSIG; sl@0: sl@0: do { sl@0: *sbuf++ = (char) ((nn <= 0 || *bp == 0 || k >= maxfsig) ? sl@0: '0' : (k++, *bp++)); sl@0: __flush_static_buf(buf, static_buf, sendbuf, sbuf); sl@0: } while (--nn > 0); sl@0: sl@0: // decimal point if needed sl@0: if (flags & ios_base::showpoint || precision > 0) { sl@0: *sbuf++ = '.'; sl@0: __flush_static_buf(buf, static_buf, sendbuf, sbuf); sl@0: } sl@0: sl@0: // digits after decimal point if any sl@0: nn = (min) (precision, MAXFCVT); sl@0: if (precision > nn) sl@0: rzero = precision - nn; sl@0: while (--nn >= 0) { sl@0: *sbuf++ = (++decpt <= 0 || *bp == '\0' || k >= maxfsig) sl@0: ? '0' : (k++, *bp++); sl@0: __flush_static_buf(buf, static_buf, sendbuf, sbuf); sl@0: } sl@0: sl@0: // trailing zeros if needed sl@0: while (rzero-- > 0) { sl@0: *sbuf++ = '0'; sl@0: __flush_static_buf(buf, static_buf, sendbuf, sbuf); sl@0: } sl@0: buf.append(static_buf, sbuf); sl@0: } sl@0: sl@0: sl@0: void __format_nan_or_inf(char * buf, double x, sl@0: ios_base::fmtflags flags) sl@0: { sl@0: static const char* inf[2] = { "inf", "Inf" }; sl@0: static const char* nan[2] = { "nan", "NaN" }; sl@0: const char** inf_or_nan = 0; sl@0: if (_Stl_is_inf((double)x)) { // Infinity sl@0: inf_or_nan = inf; sl@0: if (_Stl_is_neg_inf((double)x)) sl@0: *buf++ = '-'; sl@0: else if (flags & ios_base::showpos) sl@0: *buf++ = '+'; sl@0: } sl@0: else { // NaN sl@0: inf_or_nan = nan; sl@0: if (_Stl_is_neg_nan((double)x)) sl@0: *buf++ = '-'; sl@0: else if (flags & ios_base::showpos) sl@0: *buf++ = '+'; sl@0: } sl@0: strcpy(buf, flags & ios_base::uppercase ? inf_or_nan[1] : inf_or_nan[0]); sl@0: } sl@0: sl@0: template sl@0: static inline sl@0: void __format_float(string &buf, const char * bp, sl@0: int decpt, int sign, max_double_type x, sl@0: ios_base::fmtflags flags, sl@0: int precision, bool islong) sl@0: { sl@0: char static_buf[128]; sl@0: // Output of infinities and NANs does not depend on the format flags sl@0: if (_Stl_is_nan_or_inf((double)x)) { // Infinity or NaN sl@0: __format_nan_or_inf(static_buf, x, flags); sl@0: buf = static_buf; sl@0: } sl@0: else { // representable number sl@0: switch (flags & ios_base::floatfield) { sl@0: case ios_base::scientific: sl@0: __format_float_scientific(static_buf, bp, decpt, sign, x == 0.0, flags, sl@0: precision, islong); sl@0: buf = static_buf; sl@0: break; sl@0: sl@0: case ios_base::fixed: sl@0: __format_float_fixed(buf, bp, decpt, sign, true, flags, sl@0: precision, islong); sl@0: break; sl@0: sl@0: default: // g format sl@0: // establish default precision sl@0: if (flags & ios_base::showpoint || precision > 0) { sl@0: if (precision == 0) precision = 1; sl@0: } sl@0: else sl@0: precision = 6; sl@0: sl@0: // reset exponent if value is zero sl@0: if (x == 0) sl@0: decpt = 1; sl@0: sl@0: int kk = precision; sl@0: if (!(flags & ios_base::showpoint)) { sl@0: size_t n = strlen(bp); sl@0: if (n < kk) sl@0: kk = (int)n; sl@0: while (kk >= 1 && bp[kk-1] == '0') sl@0: --kk; sl@0: } sl@0: sl@0: if (decpt < -3 || decpt > precision) { sl@0: precision = kk - 1; sl@0: __format_float_scientific(static_buf, bp, decpt, sign, x == 0, sl@0: flags, precision, islong); sl@0: buf = static_buf; sl@0: } sl@0: else { sl@0: precision = kk - decpt; sl@0: __format_float_fixed(buf, bp, decpt, sign, true, sl@0: flags, precision, islong); sl@0: } sl@0: break; sl@0: } /* switch */ sl@0: } /* else */ sl@0: } sl@0: sl@0: # else sl@0: // Creates a format string for sprintf() sl@0: static int fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) sl@0: { sl@0: fmtbuf[0] = '%'; sl@0: int i = 1; sl@0: sl@0: if (flags & ios_base::showpos) sl@0: fmtbuf[i++] = '+'; sl@0: sl@0: if (flags & ios_base::showpoint) sl@0: fmtbuf[i++] = '#'; sl@0: sl@0: fmtbuf[i++] = '.'; sl@0: fmtbuf[i++] = '*'; sl@0: sl@0: if (long_modifier) sl@0: fmtbuf[i++] = long_modifier; sl@0: sl@0: switch (flags & ios_base::floatfield) sl@0: { sl@0: case ios_base::scientific: sl@0: fmtbuf[i++] = (flags & ios_base::uppercase) ? 'E' : 'e'; sl@0: break; sl@0: case ios_base::fixed: sl@0: #if defined (__FreeBSD__) sl@0: fmtbuf[i++] = 'f'; sl@0: #else sl@0: fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f'; sl@0: #endif sl@0: break; sl@0: default: sl@0: fmtbuf[i++] = (flags & ios_base::uppercase) ? 'G' : 'g'; sl@0: break; sl@0: } sl@0: sl@0: fmtbuf[i] = 0; sl@0: return i; sl@0: } sl@0: # endif /* USE_SPRINTF_INSTEAD */ sl@0: sl@0: sl@0: _STLP_EXP_DECLSPEC void _STLP_CALL sl@0: __write_float(string &buf, ios_base::fmtflags flags, int precision, sl@0: double x) sl@0: { sl@0: # ifdef USE_SPRINTF_INSTEAD sl@0: char static_buf[256+10];//+2 - 10/1/07 sl@0: char fmtbuf[32]; sl@0: fill_fmtbuf(fmtbuf, flags, 0); sl@0: sprintf(static_buf, fmtbuf, precision, x); sl@0: // we should be able to return static_buf + sprintf(), but we do not trust'em... sl@0: buf = static_buf; sl@0: # else sl@0: char cvtbuf[NDIG+2]; sl@0: char * bp; sl@0: int decpt, sign; sl@0: sl@0: switch (flags & ios_base::floatfield) { sl@0: case ios_base::fixed: sl@0: bp = _Stl_fcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign, cvtbuf); sl@0: break; sl@0: case ios_base::scientific : sl@0: bp = _Stl_ecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign, cvtbuf); sl@0: break; sl@0: default : sl@0: bp = _Stl_ecvtR(x, (min) (precision, MAXECVT), &decpt, &sign, cvtbuf); sl@0: break; sl@0: } sl@0: __format_float(buf, bp, decpt, sign, x, flags, precision, false); sl@0: # endif sl@0: sl@0: } sl@0: sl@0: # ifndef _STLP_NO_LONG_DOUBLE sl@0: _STLP_EXP_DECLSPEC void _STLP_CALL sl@0: __write_float(string &buf, ios_base::fmtflags flags, int precision, sl@0: long double x) sl@0: { sl@0: # ifdef USE_SPRINTF_INSTEAD sl@0: char static_buf[128]; sl@0: char fmtbuf[64]; sl@0: int i = fill_fmtbuf(fmtbuf, flags, 'L'); sl@0: sprintf(static_buf, fmtbuf, precision, x); sl@0: // we should be able to return buf + sprintf(), but we do not trust'em... sl@0: buf = static_buf; sl@0: # else sl@0: char cvtbuf[NDIG+2]; sl@0: char * bp; sl@0: int decpt, sign; sl@0: sl@0: switch (flags & ios_base::floatfield) { sl@0: case ios_base::fixed: sl@0: bp = _Stl_qfcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign, cvtbuf); sl@0: break; sl@0: case ios_base::scientific : sl@0: bp = _Stl_qecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign, cvtbuf); sl@0: break; sl@0: default : sl@0: bp = _Stl_qecvtR(x, (min) (precision, MAXECVT), &decpt, &sign, cvtbuf); sl@0: break; sl@0: } sl@0: __format_float(buf, bp, decpt, sign, x, flags, precision, true); sl@0: # endif sl@0: } sl@0: # endif sl@0: sl@0: sl@0: # ifndef _STLP_NO_WCHAR_T sl@0: sl@0: _STLP_EXP_DECLSPEC wchar_t* _STLP_CALL sl@0: __convert_float_buffer(const char* first, const char* last, wchar_t* out, sl@0: const ctype& ct, wchar_t dot) sl@0: { sl@0: ct.widen(first, last, out); sl@0: if (ct.widen('.') != dot) sl@0: replace(out, out + (last - first), ct.widen('.'), dot); sl@0: return out + (last - first); sl@0: } sl@0: sl@0: # endif sl@0: sl@0: _STLP_EXP_DECLSPEC void _STLP_CALL sl@0: __adjust_float_buffer(char* first, char* last, char dot) sl@0: { sl@0: if ('.' != dot) sl@0: replace(first, last, '.', dot); sl@0: } sl@0: sl@0: _STLP_END_NAMESPACE sl@0: sl@0: // Local Variables: sl@0: // mode:C++ sl@0: // End: