📄 num_put_float.cpp
字号:
/*
* Copyright (c) 1999
* Silicon Graphics Computer Systems, Inc.
*
* Copyright (c) 1999
* Boris Fomitchev
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
#include "stlport_prefix.h"
#include <cmath>
#include <ios>
#include <locale>
#if defined (__DECCXX)
# define NDIG 400
#else
# define NDIG 82
#endif
#if defined (_STLP_NO_LONG_DOUBLE)
# define MAXECVT 17
# define MAXFCVT 18
typedef double max_double_type;
#else
# define MAXECVT 35
# define MAXFCVT 36
typedef long double max_double_type;
#endif
#define MAXFSIG MAXECVT
#define MAXESIZ 5
#define todigit(x) ((x)+'0')
#if defined (_STLP_UNIX)
# if defined (__sun)
# include <floatingpoint.h>
# endif
# if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
// DEC, SGI & Solaris need this
# include <values.h>
# include <nan.h>
# endif
# if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
defined(__FreeBSD__)
# define USE_SPRINTF_INSTEAD
# endif
# if defined( _AIX ) // JFA 3-Aug-2000
# include <math.h>
# include <float.h>
# endif
#endif
#include <cstdio>
#include <cstdlib>
//#if defined(_CRAY)
//# include <stdlib.h>
//#endif
#if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
# include <float.h>
#endif
#if defined(__MRC__) || defined(__SC__) || defined(_CRAY) //*TY 02/24/2000 - added support for MPW
# include <fp.h>
#endif
#if defined (__CYGWIN__)
# include <ieeefp.h>
#endif
#if defined (__MSL__)
# include <cstdlib> // for atoi
# include <cstdio> // for snprintf
# include <algorithm>
# include <cassert>
#endif
#if defined (__ISCPP__)
# include <cfloat>
#endif
#include <algorithm>
#if defined (__DMC__)
# define snprintf _snprintf
#endif
#if defined(__hpux) && (!defined(_INCLUDE_HPUX_SOURCE) || defined(__GNUC__))
extern "C" double erf(double);
extern "C" double erfc(double);
extern "C" double gamma(double); /* obsolescent */
extern "C" double hypot(double, double);
extern "C" int isnan(double);
extern "C" double j0(double);
extern "C" double j1(double);
extern "C" double jn(int, double);
extern "C" double lgamma(double);
extern "C" double y0(double);
extern "C" double y1(double);
extern "C" double yn(int, double);
# define HUGE_VALF _SINFINITY
# define INFINITY _SINFINITY
# define NAN _SQNAN
# define isnan(x) _ISNAN(x)
# define isinf(x) _ISINF(x)
# define signbit(x) _SIGNBIT(x)
# define isfinite(x) _ISFINITE(x)
# define isnormal(x) _ISNORMAL(x)
# define fpclassify(x) _FPCLASSIFY(x)
# define isunordered(x,y) _ISUNORDERED(x,y)
# define isgreater(x,y) _ISGREATER(x,y)
# define isgreaterequal(x,y) _ISGREATEREQUAL(x,y)
# define isless(x,y) _ISLESS(x,y)
# define islessequal(x,y) _ISLESSEQUAL(x,y)
# define islessgreater(x,y) _ISLESSGREATER(x,y)
# define FP_NORMAL 0
# define FP_ZERO 1
# define FP_INFINITE 2
# define FP_SUBNORMAL 3
# define FP_NAN 4
# define DECIMAL_DIG 17
# define _IS64(x) (sizeof(x) == sizeof(double))
# define _IS32(x) (sizeof(x) == sizeof(float))
extern "C" {
extern double copysign(double, double);
extern const float _SINFINITY;
extern const float _SQNAN;
//# if defined (_PA_RISC)
# define _ISNAN(x) (_IS32(x)?_Isnanf(x):(isnan)(x))
# define _ISINF(x) (_IS32(x)?_Isinff(x):_Isinf(x))
# define _SIGNBIT(x) (_IS32(x)?_Signbitf(x):_Signbit(x))
# define _ISFINITE(x) (_IS32(x)?_Isfinitef(x):_Isfinite(x))
# define _ISNORMAL(x) (_IS32(x)?_Isnormalf(x):_Isnormal(x))
# define _FPCLASSIFY(x) (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1)
# define _ISUNORDERED(x,y) (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y))
extern int _Signbit(double);
extern int _Signbitf(float);
extern int _Isnanf(float);
extern int _Isfinite(double);
extern int _Isfinitef(float);
extern int _Isinf(double);
extern int _Isinff(float);
extern int _Isnormal(double);
extern int _Isnormalf(float);
extern int _Isunordered(double, double);
extern int _Isunorderedf(float, float);
extern int _Fpclassify(double);
extern int _Fpclassifyf(float);
//# else
//# include "math_ia64_internal.h"
//# define _FPCLASSIFY(x) (_IS32(x)?_Fpclassf(x):_Fpclass(x))
// extern int _Fpclass(double);
// extern int _Fpclassf(float);
//# endif
}
# if !defined (_INCLUDE_XOPEN_SOURCE_EXTENDED)
extern "C" char *fcvt(double, int, int *, int *);
extern "C" char *ecvt(double, int, int *, int *);
# endif
# if !defined (_INCLUDE_HPUX_SOURCE)
# if !defined (_LONG_DOUBLE)
# define _LONG_DOUBLE
typedef struct {
uint32_t word1, word2, word3, word4;
} long_double;
# endif /* _LONG_DOUBLE */
extern "C" char *_ldecvt(long_double, int, int *, int *);
extern "C" char *_ldfcvt(long_double, int, int *, int *);
# endif
#endif /* __hpux */
_STLP_BEGIN_NAMESPACE
_STLP_MOVE_TO_PRIV_NAMESPACE
#if defined (__MWERKS__) || defined(__BEOS__)
# define USE_SPRINTF_INSTEAD
#endif
#if defined (_AIX) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
// Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here
# if !defined(_REENTRANT) && !defined(_THREAD_SAFE) && !(defined(_POSIX_THREADS) && defined(__OpenBSD__))
# define LOCK_CVT
# define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign);
# else
static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
# define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex);
# define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf;
# endif // !_REENTRANT
#endif // _AIX || __FreeBSD__ || __NetBSD__ || __OpenBSD__
// Tests for infinity and NaN differ on different OSs. We encapsulate
// these differences here.
#if !defined (USE_SPRINTF_INSTEAD)
# if defined (__hpux) || defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
static inline bool _Stl_is_nan_or_inf(double x)
# if defined (isfinite)
{ return !isfinite(x); }
# else
{ return !finite(x); }
# endif
static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
static inline bool _Stl_is_inf(double x) { return isinf(x); }
// inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; }
static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; }
# elif (defined (__unix) || defined (__unix__)) && \
!defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
!defined (_CRAY)
static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
static inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); }
static inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); }
static inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); }
# elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 )
static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! _isnan(x);}
static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && x < 0 ; }
# elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
static inline bool _Stl_is_inf(double x) {
int fclass = _fpclass(x);
return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
}
static inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; }
static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; }
# elif defined (__MRC__) || defined (__SC__) //*TY 02/24/2000 - added support for MPW
static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
static bool _Stl_is_inf(double x) { return !isfinite(x); }
static bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); }
static bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
# elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); }
static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; }
# elif defined( _AIX ) // JFA 11-Aug-2000
static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
static bool _Stl_is_inf(double x) { return !finite(x); }
// bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; }
static bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); }
static bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
# elif defined (__ISCPP__)
static inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
static inline bool _Stl_is_inf (double x) { return _fp_isINF(x); }
static inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; }
static inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; }
# elif defined (_CRAY)
# if defined (_CRAYIEEE)
static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
static inline bool _Stl_is_inf(double x) { return isinf(x); }
static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); }
static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
# else
static inline bool _Stl_is_nan_or_inf(double x) { return false; }
static inline bool _Stl_is_inf(double x) { return false; }
static inline bool _Stl_is_neg_inf(double x) { return false; }
static inline bool _Stl_is_neg_nan(double x) { return false; }
# endif
# else // nothing from above
# define USE_SPRINTF_INSTEAD
# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -