| 1 |
/* |
| 2 |
* include/qd_real.h |
| 3 |
* |
| 4 |
* This work was supported by the Director, Office of Science, Division |
| 5 |
* of Mathematical, Information, and Computational Sciences of the |
| 6 |
* U.S. Department of Energy under contract number DE-AC03-76SF00098. |
| 7 |
* |
| 8 |
* Copyright (c) 2000-2007 |
| 9 |
* |
| 10 |
* Quad-double precision (>= 212-bit significand) floating point arithmetic |
| 11 |
* package, written in ANSI C++, taking full advantage of operator overloading. |
| 12 |
* Uses similar techniques as that of David Bailey's double-double package |
| 13 |
* and that of Jonathan Shewchuk's adaptive precision floating point |
| 14 |
* arithmetic package. See |
| 15 |
* |
| 16 |
* http://www.nersc.gov/~dhbailey/mpdist/mpdist.html |
| 17 |
* http://www.cs.cmu.edu/~quake/robust.html |
| 18 |
* |
| 19 |
* for more details. |
| 20 |
* |
| 21 |
* Yozo Hida |
| 22 |
*/ |
| 23 |
// operator int(), dd_real copysign(), fmax(), lobg(), scalbn() are added |
| 24 |
// for complex class of LLVM Clang++ : 23 Jul.2015 Atsushi Suzuki |
| 25 |
|
| 26 |
#ifndef _QD_QD_REAL_H |
| 27 |
#define _QD_QD_REAL_H |
| 28 |
|
| 29 |
#include <iostream> |
| 30 |
#include <string> |
| 31 |
#include <limits> |
| 32 |
#include <qd/qd_config.h> |
| 33 |
#include <qd/dd_real.h> |
| 34 |
|
| 35 |
struct QD_API qd_real { |
| 36 |
double x[4]; /* The Components. */ |
| 37 |
|
| 38 |
/* Eliminates any zeros in the middle component(s). */ |
| 39 |
void zero_elim(); |
| 40 |
void zero_elim(double &e); |
| 41 |
|
| 42 |
void renorm(); |
| 43 |
void renorm(double &e); |
| 44 |
|
| 45 |
void quick_accum(double d, double &e); |
| 46 |
void quick_prod_accum(double a, double b, double &e); |
| 47 |
|
| 48 |
qd_real(double x0, double x1, double x2, double x3); |
| 49 |
explicit qd_real(const double *xx); |
| 50 |
|
| 51 |
static const qd_real _2pi; |
| 52 |
static const qd_real _pi; |
| 53 |
static const qd_real _3pi4; |
| 54 |
static const qd_real _pi2; |
| 55 |
static const qd_real _pi4; |
| 56 |
static const qd_real _e; |
| 57 |
static const qd_real _log2; |
| 58 |
static const qd_real _log10; |
| 59 |
static const qd_real _nan; |
| 60 |
static const qd_real _inf; |
| 61 |
|
| 62 |
static const double _eps; |
| 63 |
static const double _min_normalized; |
| 64 |
static const qd_real _max; |
| 65 |
static const qd_real _safe_max; |
| 66 |
static const int _ndigits; |
| 67 |
|
| 68 |
qd_real(); |
| 69 |
qd_real(const char *s); |
| 70 |
qd_real(const dd_real &dd); |
| 71 |
qd_real(double d); |
| 72 |
qd_real(int i); |
| 73 |
|
| 74 |
double operator[](int i) const; |
| 75 |
double &operator[](int i); |
| 76 |
|
| 77 |
static void error(const char *msg); |
| 78 |
|
| 79 |
bool isnan() const; |
| 80 |
bool isfinite() const { return QD_ISFINITE(x[0]); } |
| 81 |
bool isinf() const { return QD_ISINF(x[0]); } |
| 82 |
|
| 83 |
static qd_real ieee_add(const qd_real &a, const qd_real &b); |
| 84 |
static qd_real sloppy_add(const qd_real &a, const qd_real &b); |
| 85 |
|
| 86 |
qd_real &operator+=(double a); |
| 87 |
qd_real &operator+=(const dd_real &a); |
| 88 |
qd_real &operator+=(const qd_real &a); |
| 89 |
|
| 90 |
qd_real &operator-=(double a); |
| 91 |
qd_real &operator-=(const dd_real &a); |
| 92 |
qd_real &operator-=(const qd_real &a); |
| 93 |
|
| 94 |
static qd_real sloppy_mul(const qd_real &a, const qd_real &b); |
| 95 |
static qd_real accurate_mul(const qd_real &a, const qd_real &b); |
| 96 |
|
| 97 |
qd_real &operator*=(double a); |
| 98 |
qd_real &operator*=(const dd_real &a); |
| 99 |
qd_real &operator*=(const qd_real &a); |
| 100 |
|
| 101 |
static qd_real sloppy_div(const qd_real &a, const dd_real &b); |
| 102 |
static qd_real accurate_div(const qd_real &a, const dd_real &b); |
| 103 |
static qd_real sloppy_div(const qd_real &a, const qd_real &b); |
| 104 |
static qd_real accurate_div(const qd_real &a, const qd_real &b); |
| 105 |
|
| 106 |
qd_real &operator/=(double a); |
| 107 |
qd_real &operator/=(const dd_real &a); |
| 108 |
qd_real &operator/=(const qd_real &a); |
| 109 |
|
| 110 |
qd_real operator^(int n) const; |
| 111 |
|
| 112 |
qd_real operator-() const; |
| 113 |
qd_real operator+() const; |
| 114 |
|
| 115 |
qd_real &operator=(double a); |
| 116 |
qd_real &operator=(const dd_real &a); |
| 117 |
qd_real &operator=(const char *s); |
| 118 |
|
| 119 |
bool is_zero() const; |
| 120 |
bool is_one() const; |
| 121 |
bool is_positive() const; |
| 122 |
bool is_negative() const; |
| 123 |
|
| 124 |
static qd_real rand(void); |
| 125 |
|
| 126 |
void to_digits(char *s, int &expn, int precision = _ndigits) const; |
| 127 |
void write(char *s, int len, int precision = _ndigits, |
| 128 |
bool showpos = false, bool uppercase = false) const; |
| 129 |
std::string to_string(int precision = _ndigits, int width = 0, |
| 130 |
std::ios_base::fmtflags fmt = static_cast<std::ios_base::fmtflags>(0), |
| 131 |
bool showpos = false, bool uppercase = false, char fill = ' ') const; |
| 132 |
static int read(const char *s, qd_real &a); |
| 133 |
|
| 134 |
/* Debugging methods */ |
| 135 |
void dump(const std::string &name = "", std::ostream &os = std::cerr) const; |
| 136 |
void dump_bits(const std::string &name = "", |
| 137 |
std::ostream &os = std::cerr) const; |
| 138 |
|
| 139 |
static qd_real debug_rand(); |
| 140 |
|
| 141 |
// added for complex class of LLVM Clang++: 23 Jul.2015 Atsushi Suzuki |
| 142 |
operator int() { return int(x[0]); } |
| 143 |
operator int() const { return int(x[0]); } |
| 144 |
|
| 145 |
}; |
| 146 |
|
| 147 |
namespace std { |
| 148 |
template <> |
| 149 |
class numeric_limits<qd_real> : public numeric_limits<double> { |
| 150 |
public: |
| 151 |
inline static double epsilon() { return qd_real::_eps; } |
| 152 |
inline static double min() { return qd_real::_min_normalized; } |
| 153 |
inline static qd_real max() { return qd_real::_max; } |
| 154 |
inline static qd_real safe_max() { return qd_real::_safe_max; } |
| 155 |
static const int digits = 209; |
| 156 |
static const int digits10 = 62; |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
QD_API qd_real polyeval(const qd_real *c, int n, const qd_real &x); |
| 161 |
QD_API qd_real polyroot(const qd_real *c, int n, |
| 162 |
const qd_real &x0, int max_iter = 64, double thresh = 0.0); |
| 163 |
|
| 164 |
QD_API qd_real qdrand(void); |
| 165 |
QD_API qd_real sqrt(const qd_real &a); |
| 166 |
|
| 167 |
QD_API inline bool isnan(const qd_real &a) { return a.isnan(); } |
| 168 |
QD_API inline bool isfinite(const qd_real &a) { return a.isfinite(); } |
| 169 |
QD_API inline bool isinf(const qd_real &a) { return a.isinf(); } |
| 170 |
|
| 171 |
/* Computes qd * d where d is known to be a power of 2. |
| 172 |
This can be done component wise. */ |
| 173 |
QD_API qd_real mul_pwr2(const qd_real &qd, double d); |
| 174 |
|
| 175 |
QD_API qd_real operator+(const qd_real &a, const qd_real &b); |
| 176 |
QD_API qd_real operator+(const dd_real &a, const qd_real &b); |
| 177 |
QD_API qd_real operator+(const qd_real &a, const dd_real &b); |
| 178 |
QD_API qd_real operator+(const qd_real &a, double b); |
| 179 |
QD_API qd_real operator+(double a, const qd_real &b); |
| 180 |
|
| 181 |
QD_API qd_real operator-(const qd_real &a, const qd_real &b); |
| 182 |
QD_API qd_real operator-(const dd_real &a, const qd_real &b); |
| 183 |
QD_API qd_real operator-(const qd_real &a, const dd_real &b); |
| 184 |
QD_API qd_real operator-(const qd_real &a, double b); |
| 185 |
QD_API qd_real operator-(double a, const qd_real &b); |
| 186 |
|
| 187 |
QD_API qd_real operator*(const qd_real &a, const qd_real &b); |
| 188 |
QD_API qd_real operator*(const dd_real &a, const qd_real &b); |
| 189 |
QD_API qd_real operator*(const qd_real &a, const dd_real &b); |
| 190 |
QD_API qd_real operator*(const qd_real &a, double b); |
| 191 |
QD_API qd_real operator*(double a, const qd_real &b); |
| 192 |
|
| 193 |
QD_API qd_real operator/(const qd_real &a, const qd_real &b); |
| 194 |
QD_API qd_real operator/(const dd_real &a, const qd_real &b); |
| 195 |
QD_API qd_real operator/(const qd_real &a, const dd_real &b); |
| 196 |
QD_API qd_real operator/(const qd_real &a, double b); |
| 197 |
QD_API qd_real operator/(double a, const qd_real &b); |
| 198 |
|
| 199 |
QD_API qd_real sqr(const qd_real &a); |
| 200 |
QD_API qd_real sqrt(const qd_real &a); |
| 201 |
QD_API qd_real pow(const qd_real &a, int n); |
| 202 |
QD_API qd_real pow(const qd_real &a, const qd_real &b); |
| 203 |
QD_API qd_real npwr(const qd_real &a, int n); |
| 204 |
|
| 205 |
QD_API qd_real nroot(const qd_real &a, int n); |
| 206 |
|
| 207 |
QD_API qd_real rem(const qd_real &a, const qd_real &b); |
| 208 |
QD_API qd_real drem(const qd_real &a, const qd_real &b); |
| 209 |
QD_API qd_real divrem(const qd_real &a, const qd_real &b, qd_real &r); |
| 210 |
|
| 211 |
dd_real to_dd_real(const qd_real &a); |
| 212 |
double to_double(const qd_real &a); |
| 213 |
int to_int(const qd_real &a); |
| 214 |
|
| 215 |
QD_API bool operator==(const qd_real &a, const qd_real &b); |
| 216 |
QD_API bool operator==(const qd_real &a, const dd_real &b); |
| 217 |
QD_API bool operator==(const dd_real &a, const qd_real &b); |
| 218 |
QD_API bool operator==(double a, const qd_real &b); |
| 219 |
QD_API bool operator==(const qd_real &a, double b); |
| 220 |
QD_API bool operator==(int a, const qd_real &b); |
| 221 |
QD_API bool operator==(const qd_real &a, int b); |
| 222 |
|
| 223 |
QD_API bool operator<(const qd_real &a, const qd_real &b); |
| 224 |
QD_API bool operator<(const qd_real &a, const dd_real &b); |
| 225 |
QD_API bool operator<(const dd_real &a, const qd_real &b); |
| 226 |
QD_API bool operator<(double a, const qd_real &b); |
| 227 |
QD_API bool operator<(const qd_real &a, double b); |
| 228 |
QD_API bool operator<(int a, const qd_real &b); |
| 229 |
QD_API bool operator<(const qd_real &a, int b); |
| 230 |
|
| 231 |
QD_API bool operator>(const qd_real &a, const qd_real &b); |
| 232 |
QD_API bool operator>(const qd_real &a, const dd_real &b); |
| 233 |
QD_API bool operator>(const dd_real &a, const qd_real &b); |
| 234 |
QD_API bool operator>(double a, const qd_real &b); |
| 235 |
QD_API bool operator>(const qd_real &a, double b); |
| 236 |
QD_API bool operator>(int a, const qd_real &b); |
| 237 |
QD_API bool operator>(const qd_real &a, int b); |
| 238 |
|
| 239 |
QD_API bool operator<=(const qd_real &a, const qd_real &b); |
| 240 |
QD_API bool operator<=(const qd_real &a, const dd_real &b); |
| 241 |
QD_API bool operator<=(const dd_real &a, const qd_real &b); |
| 242 |
QD_API bool operator<=(double a, const qd_real &b); |
| 243 |
QD_API bool operator<=(const qd_real &a, double b); |
| 244 |
QD_API bool operator<=(int a, const qd_real &b); |
| 245 |
QD_API bool operator<=(const qd_real &a, int b); |
| 246 |
|
| 247 |
QD_API bool operator>=(const qd_real &a, const qd_real &b); |
| 248 |
QD_API bool operator>=(const qd_real &a, const dd_real &b); |
| 249 |
QD_API bool operator>=(const dd_real &a, const qd_real &b); |
| 250 |
QD_API bool operator>=(double a, const qd_real &b); |
| 251 |
QD_API bool operator>=(const qd_real &a, double b); |
| 252 |
QD_API bool operator>=(int a, const qd_real &b); |
| 253 |
QD_API bool operator>=(const qd_real &a, int b); |
| 254 |
|
| 255 |
QD_API bool operator!=(const qd_real &a, const qd_real &b); |
| 256 |
QD_API bool operator!=(const qd_real &a, const dd_real &b); |
| 257 |
QD_API bool operator!=(const dd_real &a, const qd_real &b); |
| 258 |
QD_API bool operator!=(double a, const qd_real &b); |
| 259 |
QD_API bool operator!=(const qd_real &a, double b); |
| 260 |
QD_API bool operator!=(int a, const qd_real &b); |
| 261 |
QD_API bool operator!=(const qd_real &a, int b); |
| 262 |
|
| 263 |
QD_API qd_real fabs(const qd_real &a); |
| 264 |
QD_API qd_real abs(const qd_real &a); /* same as fabs */ |
| 265 |
|
| 266 |
QD_API qd_real ldexp(const qd_real &a, int n); |
| 267 |
|
| 268 |
QD_API qd_real nint(const qd_real &a); |
| 269 |
QD_API qd_real quick_nint(const qd_real &a); |
| 270 |
QD_API qd_real floor(const qd_real &a); |
| 271 |
QD_API qd_real ceil(const qd_real &a); |
| 272 |
QD_API qd_real aint(const qd_real &a); |
| 273 |
|
| 274 |
QD_API qd_real sin(const qd_real &a); |
| 275 |
QD_API qd_real cos(const qd_real &a); |
| 276 |
QD_API qd_real tan(const qd_real &a); |
| 277 |
QD_API void sincos(const qd_real &a, qd_real &s, qd_real &c); |
| 278 |
|
| 279 |
QD_API qd_real asin(const qd_real &a); |
| 280 |
QD_API qd_real acos(const qd_real &a); |
| 281 |
QD_API qd_real atan(const qd_real &a); |
| 282 |
QD_API qd_real atan2(const qd_real &y, const qd_real &x); |
| 283 |
|
| 284 |
QD_API qd_real exp(const qd_real &a); |
| 285 |
QD_API qd_real log(const qd_real &a); |
| 286 |
QD_API qd_real log10(const qd_real &a); |
| 287 |
|
| 288 |
QD_API qd_real sinh(const qd_real &a); |
| 289 |
QD_API qd_real cosh(const qd_real &a); |
| 290 |
QD_API qd_real tanh(const qd_real &a); |
| 291 |
QD_API void sincosh(const qd_real &a, qd_real &sin_qd, qd_real &cos_qd); |
| 292 |
|
| 293 |
QD_API qd_real asinh(const qd_real &a); |
| 294 |
QD_API qd_real acosh(const qd_real &a); |
| 295 |
QD_API qd_real atanh(const qd_real &a); |
| 296 |
|
| 297 |
QD_API qd_real qdrand(void); |
| 298 |
|
| 299 |
QD_API qd_real max(const qd_real &a, const qd_real &b); |
| 300 |
QD_API qd_real max(const qd_real &a, const qd_real &b, const qd_real &c); |
| 301 |
QD_API qd_real min(const qd_real &a, const qd_real &b); |
| 302 |
QD_API qd_real min(const qd_real &a, const qd_real &b, const qd_real &c); |
| 303 |
|
| 304 |
QD_API qd_real fmod(const qd_real &a, const qd_real &b); |
| 305 |
|
| 306 |
QD_API std::ostream &operator<<(std::ostream &s, const qd_real &a); |
| 307 |
QD_API std::istream &operator>>(std::istream &s, qd_real &a); |
| 308 |
#ifdef QD_INLINE |
| 309 |
#include <qd/qd_inline.h> |
| 310 |
#endif |
| 311 |
|
| 312 |
// added for complex class of LLVM Clang++ : 23 Jul.2015 Atsushi Suzuki |
| 313 |
|
| 314 |
inline qd_real copysign(const qd_real &x, const qd_real &y) { |
| 315 |
return (y.x[0] < 0.0) ? ((x.x[0] < 0.0) ? x : (-x)) : ((x.x[0] < 0.0) ? (-x) : x); |
| 316 |
} |
| 317 |
|
| 318 |
inline qd_real fmax(const qd_real &x, const qd_real &y) { |
| 319 |
return x.x[0] < y.x[0] ? y : x; |
| 320 |
} |
| 321 |
|
| 322 |
inline qd_real logb(const qd_real &y) { |
| 323 |
return qd_real(logb(y.x[0])); |
| 324 |
} |
| 325 |
#if 0 |
| 326 |
inline qd_real scalbn(const qd_real &x, int n) { |
| 327 |
return qd_real(scalb(x.x[0], n)); |
| 328 |
} |
| 329 |
#endif |
| 330 |
#endif /* _QD_QD_REAL_H */ |
| 331 |
|