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