mathlimits.h
1 // Copyright 2005 Google Inc.
2 // All Rights Reserved.
3 //
4 //
5 // Useful integer and floating point limits and type traits.
6 //
7 // This partially replaces/duplictes numeric_limits<> from <limits>.
8 // We get a Google-style class that we have a greater control over
9 // and thus can add new features to it or fix whatever happens to be broken in
10 // numeric_limits for the compilers we use.
11 //
12 
13 #ifndef UTIL_MATH_MATHLIMITS_H__
14 #define UTIL_MATH_MATHLIMITS_H__
15 
16 #include <cstring>
17 #include <cmath>
18 #include <cfloat>
19 #include "base/integral_types.h"
20 
21 // ========================================================================= //
22 
23 // Useful integer and floating point limits and type traits.
24 // This is just for the documentation;
25 // real members are defined in our specializations below.
26 template<typename T> struct MathLimits {
27  // Type name.
28  typedef T Type;
29  // Unsigned version of the Type with the same byte size.
30  // Same as Type for floating point and unsigned types.
31  typedef T UnsignedType;
32  // If the type supports negative values.
33  static const bool kIsSigned;
34  // If the type supports only integer values.
35  static const bool kIsInteger;
36  // Magnitude-wise smallest representable positive value.
37  static const Type kPosMin;
38  // Magnitude-wise largest representable positive value.
39  static const Type kPosMax;
40  // Smallest representable value.
41  static const Type kMin;
42  // Largest representable value.
43  static const Type kMax;
44  // Magnitude-wise smallest representable negative value.
45  // Present only if kIsSigned.
46  static const Type kNegMin;
47  // Magnitude-wise largest representable negative value.
48  // Present only if kIsSigned.
49  static const Type kNegMax;
50  // Smallest integer x such that 10^x is representable.
51  static const int kMin10Exp;
52  // Largest integer x such that 10^x is representable.
53  static const int kMax10Exp;
54  // Smallest positive value such that Type(1) + kEpsilon != Type(1)
55  static const Type kEpsilon;
56  // Typical rounding error that is enough to cover
57  // a few simple floating-point operations.
58  // Slightly larger than kEpsilon to account for a few rounding errors.
59  // Is zero if kIsInteger.
60  static const Type kStdError;
61  // Number of decimal digits of mantissa precision.
62  // Present only if !kIsInteger.
63  static const int kPrecisionDigits;
64  // Not a number, i.e. result of 0/0.
65  // Present only if !kIsInteger.
66  static const Type kNaN;
67  // Positive infinity, i.e. result of 1/0.
68  // Present only if !kIsInteger.
69  static const Type kPosInf;
70  // Negative infinity, i.e. result of -1/0.
71  // Present only if !kIsInteger.
72  static const Type kNegInf;
73 
74  // NOTE: Special floating point values behave
75  // in a special (but mathematically-logical) way
76  // in terms of (in)equalty comparison and mathematical operations
77  // -- see out unittest for examples.
78 
79  // Special floating point value testers.
80  // Present in integer types for convenience.
81  static bool IsFinite(const Type x);
82  static bool IsNaN(const Type x);
83  static bool IsInf(const Type x);
84  static bool IsPosInf(const Type x);
85  static bool IsNegInf(const Type x);
86 };
87 
88 // ========================================================================= //
89 
90 // All #define-s below are simply to refactor the declarations of
91 // MathLimits template specializations.
92 // They are all #undef-ined below.
93 
94 // The hoop-jumping in *_INT_(MAX|MIN) below is so that the compiler does not
95 // get an overflow while computing the constants.
96 
97 #define SIGNED_INT_MAX(Type) \
98  (((Type(1) << (sizeof(Type)*8 - 2)) - 1) + (Type(1) << (sizeof(Type)*8 - 2)))
99 
100 #define SIGNED_INT_MIN(Type) \
101  (-(Type(1) << (sizeof(Type)*8 - 2)) - (Type(1) << (sizeof(Type)*8 - 2)))
102 
103 #define UNSIGNED_INT_MAX(Type) \
104  (((Type(1) << (sizeof(Type)*8 - 1)) - 1) + (Type(1) << (sizeof(Type)*8 - 1)))
105 
106 // Compile-time selected log10-related constants for integer types.
107 #define SIGNED_MAX_10_EXP(Type) \
108  (sizeof(Type) == 1 ? 2 : ( \
109  sizeof(Type) == 2 ? 4 : ( \
110  sizeof(Type) == 4 ? 9 : ( \
111  sizeof(Type) == 8 ? 18 : -1))))
112 
113 #define UNSIGNED_MAX_10_EXP(Type) \
114  (sizeof(Type) == 1 ? 2 : ( \
115  sizeof(Type) == 2 ? 4 : ( \
116  sizeof(Type) == 4 ? 9 : ( \
117  sizeof(Type) == 8 ? 19 : -1))))
118 
119 #define DECL_INT_LIMIT_FUNCS \
120  static bool IsFinite(const Type ) { return true; } \
121  static bool IsNaN(const Type ) { return false; } \
122  static bool IsInf(const Type ) { return false; } \
123  static bool IsPosInf(const Type ) { return false; } \
124  static bool IsNegInf(const Type ) { return false; }
125 
126 #define DECL_SIGNED_INT_LIMITS(IntType, UnsignedIntType) \
127 template<> \
128 struct MathLimits<IntType> { \
129  typedef IntType Type; \
130  typedef UnsignedIntType UnsignedType; \
131  static const bool kIsSigned = true; \
132  static const bool kIsInteger = true; \
133  static const Type kPosMin = 1; \
134  static const Type kPosMax = SIGNED_INT_MAX(Type); \
135  static const Type kMin = SIGNED_INT_MIN(Type); \
136  static const Type kMax = kPosMax; \
137  static const Type kNegMin = -1; \
138  static const Type kNegMax = kMin; \
139  static const int kMin10Exp = 0; \
140  static const int kMax10Exp = SIGNED_MAX_10_EXP(Type); \
141  static const Type kEpsilon = 1; \
142  static const Type kStdError = 0; \
143  DECL_INT_LIMIT_FUNCS \
144 };
145 
146 #define DECL_UNSIGNED_INT_LIMITS(IntType) \
147 template<> \
148 struct MathLimits<IntType> { \
149  typedef IntType Type; \
150  typedef IntType UnsignedType; \
151  static const bool kIsSigned = false; \
152  static const bool kIsInteger = true; \
153  static const Type kPosMin = 1; \
154  static const Type kPosMax = UNSIGNED_INT_MAX(Type); \
155  static const Type kMin = 0; \
156  static const Type kMax = kPosMax; \
157  static const int kMin10Exp = 0; \
158  static const int kMax10Exp = UNSIGNED_MAX_10_EXP(Type); \
159  static const Type kEpsilon = 1; \
160  static const Type kStdError = 0; \
161  DECL_INT_LIMIT_FUNCS \
162 };
163 
164 DECL_SIGNED_INT_LIMITS(signed char, unsigned char)
165 DECL_SIGNED_INT_LIMITS(signed short int, unsigned short int)
166 DECL_SIGNED_INT_LIMITS(signed int, unsigned int)
167 DECL_SIGNED_INT_LIMITS(signed long int, unsigned long int)
168 DECL_SIGNED_INT_LIMITS(signed long long int, unsigned long long int)
169 DECL_UNSIGNED_INT_LIMITS(unsigned char)
170 DECL_UNSIGNED_INT_LIMITS(unsigned short int)
171 DECL_UNSIGNED_INT_LIMITS(unsigned int)
172 DECL_UNSIGNED_INT_LIMITS(unsigned long int)
173 DECL_UNSIGNED_INT_LIMITS(unsigned long long int)
174 
175 #undef DECL_SIGNED_INT_LIMITS
176 #undef DECL_UNSIGNED_INT_LIMITS
177 #undef SIGNED_INT_MAX
178 #undef SIGNED_INT_MIN
179 #undef UNSIGNED_INT_MAX
180 #undef SIGNED_MAX_10_EXP
181 #undef UNSIGNED_MAX_10_EXP
182 #undef DECL_INT_LIMIT_FUNCS
183 
184 // ========================================================================= //
185 #ifdef WIN32 // Lacks built-in isnan() and std::isinf()
186 #define DECL_FP_LIMIT_FUNCS \
187  static bool IsFinite(const Type x) { return _finite(x); } \
188  static bool IsNaN(const Type x) { return _isnan(x); } \
189  static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCLASS_PINF)) != 0; } \
190  static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \
191  static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; }
192 #else
193 #define DECL_FP_LIMIT_FUNCS \
194  static bool IsFinite(const Type x) { return !std::isinf(x) && !std::isnan(x); } \
195  static bool IsNaN(const Type x) { return std::isnan(x); } \
196  static bool IsInf(const Type x) { return std::isinf(x); } \
197  static bool IsPosInf(const Type x) { return std::isinf(x) && x > 0; } \
198  static bool IsNegInf(const Type x) { return std::isinf(x) && x < 0; }
199 #endif
200 
201 // We can't put floating-point constant values in the header here because
202 // such constants are not considered to be primitive-type constants by gcc.
203 // CAVEAT: Hence, they are going to be initialized only during
204 // the global objects construction time.
205 #define DECL_FP_LIMITS(FP_Type, PREFIX) \
206 template<> \
207 struct MathLimits<FP_Type> { \
208  typedef FP_Type Type; \
209  typedef FP_Type UnsignedType; \
210  static const bool kIsSigned = true; \
211  static const bool kIsInteger = false; \
212  static const Type kPosMin; \
213  static const Type kPosMax; \
214  static const Type kMin; \
215  static const Type kMax; \
216  static const Type kNegMin; \
217  static const Type kNegMax; \
218  static const int kMin10Exp = PREFIX##_MIN_10_EXP; \
219  static const int kMax10Exp = PREFIX##_MAX_10_EXP; \
220  static const Type kEpsilon; \
221  static const Type kStdError; \
222  static const int kPrecisionDigits = PREFIX##_DIG; \
223  static const Type kNaN; \
224  static const Type kPosInf; \
225  static const Type kNegInf; \
226  DECL_FP_LIMIT_FUNCS \
227 };
228 
229 DECL_FP_LIMITS(float, FLT)
230 DECL_FP_LIMITS(double, DBL)
231 DECL_FP_LIMITS(long double, LDBL)
232 
233 #undef DECL_FP_LIMITS
234 #undef DECL_FP_LIMIT_FUNCS
235 
236 // ========================================================================= //
237 
238 #endif // UTIL_MATH_MATHLIMITS_H__