num_put_float.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  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.
  16. *
  17. */
  18. #include "stlport_prefix.h"
  19. #include <cmath>
  20. #include <ios>
  21. #include <locale>
  22. #if defined (__DECCXX)
  23. # define NDIG 400
  24. #else
  25. # define NDIG 82
  26. #endif
  27. #define todigit(x) ((x)+'0')
  28. #if defined (_STLP_UNIX)
  29. # if defined (__sun)
  30. # include <floatingpoint.h>
  31. # endif
  32. # if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
  33. // DEC, SGI & Solaris need this
  34. # include <values.h>
  35. # include <nan.h>
  36. # endif
  37. # if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
  38. defined(__FreeBSD__)
  39. # define USE_SPRINTF_INSTEAD
  40. # endif
  41. # if defined (_AIX) // JFA 3-Aug-2000
  42. # include <math.h>
  43. # include <float.h>
  44. # endif
  45. # include <math.h>
  46. #endif
  47. #include <cstdio>
  48. #include <cstdlib>
  49. #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
  50. defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
  51. # include <float.h>
  52. #endif
  53. #if defined (__MRC__) || defined (__SC__) || defined (_CRAY) //*TY 02/24/2000 - added support for MPW
  54. # include <fp.h>
  55. #endif
  56. #if defined (__CYGWIN__)
  57. # include <ieeefp.h>
  58. #endif
  59. #if defined (__MSL__)
  60. # include <cstdlib> // for atoi
  61. # include <cstdio> // for snprintf
  62. # include <algorithm>
  63. # include <cassert>
  64. #endif
  65. #if defined (__ISCPP__)
  66. # include <cfloat>
  67. #endif
  68. #include <algorithm>
  69. #if defined (__DMC__)
  70. # define snprintf _snprintf
  71. #endif
  72. _STLP_BEGIN_NAMESPACE
  73. _STLP_MOVE_TO_PRIV_NAMESPACE
  74. #if defined (__MWERKS__) || defined(__BEOS__)
  75. # define USE_SPRINTF_INSTEAD
  76. #endif
  77. template <int N>
  78. struct _Dig
  79. {
  80. enum { dig = _Dig<N/10>::dig + 1 };
  81. };
  82. _STLP_TEMPLATE_NULL
  83. struct _Dig<0>
  84. {
  85. enum { dig = 0 };
  86. };
  87. #ifdef _STLP_NO_LONG_DOUBLE
  88. # define MAXEDIGITS int(_Dig<DBL_MAX_10_EXP>::dig)
  89. # define MAXFSIG DBL_DIG
  90. # define MAXFCVT (DBL_DIG + 1)
  91. #else
  92. # define MAXEDIGITS int(_Dig<LDBL_MAX_10_EXP>::dig)
  93. # define MAXFSIG LDBL_DIG
  94. # define MAXFCVT (LDBL_DIG + 1)
  95. #endif
  96. // Tests for infinity and NaN differ on different OSs. We encapsulate
  97. // these differences here.
  98. #if !defined (USE_SPRINTF_INSTEAD)
  99. # if defined (__hpux) && defined (__GNUC__)
  100. # define _STLP_USE_SIGN_HELPER
  101. # elif defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
  102. defined (__CYGWIN__) || \
  103. defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
  104. defined (__HP_aCC)
  105. static inline bool _Stl_is_nan_or_inf(double x)
  106. # if defined (isfinite)
  107. { return !isfinite(x); }
  108. # else
  109. { return !finite(x); }
  110. # endif
  111. static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
  112. static inline bool _Stl_is_inf(double x) { return isinf(x); }
  113. // inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; }
  114. static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; }
  115. # elif (defined (__unix) || defined (__unix__)) && \
  116. !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
  117. !defined (_CRAY)
  118. static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
  119. static inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); }
  120. static inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); }
  121. static inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); }
  122. # elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
  123. static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
  124. # if !defined (__BORLANDC__)
  125. static inline bool _Stl_is_inf(double x) {
  126. int fclass = _fpclass(x);
  127. return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
  128. }
  129. static inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; }
  130. # else
  131. static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && !_isnan(x);}
  132. static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
  133. # endif
  134. static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; }
  135. # if defined (__BORLANDC__)
  136. static inline bool _Stl_is_nan_or_inf(long double x) { return !_finitel(x); }
  137. static inline bool _Stl_is_inf(long double x) { return _Stl_is_nan_or_inf(x) && !_isnanl(x);}
  138. static inline bool _Stl_is_neg_inf(long double x) { return _Stl_is_inf(x) && x < 0 ; }
  139. static inline bool _Stl_is_neg_nan(long double x) { return _isnanl(x) && _copysignl(1.l, x) < 0 ; }
  140. # elif !defined (_STLP_NO_LONG_DOUBLE)
  141. // Simply there to avoid warning long double -> double implicit conversion:
  142. static inline bool _Stl_is_nan_or_inf(long double x) { return _Stl_is_nan_or_inf(__STATIC_CAST(double, x)); }
  143. static inline bool _Stl_is_inf(long double x) { return _Stl_is_inf(__STATIC_CAST(double, x));}
  144. static inline bool _Stl_is_neg_inf(long double x) { return _Stl_is_neg_inf(__STATIC_CAST(double, x)); }
  145. static inline bool _Stl_is_neg_nan(long double x) { return _Stl_is_neg_nan(__STATIC_CAST(double, x)); }
  146. # endif
  147. # elif defined (__MRC__) || defined (__SC__) || defined (__DMC__)
  148. static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
  149. static bool _Stl_is_inf(double x) { return !isfinite(x); }
  150. static bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); }
  151. static bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
  152. # elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
  153. static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
  154. static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); }
  155. static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
  156. static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; }
  157. # elif defined( _AIX ) // JFA 11-Aug-2000
  158. static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
  159. static bool _Stl_is_inf(double x) { return !finite(x); }
  160. // bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; }
  161. static bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); }
  162. static bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
  163. # elif defined (__ISCPP__)
  164. static inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
  165. static inline bool _Stl_is_inf (double x) { return _fp_isINF(x); }
  166. static inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; }
  167. static inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; }
  168. # elif defined (_CRAY)
  169. # if defined (_CRAYIEEE)
  170. static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
  171. static inline bool _Stl_is_inf(double x) { return isinf(x); }
  172. static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); }
  173. static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
  174. # else
  175. static inline bool _Stl_is_nan_or_inf(double x) { return false; }
  176. static inline bool _Stl_is_inf(double x) { return false; }
  177. static inline bool _Stl_is_neg_inf(double x) { return false; }
  178. static inline bool _Stl_is_neg_nan(double x) { return false; }
  179. # endif
  180. # else // nothing from above
  181. # define USE_SPRINTF_INSTEAD
  182. # endif
  183. #endif // !USE_SPRINTF_INSTEAD
  184. #if !defined (USE_SPRINTF_INSTEAD)
  185. // Reentrant versions of floating-point conversion functions. The argument
  186. // lists look slightly different on different operating systems, so we're
  187. // encapsulating the differences here.
  188. # if defined (__CYGWIN__) || defined(__DJGPP)
  189. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
  190. { return ecvtbuf(x, n, pt, sign, buf); }
  191. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
  192. { return fcvtbuf(x, n, pt, sign, buf); }
  193. # if !defined (_STLP_NO_LONG_DOUBLE)
  194. # if defined (__CYGWIN__)
  195. # define _STLP_EMULATE_LONG_DOUBLE_CVT
  196. # else
  197. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
  198. { return ecvtbuf(x, n, pt, sign, buf); }
  199. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
  200. { return fcvtbuf(x, n, pt, sign, buf); }
  201. # endif
  202. # endif
  203. # elif defined (_STLP_USE_GLIBC)
  204. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  205. { return ecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
  206. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  207. { return fcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
  208. # ifndef _STLP_NO_LONG_DOUBLE
  209. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  210. { return qecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
  211. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  212. { return qfcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
  213. # endif
  214. # define _STLP_NEED_CVT_BUFFER_SIZE
  215. # elif defined (__sun)
  216. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
  217. { return econvert(x, n, pt, sign, buf); }
  218. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
  219. { return fconvert(x, n, pt, sign, buf); }
  220. # ifndef _STLP_NO_LONG_DOUBLE
  221. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
  222. { return qeconvert(&x, n, pt, sign, buf); }
  223. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
  224. { return qfconvert(&x, n, pt, sign, buf); }
  225. # endif
  226. # elif defined (__DECCXX)
  227. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  228. { return (ecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0); }
  229. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  230. { return (fcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0); }
  231. # ifndef _STLP_NO_LONG_DOUBLE
  232. // fbp : no "long double" conversions !
  233. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  234. { return (ecvt_r((double)x, n, pt, sign, buf, bsize) == 0 ? buf : 0) ; }
  235. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
  236. { return (fcvt_r((double)x, n, pt, sign, buf, bsize) == 0 ? buf : 0); }
  237. # endif
  238. # define _STLP_NEED_CVT_BUFFER_SIZE
  239. # elif defined (__hpux)
  240. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
  241. { return ecvt(x, n, pt, sign); }
  242. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
  243. { return fcvt(x, n, pt, sign); }
  244. # if !defined (_STLP_NO_LONG_DOUBLE)
  245. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
  246. { return _ldecvt(*(long_double*)&x, n, pt, sign); }
  247. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
  248. { return _ldfcvt(*(long_double*)&x, n, pt, sign); }
  249. # endif
  250. # define _STLP_CVT_NEED_SYNCHRONIZATION
  251. # elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
  252. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
  253. { return ecvt_r(x, n, pt, sign, buf); }
  254. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
  255. { return fcvt_r(x, n, pt, sign, buf); }
  256. # if !defined (_STLP_NO_LONG_DOUBLE)
  257. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
  258. { return qecvt_r(x, n, pt, sign, buf); }
  259. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
  260. { return qfcvt_r(x, n, pt, sign, buf); }
  261. # endif
  262. # elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
  263. # if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
  264. # define _STLP_APPEND(a, b) a##b
  265. # define _STLP_BUF_PARAMS , char* buf, size_t bsize
  266. # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
  267. # else
  268. # define _STLP_BUF_PARAMS
  269. # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
  270. # define _STLP_CVT_NEED_SYNCHRONIZATION
  271. # endif
  272. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
  273. { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
  274. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
  275. { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
  276. # if !defined (_STLP_NO_LONG_DOUBLE)
  277. # if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
  278. # define _STLP_PARAMS , buf, bsize
  279. # else
  280. # define _STLP_PARAMS
  281. # endif
  282. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
  283. { return _Stl_ecvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
  284. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
  285. { return _Stl_fcvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
  286. # undef _STLP_PARAMS
  287. # endif
  288. # undef _STLP_SECURE_FUN
  289. # undef _STLP_BUF_PARAMS
  290. # undef _STLP_APPEND
  291. # if defined (__BORLANDC__) /* || defined (__GNUC__) MinGW do not support 'L' modifier so emulation do not work */
  292. # define _STLP_EMULATE_LONG_DOUBLE_CVT
  293. # endif
  294. # elif defined (__ISCPP__)
  295. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
  296. { return _fp_ecvt( x, n, pt, sign, buf); }
  297. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
  298. { return _fp_fcvt(x, n, pt, sign, buf); }
  299. # if !defined (_STLP_NO_LONG_DOUBLE)
  300. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
  301. { return _fp_ecvt( x, n, pt, sign, buf); }
  302. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
  303. { return _fp_fcvt(x, n, pt, sign, buf); }
  304. # endif
  305. # elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
  306. defined (__MRC__) || defined (__SC__) || defined (_CRAY) || \
  307. defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) || \
  308. defined (__DMC__)
  309. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
  310. { return ecvt(x, n, pt, sign ); }
  311. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
  312. { return fcvt(x, n, pt, sign); }
  313. # if !defined (_STLP_NO_LONG_DOUBLE)
  314. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
  315. { return ecvt(x, n, pt, sign ); }
  316. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
  317. { return fcvt(x, n, pt, sign); }
  318. # endif
  319. # define _STLP_CVT_NEED_SYNCHRONIZATION
  320. # else
  321. # error Missing _Stl_ecvtR and _Stl_fcvtR implementations.
  322. # endif
  323. #if defined (_STLP_CVT_NEED_SYNCHRONIZATION)
  324. /* STLport synchronize access to *cvt functions but those methods might
  325. * be called from outside, in this case we will still have a race condition. */
  326. # if defined (_STLP_THREADS)
  327. static _STLP_STATIC_MUTEX& put_float_mutex() {
  328. static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
  329. return __put_float_mutex;
  330. }
  331. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) {
  332. _STLP_auto_lock lock(put_float_mutex());
  333. strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
  334. }
  335. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) {
  336. _STLP_auto_lock lock(put_float_mutex());
  337. strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
  338. }
  339. # if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
  340. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
  341. _STLP_auto_lock lock(put_float_mutex());
  342. strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
  343. }
  344. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
  345. _STLP_auto_lock lock(put_float_mutex());
  346. strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
  347. }
  348. # endif
  349. # else
  350. static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char*)
  351. { return _Stl_ecvtR(x, n, pt, sign); }
  352. static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char*)
  353. { return _Stl_fcvtR(x, n, pt, sign); }
  354. # if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
  355. static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char*)
  356. { return _Stl_ecvtR(x, n, pt, sign); }
  357. static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char*)
  358. { return _Stl_fcvtR(x, n, pt, sign); }
  359. # endif
  360. # endif
  361. #endif
  362. # if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) && !defined (_STLP_NEED_CVT_BUFFER_SIZE)
  363. # define _STLP_CVT_BUFFER(B) B
  364. # else
  365. # define _STLP_CVT_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
  366. # endif
  367. # if defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
  368. static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier);
  369. // Emulation of ecvt/fcvt functions using sprintf:
  370. static char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
  371. // If long double value can be safely converted to double without losing precision
  372. // we use the ecvt function for double:
  373. double y = __STATIC_CAST(double, x);
  374. if (x == y)
  375. return _Stl_ecvtR(y, n, pt, sign, buf);
  376. char fmtbuf[32];
  377. __fill_fmtbuf(fmtbuf, 0, 'L');
  378. sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
  379. /* We are waiting for something having the form x.xxxe+yyyy */
  380. *pt = 0;
  381. *sign = 0;
  382. int i = -1;
  383. int offset = 0;
  384. while (buf[++i] != 0 && n != 0) {
  385. if (buf[i] >= '0' && buf[i] <= '9') {
  386. --n;
  387. if (offset != 0)
  388. buf[i - offset] = buf[i];
  389. }
  390. else {
  391. if (offset != 0) break;
  392. ++offset;
  393. *pt = i;
  394. }
  395. }
  396. if (offset != 0)
  397. buf[i - offset] = 0;
  398. // Extract exponent part in point position:
  399. int e = 0;
  400. while (buf[++i] != 0) {
  401. if (buf[i] >= '0' && buf[i] <= '9') {
  402. e = e * 10 + (buf[i] - '0');
  403. }
  404. }
  405. *pt += e;
  406. return buf;
  407. }
  408. static char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
  409. // If long double value can be safely converted to double without losing precision
  410. // we use the fcvt function for double:
  411. double y = __STATIC_CAST(double, x);
  412. if (x == y)
  413. return _Stl_fcvtR(y, n, pt, sign, buf);
  414. char fmtbuf[32];
  415. __fill_fmtbuf(fmtbuf, ios_base::fixed, 'L');
  416. sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
  417. *pt = 0;
  418. *sign = 0;
  419. int i = -1;
  420. int offset = 0;
  421. while (buf[++i] != 0 && (offset == 0 || n != 0)) {
  422. if (buf[i] >= '0' && buf[i] <= '9') {
  423. if (offset != 0) {
  424. --n;
  425. buf[i - offset] = buf[i];
  426. }
  427. }
  428. else {
  429. ++offset;
  430. *pt = i;
  431. }
  432. }
  433. if (offset != 0)
  434. buf[i - offset] = 0;
  435. else
  436. *pt = i;
  437. return buf;
  438. }
  439. #endif
  440. //----------------------------------------------------------------------
  441. // num_put
  442. // __format_float formats a mantissa and exponent as returned by
  443. // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
  444. // according to the specified precision and format flags. This is
  445. // based on doprnt but is much simpler since it is concerned only
  446. // with floating point input and does not consider all formats. It
  447. // also does not deal with blank padding, which is handled by
  448. // __copy_float_and_fill.
  449. static size_t __format_float_scientific( __iostring& buf, const char *bp,
  450. int decpt, int sign, bool is_zero,
  451. ios_base::fmtflags flags,
  452. int precision) {
  453. // sign if required
  454. if (sign)
  455. buf += '-';
  456. else if (flags & ios_base::showpos)
  457. buf += '+';
  458. // first digit of mantissa
  459. buf += *bp++;
  460. // start of grouping position, grouping won't occur in scientific notation
  461. // as it is impossible to have something like 1234.0e04 but we return a correct
  462. // group position for coherency with __format_float_fixed.
  463. size_t __group_pos = buf.size();
  464. // decimal point if required
  465. if (precision != 0 || flags & ios_base::showpoint) {
  466. buf += '.';
  467. }
  468. // rest of mantissa
  469. while (*bp != 0 && precision--)
  470. buf += *bp++;
  471. // trailing 0 if needed
  472. if (precision > 0)
  473. buf.append(precision, '0');
  474. // exponent size = number of digits + exponent sign + exponent symbol + trailing zero
  475. char expbuf[MAXEDIGITS + 3];
  476. //We start filling at the buffer end
  477. char *suffix = expbuf + MAXEDIGITS + 2;
  478. *suffix = 0;
  479. if (!is_zero) {
  480. int nn = decpt - 1;
  481. if (nn < 0)
  482. nn = -nn;
  483. for (; nn > 9; nn /= 10)
  484. *--suffix = (char) todigit(nn % 10);
  485. *--suffix = (char) todigit(nn);
  486. }
  487. // prepend leading zeros to exponent
  488. // C89 Standard says that it should be at least 2 digits, C99 Standard says that
  489. // we stop prepend zeros if more than 3 digits. To repect both STLport prepend zeros
  490. // until it is 2 digits.
  491. while (suffix > &expbuf[MAXEDIGITS])
  492. *--suffix = '0';
  493. // put in the exponent sign
  494. *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
  495. // put in the e
  496. *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
  497. // copy the suffix
  498. buf += suffix;
  499. return __group_pos;
  500. }
  501. static size_t __format_float_fixed( __iostring &buf, const char *bp,
  502. int decpt, int sign,
  503. ios_base::fmtflags flags,
  504. int precision) {
  505. if ( sign && (decpt > -precision) && (*bp != 0) )
  506. buf += '-';
  507. else if ( flags & ios_base::showpos )
  508. buf += '+';
  509. // digits before decimal point
  510. int nnn = decpt;
  511. do {
  512. buf += (nnn <= 0 || *bp == 0) ? '0' : *bp++;
  513. } while ( --nnn > 0 );
  514. // start of grouping position
  515. size_t __group_pos = buf.size();
  516. // decimal point if needed
  517. if ( flags & ios_base::showpoint || precision > 0 ) {
  518. buf += '.';
  519. }
  520. // digits after decimal point if any
  521. while ( *bp != 0 && --precision >= 0 ) {
  522. buf += (++decpt <= 0) ? '0' : *bp++;
  523. }
  524. // trailing zeros if needed
  525. if (precision > 0)
  526. buf.append(precision, '0');
  527. return __group_pos;
  528. }
  529. #if defined (_STLP_USE_SIGN_HELPER)
  530. template<class _FloatT>
  531. struct float_sign_helper {
  532. float_sign_helper(_FloatT __x)
  533. { _M_number._num = __x; }
  534. bool is_negative() const {
  535. const unsigned short sign_mask(1 << (sizeof(unsigned short) * CHAR_BIT - 1));
  536. return (get_sign_word() & sign_mask) != 0;
  537. }
  538. private:
  539. union {
  540. unsigned short _Words[8];
  541. _FloatT _num;
  542. } _M_number;
  543. unsigned short get_word_higher() const _STLP_NOTHROW
  544. { return _M_number._Words[0]; }
  545. unsigned short get_word_lower() const _STLP_NOTHROW
  546. { return _M_number._Words[(sizeof(_FloatT) >= 12 ? 10 : sizeof(_FloatT)) / sizeof(unsigned short) - 1]; }
  547. unsigned short get_sign_word() const _STLP_NOTHROW
  548. # if defined (_STLP_BIG_ENDIAN)
  549. { return get_word_higher(); }
  550. # else /* _STLP_LITTLE_ENDIAN */
  551. { return get_word_lower(); }
  552. # endif
  553. };
  554. #endif
  555. template <class _FloatT>
  556. static size_t __format_nan_or_inf(__iostring& buf, _FloatT x, ios_base::fmtflags flags) {
  557. static const char* inf[2] = { "inf", "Inf" };
  558. static const char* nan[2] = { "nan", "NaN" };
  559. const char** inf_or_nan;
  560. #if !defined (_STLP_USE_SIGN_HELPER)
  561. if (_Stl_is_inf(x)) { // Infinity
  562. inf_or_nan = inf;
  563. if (_Stl_is_neg_inf(x))
  564. buf += '-';
  565. else if (flags & ios_base::showpos)
  566. buf += '+';
  567. } else { // NaN
  568. inf_or_nan = nan;
  569. if (_Stl_is_neg_nan(x))
  570. buf += '-';
  571. else if (flags & ios_base::showpos)
  572. buf += '+';
  573. }
  574. #else
  575. typedef numeric_limits<_FloatT> limits;
  576. if (x == limits::infinity() || x == -limits::infinity()) {
  577. inf_or_nan = inf;
  578. } else { // NaN
  579. inf_or_nan = nan;
  580. }
  581. float_sign_helper<_FloatT> helper(x);
  582. if (helper.is_negative())
  583. buf += '-';
  584. else if (flags & ios_base::showpos)
  585. buf += '+';
  586. #endif
  587. size_t ret = buf.size();
  588. buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
  589. return ret;
  590. }
  591. static inline size_t __format_float(__iostring &buf, const char * bp,
  592. int decpt, int sign, bool is_zero,
  593. ios_base::fmtflags flags,
  594. int precision) {
  595. size_t __group_pos = 0;
  596. switch (flags & ios_base::floatfield) {
  597. case ios_base::scientific:
  598. __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
  599. flags, precision);
  600. break;
  601. case ios_base::fixed:
  602. __group_pos = __format_float_fixed( buf, bp, decpt, sign,
  603. flags, precision);
  604. break;
  605. default: // g format
  606. // establish default precision
  607. if (flags & ios_base::showpoint || precision > 0) {
  608. if (precision == 0) precision = 1;
  609. } else
  610. precision = 6;
  611. // reset exponent if value is zero
  612. if (is_zero)
  613. decpt = 1;
  614. int kk = precision;
  615. if (!(flags & ios_base::showpoint)) {
  616. size_t n = strlen(bp);
  617. if (n < (size_t)kk)
  618. kk = (int)n;
  619. while (kk >= 1 && bp[kk-1] == '0')
  620. --kk;
  621. }
  622. if (decpt < -3 || decpt > precision) {
  623. precision = kk - 1;
  624. __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
  625. flags, precision);
  626. } else {
  627. precision = kk - decpt;
  628. __group_pos = __format_float_fixed( buf, bp, decpt, sign,
  629. flags, precision);
  630. }
  631. break;
  632. } /* switch */
  633. return __group_pos;
  634. }
  635. #endif
  636. #if defined (USE_SPRINTF_INSTEAD) || defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
  637. struct GroupPos {
  638. bool operator () (char __c) const {
  639. return __c == '.' ||
  640. __c == 'e' || __c == 'E';
  641. }
  642. };
  643. // Creates a format string for sprintf()
  644. static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
  645. fmtbuf[0] = '%';
  646. int i = 1;
  647. if (flags & ios_base::showpos)
  648. fmtbuf[i++] = '+';
  649. if (flags & ios_base::showpoint)
  650. fmtbuf[i++] = '#';
  651. fmtbuf[i++] = '.';
  652. fmtbuf[i++] = '*';
  653. if (long_modifier)
  654. fmtbuf[i++] = long_modifier;
  655. switch (flags & ios_base::floatfield)
  656. {
  657. case ios_base::scientific:
  658. fmtbuf[i++] = (flags & ios_base::uppercase) ? 'E' : 'e';
  659. break;
  660. case ios_base::fixed:
  661. # if defined (__FreeBSD__)
  662. fmtbuf[i++] = 'f';
  663. # else
  664. fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
  665. # endif
  666. break;
  667. default:
  668. fmtbuf[i++] = (flags & ios_base::uppercase) ? 'G' : 'g';
  669. break;
  670. }
  671. fmtbuf[i] = 0;
  672. }
  673. #endif /* USE_SPRINTF_INSTEAD */
  674. template <class _FloatT>
  675. static size_t __write_floatT(__iostring &buf, ios_base::fmtflags flags, int precision,
  676. _FloatT x
  677. #if defined (USE_SPRINTF_INSTEAD)
  678. , char modifier) {
  679. /* In theory, if we want 'arbitrary' precision, we should use 'arbitrary'
  680. * buffer size below, but really we limited by exponent part in double.
  681. * - ptr
  682. */
  683. typedef numeric_limits<_FloatT> limits;
  684. char static_buf[limits::max_exponent10 + 6]; // 6: -xxx.yyyE-zzz (sign, dot, E, exp sign, \0)
  685. char fmtbuf[32];
  686. __fill_fmtbuf(fmtbuf, flags, modifier);
  687. snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
  688. buf = static_buf;
  689. return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
  690. #else
  691. ) {
  692. typedef numeric_limits<_FloatT> limits;
  693. //If numeric_limits support is correct we use the exposed values to detect NaN and infinity:
  694. if (limits::has_infinity && limits::has_quiet_NaN) {
  695. if (!(x == x) || // NaN check
  696. (x == limits::infinity() || x == -limits::infinity())) {
  697. return __format_nan_or_inf(buf, x, flags);
  698. }
  699. }
  700. // numeric_limits support is not good enough, we rely on platform dependent function
  701. // _Stl_is_nan_or_inf that do not support long double.
  702. else if (_Stl_is_nan_or_inf(x)) {
  703. return __format_nan_or_inf(buf, x, flags);
  704. }
  705. # if defined (__MINGW32__)
  706. //For the moment MinGW is limited to display at most numeric_limits<double>::max()
  707. if (x > numeric_limits<double>::max() ||
  708. x < -numeric_limits<double>::max()) {
  709. return __format_nan_or_inf(buf, x, flags);
  710. }
  711. # endif
  712. /* Buffer size is max number of digits which is the addition of:
  713. * - max_exponent10: max number of digits in fixed mode
  714. * - digits10 + 2: max number of significant digits
  715. * - trailing '\0'
  716. */
  717. char cvtbuf[limits::max_exponent10 + limits::digits10 + 2 + 1];
  718. char *bp;
  719. int decpt, sign;
  720. switch (flags & ios_base::floatfield) {
  721. case ios_base::fixed:
  722. {
  723. /* Here, number of digits represents digits _after_ decimal point.
  724. * In order to limit static buffer size we have to give 2 different values depending on x value.
  725. * For small values (abs(x) < 1) we need as many digits as requested by precision limited by the maximum number of digits
  726. * which is min_exponent10 + digits10 + 2
  727. * For bigger values we won't have more than limits::digits10 + 2 digits after decimal point. */
  728. int digits10 = (x > -1.0 && x < 1.0 ? -limits::min_exponent10 + limits::digits10 + 2
  729. : limits::digits10 + 2);
  730. bp = _Stl_fcvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
  731. }
  732. break;
  733. case ios_base::scientific:
  734. default:
  735. /* Here, number of digits is total number of digits which is limited to digits10 + 2. */
  736. {
  737. int digits10 = limits::digits10 + 2;
  738. bp = _Stl_ecvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
  739. }
  740. break;
  741. }
  742. return __format_float(buf, bp, decpt, sign, x == 0.0, flags, precision);
  743. #endif
  744. }
  745. size_t _STLP_CALL
  746. __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
  747. double x) {
  748. return __write_floatT(buf, flags, precision, x
  749. #if defined (USE_SPRINTF_INSTEAD)
  750. , 0
  751. #endif
  752. );
  753. }
  754. #if !defined (_STLP_NO_LONG_DOUBLE)
  755. size_t _STLP_CALL
  756. __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
  757. long double x) {
  758. return __write_floatT(buf, flags, precision, x
  759. #if defined (USE_SPRINTF_INSTEAD)
  760. , 'L'
  761. #endif
  762. );
  763. }
  764. #endif
  765. void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
  766. typedef numeric_limits<_STLP_LONGEST_FLOAT_TYPE> limits;
  767. #if defined (USE_SPRINTF_INSTEAD)
  768. char cvtbuf[limits::max_exponent10 + 6];
  769. # if !defined (_STLP_NO_LONG_DOUBLE)
  770. snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
  771. # else
  772. snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x); // check for 1234.56!
  773. # endif
  774. char *p = strchr( cvtbuf, '.' );
  775. if ( p == 0 ) {
  776. out.append( cvtbuf );
  777. } else {
  778. out.append( cvtbuf, p );
  779. }
  780. #else
  781. char cvtbuf[limits::max_exponent10 + 1];
  782. char * bp;
  783. int decpt, sign;
  784. bp = _Stl_fcvtR(__x, 0, &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf));
  785. if (sign) {
  786. out += '-';
  787. }
  788. out.append(bp, bp + decpt);
  789. #endif
  790. }
  791. #if !defined (_STLP_NO_WCHAR_T)
  792. void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
  793. const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot) {
  794. string::const_iterator str_ite(str.begin()), str_end(str.end());
  795. //First loop, check the dot char
  796. if (__check_dot) {
  797. while (str_ite != str_end) {
  798. if (*str_ite != '.') {
  799. out += ct.widen(*str_ite++);
  800. } else {
  801. out += dot;
  802. break;
  803. }
  804. }
  805. } else {
  806. if (str_ite != str_end) {
  807. out += ct.widen(*str_ite);
  808. }
  809. }
  810. if (str_ite != str_end) {
  811. //Second loop, dot has been found, no check anymore
  812. while (++str_ite != str_end) {
  813. out += ct.widen(*str_ite);
  814. }
  815. }
  816. }
  817. #endif
  818. void _STLP_CALL
  819. __adjust_float_buffer(__iostring &str, char dot) {
  820. if ('.' != dot) {
  821. size_t __dot_pos = str.find('.');
  822. if (__dot_pos != string::npos) {
  823. str[__dot_pos] = dot;
  824. }
  825. }
  826. }
  827. _STLP_MOVE_TO_STD_NAMESPACE
  828. _STLP_END_NAMESPACE
  829. // Local Variables:
  830. // mode:C++
  831. // End: