mathlimits.cc
1 // Copyright 2005 Google Inc.
2 // All Rights Reserved.
3 //
4 //
5 
6 #include "util/math/mathlimits.h"
7 
8 #include "base/integral_types.h"
9 
10 // MSVC++ 2005 thinks the header declaration was a definition, and
11 // erroneously flags these as a duplicate definition.
12 #ifdef COMPILER_MSVC
13 
14 #define DEF_COMMON_LIMITS(Type)
15 #define DEF_UNSIGNED_INT_LIMITS(Type)
16 #define DEF_SIGNED_INT_LIMITS(Type)
17 #define DEF_PRECISION_LIMITS(Type)
18 
19 #else
20 
21 #define DEF_COMMON_LIMITS(Type) \
22 const bool MathLimits<Type>::kIsSigned; \
23 const bool MathLimits<Type>::kIsInteger; \
24 const int MathLimits<Type>::kMin10Exp; \
25 const int MathLimits<Type>::kMax10Exp;
26 
27 #define DEF_UNSIGNED_INT_LIMITS(Type) \
28 DEF_COMMON_LIMITS(Type) \
29 const Type MathLimits<Type>::kPosMin; \
30 const Type MathLimits<Type>::kPosMax; \
31 const Type MathLimits<Type>::kMin; \
32 const Type MathLimits<Type>::kMax; \
33 const Type MathLimits<Type>::kEpsilon; \
34 const Type MathLimits<Type>::kStdError;
35 
36 #define DEF_SIGNED_INT_LIMITS(Type) \
37 DEF_UNSIGNED_INT_LIMITS(Type) \
38 const Type MathLimits<Type>::kNegMin; \
39 const Type MathLimits<Type>::kNegMax;
40 
41 #define DEF_PRECISION_LIMITS(Type) \
42 const int MathLimits<Type>::kPrecisionDigits;
43 
44 #endif // not COMPILER_MSVC
45 
46 #define DEF_FP_LIMITS(Type, PREFIX) \
47 DEF_COMMON_LIMITS(Type) \
48 const Type MathLimits<Type>::kPosMin = PREFIX##_MIN; \
49 const Type MathLimits<Type>::kPosMax = PREFIX##_MAX; \
50 const Type MathLimits<Type>::kMin = -MathLimits<Type>::kPosMax; \
51 const Type MathLimits<Type>::kMax = MathLimits<Type>::kPosMax; \
52 const Type MathLimits<Type>::kNegMin = -MathLimits<Type>::kPosMin; \
53 const Type MathLimits<Type>::kNegMax = -MathLimits<Type>::kPosMax; \
54 const Type MathLimits<Type>::kEpsilon = PREFIX##_EPSILON; \
55 /* 32 is 5 bits of mantissa error; should be adequate for common errors */ \
56 const Type MathLimits<Type>::kStdError = MathLimits<Type>::kEpsilon * 32; \
57 DEF_PRECISION_LIMITS(Type) \
58 const Type MathLimits<Type>::kNaN = HUGE_VAL - HUGE_VAL; \
59 const Type MathLimits<Type>::kPosInf = HUGE_VAL; \
60 const Type MathLimits<Type>::kNegInf = -HUGE_VAL;
61 
62 DEF_SIGNED_INT_LIMITS(int8)
63 DEF_SIGNED_INT_LIMITS(int16)
64 DEF_SIGNED_INT_LIMITS(int32)
65 DEF_SIGNED_INT_LIMITS(int64)
66 DEF_UNSIGNED_INT_LIMITS(uint8)
67 DEF_UNSIGNED_INT_LIMITS(uint16)
68 DEF_UNSIGNED_INT_LIMITS(uint32)
69 DEF_UNSIGNED_INT_LIMITS(uint64)
70 
71 //DEF_SIGNED_INT_LIMITS(long int)
72 //DEF_UNSIGNED_INT_LIMITS(unsigned long int)
73 
74 DEF_FP_LIMITS(float, FLT)
75 DEF_FP_LIMITS(double, DBL)
76 DEF_FP_LIMITS(long double, LDBL);
77 
78 #undef DEF_COMMON_LIMITS
79 #undef DEF_SIGNED_INT_LIMITS
80 #undef DEF_UNSIGNED_INT_LIMITS
81 #undef DEF_FP_LIMITS
82 #undef DEF_PRECISION_LIMITS