[8901] | 1 | /// \ingroup newmat
|
---|
| 2 | ///@{
|
---|
| 3 |
|
---|
| 4 | /// \file precisio.h
|
---|
| 5 | /// Floating point precision constants.
|
---|
| 6 |
|
---|
| 7 | #ifndef PRECISION_LIB
|
---|
| 8 | #define PRECISION_LIB 0
|
---|
| 9 |
|
---|
| 10 | #define WANT_MATH
|
---|
| 11 | #include "include.h" // in case being used as stand alone
|
---|
| 12 |
|
---|
| 13 | #ifdef _STANDARD_ // standard library available
|
---|
| 14 | #include <limits>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | #ifdef use_namespace
|
---|
| 18 | namespace NEWMAT {
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | #ifdef _STANDARD_ // standard library available
|
---|
| 22 |
|
---|
| 23 | #ifdef OPT_COMPATIBLE
|
---|
| 24 | #include <cfloat> // for FLT_MAX
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 | using namespace std;
|
---|
| 28 |
|
---|
| 29 | /// Floating point precision.
|
---|
| 30 | class FloatingPointPrecision
|
---|
| 31 | {
|
---|
| 32 | public:
|
---|
| 33 | static int Dig() // number of decimal digits or precision
|
---|
| 34 | { return numeric_limits<Real>::digits10 ; }
|
---|
| 35 |
|
---|
| 36 | static Real Epsilon() // smallest number such that 1+Eps!=Eps
|
---|
| 37 | { return numeric_limits<Real>::epsilon(); }
|
---|
| 38 |
|
---|
| 39 | static int Mantissa() // bits in mantisa
|
---|
| 40 | { return numeric_limits<Real>::digits; }
|
---|
| 41 |
|
---|
| 42 | static Real Maximum() // maximum value
|
---|
| 43 | { return numeric_limits<Real>::max(); }
|
---|
| 44 |
|
---|
| 45 | static int MaximumDecimalExponent() // maximum decimal exponent
|
---|
| 46 | { return numeric_limits<Real>::max_exponent10; }
|
---|
| 47 |
|
---|
| 48 | static int MaximumExponent() // maximum binary exponent
|
---|
| 49 | { return numeric_limits<Real>::max_exponent; }
|
---|
| 50 |
|
---|
| 51 | static Real LnMaximum() // natural log of maximum
|
---|
| 52 | { return (Real)log(Maximum()); }
|
---|
| 53 |
|
---|
| 54 | static Real Minimum() // minimum positive value
|
---|
| 55 | { return numeric_limits<Real>::min(); }
|
---|
| 56 |
|
---|
| 57 | static int MinimumDecimalExponent() // minimum decimal exponent
|
---|
| 58 | { return numeric_limits<Real>::min_exponent10; }
|
---|
| 59 |
|
---|
| 60 | static int MinimumExponent() // minimum binary exponent
|
---|
| 61 | { return numeric_limits<Real>::min_exponent; }
|
---|
| 62 |
|
---|
| 63 | static Real LnMinimum() // natural log of minimum
|
---|
| 64 | { return (Real)log(Minimum()); }
|
---|
| 65 |
|
---|
| 66 | static int Radix() // exponent radix
|
---|
| 67 | { return numeric_limits<Real>::radix; }
|
---|
| 68 |
|
---|
| 69 | static int Rounds() // addition rounding (1 = does round)
|
---|
| 70 | {
|
---|
| 71 | return numeric_limits<Real>::round_style ==
|
---|
| 72 | round_to_nearest ? 1 : 0;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | #else // _STANDARD_ not defined
|
---|
| 79 |
|
---|
| 80 | #ifndef SystemV // if there is float.h
|
---|
| 81 |
|
---|
| 82 | #ifdef USING_FLOAT
|
---|
| 83 |
|
---|
| 84 | /// Floating point precision (type float).
|
---|
| 85 | class FloatingPointPrecision
|
---|
| 86 | {
|
---|
| 87 | public:
|
---|
| 88 | static int Dig()
|
---|
| 89 | { return FLT_DIG; } // number of decimal digits or precision
|
---|
| 90 |
|
---|
| 91 | static Real Epsilon()
|
---|
| 92 | { return FLT_EPSILON; } // smallest number such that 1+Eps!=Eps
|
---|
| 93 |
|
---|
| 94 | static int Mantissa()
|
---|
| 95 | { return FLT_MANT_DIG; } // bits in mantisa
|
---|
| 96 |
|
---|
| 97 | static Real Maximum()
|
---|
| 98 | { return FLT_MAX; } // maximum value
|
---|
| 99 |
|
---|
| 100 | static int MaximumDecimalExponent()
|
---|
| 101 | { return FLT_MAX_10_EXP; } // maximum decimal exponent
|
---|
| 102 |
|
---|
| 103 | static int MaximumExponent()
|
---|
| 104 | { return FLT_MAX_EXP; } // maximum binary exponent
|
---|
| 105 |
|
---|
| 106 | static Real LnMaximum()
|
---|
| 107 | { return (Real)log(Maximum()); } // natural log of maximum
|
---|
| 108 |
|
---|
| 109 | static Real Minimum()
|
---|
| 110 | { return FLT_MIN; } // minimum positive value
|
---|
| 111 |
|
---|
| 112 | static int MinimumDecimalExponent()
|
---|
| 113 | { return FLT_MIN_10_EXP; } // minimum decimal exponent
|
---|
| 114 |
|
---|
| 115 | static int MinimumExponent()
|
---|
| 116 | { return FLT_MIN_EXP; } // minimum binary exponent
|
---|
| 117 |
|
---|
| 118 | static Real LnMinimum()
|
---|
| 119 | { return (Real)log(Minimum()); } // natural log of minimum
|
---|
| 120 |
|
---|
| 121 | static int Radix()
|
---|
| 122 | { return FLT_RADIX; } // exponent radix
|
---|
| 123 |
|
---|
| 124 | static int Rounds()
|
---|
| 125 | { return FLT_ROUNDS; } // addition rounding (1 = does round)
|
---|
| 126 |
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | #endif // USING_FLOAT
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | #ifdef USING_DOUBLE
|
---|
| 133 |
|
---|
| 134 | /// Floating point precision (type double).
|
---|
| 135 | class FloatingPointPrecision
|
---|
| 136 | {
|
---|
| 137 | public:
|
---|
| 138 |
|
---|
| 139 | static int Dig()
|
---|
| 140 | { return DBL_DIG; } // number of decimal digits or precision
|
---|
| 141 |
|
---|
| 142 | static Real Epsilon()
|
---|
| 143 | { return DBL_EPSILON; } // smallest number such that 1+Eps!=Eps
|
---|
| 144 |
|
---|
| 145 | static int Mantissa()
|
---|
| 146 | { return DBL_MANT_DIG; } // bits in mantisa
|
---|
| 147 |
|
---|
| 148 | static Real Maximum()
|
---|
| 149 | { return DBL_MAX; } // maximum value
|
---|
| 150 |
|
---|
| 151 | static int MaximumDecimalExponent()
|
---|
| 152 | { return DBL_MAX_10_EXP; } // maximum decimal exponent
|
---|
| 153 |
|
---|
| 154 | static int MaximumExponent()
|
---|
| 155 | { return DBL_MAX_EXP; } // maximum binary exponent
|
---|
| 156 |
|
---|
| 157 | static Real LnMaximum()
|
---|
| 158 | { return (Real)log(Maximum()); } // natural log of maximum
|
---|
| 159 |
|
---|
| 160 | static Real Minimum()
|
---|
| 161 | {
|
---|
| 162 | //#ifdef __BCPLUSPLUS__
|
---|
| 163 | // return 2.225074e-308; // minimum positive value
|
---|
| 164 | //#else
|
---|
| 165 | return DBL_MIN;
|
---|
| 166 | //#endif
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | static int MinimumDecimalExponent()
|
---|
| 170 | { return DBL_MIN_10_EXP; } // minimum decimal exponent
|
---|
| 171 |
|
---|
| 172 | static int MinimumExponent()
|
---|
| 173 | { return DBL_MIN_EXP; } // minimum binary exponent
|
---|
| 174 |
|
---|
| 175 | static Real LnMinimum()
|
---|
| 176 | { return (Real)log(Minimum()); } // natural log of minimum
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | static int Radix()
|
---|
| 180 | { return FLT_RADIX; } // exponent radix
|
---|
| 181 |
|
---|
| 182 | static int Rounds()
|
---|
| 183 | { return FLT_ROUNDS; } // addition rounding (1 = does round)
|
---|
| 184 |
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | #endif // USING_DOUBLE
|
---|
| 188 |
|
---|
| 189 | #else // if there is no float.h
|
---|
| 190 |
|
---|
| 191 | #ifdef OPT_COMPATIBLE
|
---|
| 192 | #define FLT_MAX MAXFLOAT
|
---|
| 193 | #endif
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | #ifdef USING_FLOAT
|
---|
| 197 |
|
---|
| 198 | /// Floating point precision (type float).
|
---|
| 199 | class FloatingPointPrecision
|
---|
| 200 | {
|
---|
| 201 | public:
|
---|
| 202 |
|
---|
| 203 | static Real Epsilon()
|
---|
| 204 | { return pow(2.0,(int)(1-FSIGNIF)); }
|
---|
| 205 | // smallest number such that 1+Eps!=Eps
|
---|
| 206 |
|
---|
| 207 | static Real Maximum()
|
---|
| 208 | { return MAXFLOAT; } // maximum value
|
---|
| 209 |
|
---|
| 210 | static Real LnMaximum()
|
---|
| 211 | { return (Real)log(Maximum()); } // natural log of maximum
|
---|
| 212 |
|
---|
| 213 | static Real Minimum()
|
---|
| 214 | { return MINFLOAT; } // minimum positive value
|
---|
| 215 |
|
---|
| 216 | static Real LnMinimum()
|
---|
| 217 | { return (Real)log(Minimum()); } // natural log of minimum
|
---|
| 218 |
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 | #endif // USING_FLOAT
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | #ifdef USING_DOUBLE
|
---|
| 225 |
|
---|
| 226 | /// Floating point precision (type double).
|
---|
| 227 | class FloatingPointPrecision
|
---|
| 228 | {
|
---|
| 229 | public:
|
---|
| 230 |
|
---|
| 231 | static Real Epsilon()
|
---|
| 232 | { return pow(2.0,(int)(1-DSIGNIF)); }
|
---|
| 233 | // smallest number such that 1+Eps!=Eps
|
---|
| 234 |
|
---|
| 235 | static Real Maximum()
|
---|
| 236 | { return MAXDOUBLE; } // maximum value
|
---|
| 237 |
|
---|
| 238 | static Real LnMaximum()
|
---|
| 239 | { return LN_MAXDOUBLE; } // natural log of maximum
|
---|
| 240 |
|
---|
| 241 | static Real Minimum()
|
---|
| 242 | { return MINDOUBLE; }
|
---|
| 243 |
|
---|
| 244 | static Real LnMinimum()
|
---|
| 245 | { return LN_MINDOUBLE; } // natural log of minimum
|
---|
| 246 | };
|
---|
| 247 |
|
---|
| 248 | #endif // USING_DOUBLE
|
---|
| 249 |
|
---|
| 250 | #endif // SystemV
|
---|
| 251 |
|
---|
| 252 | #endif // _STANDARD_
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | #ifdef use_namespace
|
---|
| 258 | }
|
---|
| 259 | #endif // use_namespace
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | #endif // PRECISION_LIB
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | ///@}
|
---|