| 1 |
/* |
| 2 |
* include/dd_inline.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-2001 |
| 9 |
* |
| 10 |
* Contains small functions (suitable for inlining) in the double-double |
| 11 |
* arithmetic package. |
| 12 |
*/ |
| 13 |
#ifndef _QD_DD_INLINE_H |
| 14 |
#define _QD_DD_INLINE_H |
| 15 |
|
| 16 |
#include <cmath> |
| 17 |
#include <qd/inline.h> |
| 18 |
|
| 19 |
#ifndef QD_INLINE |
| 20 |
#define inline |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
/*********** Additions ************/ |
| 25 |
/* double-double = double + double */ |
| 26 |
inline dd_real dd_real::add(double a, double b) { |
| 27 |
double s, e; |
| 28 |
s = qd::two_sum(a, b, e); |
| 29 |
return dd_real(s, e); |
| 30 |
} |
| 31 |
|
| 32 |
/* double-double + double */ |
| 33 |
inline dd_real operator+(const dd_real &a, double b) { |
| 34 |
double s1, s2; |
| 35 |
s1 = qd::two_sum(a.x[0], b, s2); |
| 36 |
s2 += a.x[1]; |
| 37 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 38 |
return dd_real(s1, s2); |
| 39 |
} |
| 40 |
|
| 41 |
/* double-double + double-double */ |
| 42 |
inline dd_real dd_real::ieee_add(const dd_real &a, const dd_real &b) { |
| 43 |
/* This one satisfies IEEE style error bound, |
| 44 |
due to K. Briggs and W. Kahan. */ |
| 45 |
double s1, s2, t1, t2; |
| 46 |
|
| 47 |
s1 = qd::two_sum(a.x[0], b.x[0], s2); |
| 48 |
t1 = qd::two_sum(a.x[1], b.x[1], t2); |
| 49 |
s2 += t1; |
| 50 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 51 |
s2 += t2; |
| 52 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 53 |
return dd_real(s1, s2); |
| 54 |
} |
| 55 |
|
| 56 |
inline dd_real dd_real::sloppy_add(const dd_real &a, const dd_real &b) { |
| 57 |
/* This is the less accurate version ... obeys Cray-style |
| 58 |
error bound. */ |
| 59 |
double s, e; |
| 60 |
|
| 61 |
s = qd::two_sum(a.x[0], b.x[0], e); |
| 62 |
e += (a.x[1] + b.x[1]); |
| 63 |
s = qd::quick_two_sum(s, e, e); |
| 64 |
return dd_real(s, e); |
| 65 |
} |
| 66 |
|
| 67 |
inline dd_real operator+(const dd_real &a, const dd_real &b) { |
| 68 |
#ifndef QD_IEEE_ADD |
| 69 |
return dd_real::sloppy_add(a, b); |
| 70 |
#else |
| 71 |
return dd_real::ieee_add(a, b); |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
/* double + double-double */ |
| 76 |
inline dd_real operator+(double a, const dd_real &b) { |
| 77 |
return (b + a); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/*********** Self-Additions ************/ |
| 82 |
/* double-double += double */ |
| 83 |
inline dd_real &dd_real::operator+=(double a) { |
| 84 |
double s1, s2; |
| 85 |
s1 = qd::two_sum(x[0], a, s2); |
| 86 |
s2 += x[1]; |
| 87 |
x[0] = qd::quick_two_sum(s1, s2, x[1]); |
| 88 |
return *this; |
| 89 |
} |
| 90 |
|
| 91 |
/* double-double += double-double */ |
| 92 |
inline dd_real &dd_real::operator+=(const dd_real &a) { |
| 93 |
#ifndef QD_IEEE_ADD |
| 94 |
double s, e; |
| 95 |
s = qd::two_sum(x[0], a.x[0], e); |
| 96 |
e += x[1]; |
| 97 |
e += a.x[1]; |
| 98 |
x[0] = qd::quick_two_sum(s, e, x[1]); |
| 99 |
return *this; |
| 100 |
#else |
| 101 |
double s1, s2, t1, t2; |
| 102 |
s1 = qd::two_sum(x[0], a.x[0], s2); |
| 103 |
t1 = qd::two_sum(x[1], a.x[1], t2); |
| 104 |
s2 += t1; |
| 105 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 106 |
s2 += t2; |
| 107 |
x[0] = qd::quick_two_sum(s1, s2, x[1]); |
| 108 |
return *this; |
| 109 |
#endif |
| 110 |
} |
| 111 |
|
| 112 |
/*********** Subtractions ************/ |
| 113 |
/* double-double = double - double */ |
| 114 |
inline dd_real dd_real::sub(double a, double b) { |
| 115 |
double s, e; |
| 116 |
s = qd::two_diff(a, b, e); |
| 117 |
return dd_real(s, e); |
| 118 |
} |
| 119 |
|
| 120 |
/* double-double - double */ |
| 121 |
inline dd_real operator-(const dd_real &a, double b) { |
| 122 |
double s1, s2; |
| 123 |
s1 = qd::two_diff(a.x[0], b, s2); |
| 124 |
s2 += a.x[1]; |
| 125 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 126 |
return dd_real(s1, s2); |
| 127 |
} |
| 128 |
|
| 129 |
/* double-double - double-double */ |
| 130 |
inline dd_real operator-(const dd_real &a, const dd_real &b) { |
| 131 |
#ifndef QD_IEEE_ADD |
| 132 |
double s, e; |
| 133 |
s = qd::two_diff(a.x[0], b.x[0], e); |
| 134 |
e += a.x[1]; |
| 135 |
e -= b.x[1]; |
| 136 |
s = qd::quick_two_sum(s, e, e); |
| 137 |
return dd_real(s, e); |
| 138 |
#else |
| 139 |
double s1, s2, t1, t2; |
| 140 |
s1 = qd::two_diff(a.x[0], b.x[0], s2); |
| 141 |
t1 = qd::two_diff(a.x[1], b.x[1], t2); |
| 142 |
s2 += t1; |
| 143 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 144 |
s2 += t2; |
| 145 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 146 |
return dd_real(s1, s2); |
| 147 |
#endif |
| 148 |
} |
| 149 |
|
| 150 |
/* double - double-double */ |
| 151 |
inline dd_real operator-(double a, const dd_real &b) { |
| 152 |
double s1, s2; |
| 153 |
s1 = qd::two_diff(a, b.x[0], s2); |
| 154 |
s2 -= b.x[1]; |
| 155 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 156 |
return dd_real(s1, s2); |
| 157 |
} |
| 158 |
|
| 159 |
/*********** Self-Subtractions ************/ |
| 160 |
/* double-double -= double */ |
| 161 |
inline dd_real &dd_real::operator-=(double a) { |
| 162 |
double s1, s2; |
| 163 |
s1 = qd::two_diff(x[0], a, s2); |
| 164 |
s2 += x[1]; |
| 165 |
x[0] = qd::quick_two_sum(s1, s2, x[1]); |
| 166 |
return *this; |
| 167 |
} |
| 168 |
|
| 169 |
/* double-double -= double-double */ |
| 170 |
inline dd_real &dd_real::operator-=(const dd_real &a) { |
| 171 |
#ifndef QD_IEEE_ADD |
| 172 |
double s, e; |
| 173 |
s = qd::two_diff(x[0], a.x[0], e); |
| 174 |
e += x[1]; |
| 175 |
e -= a.x[1]; |
| 176 |
x[0] = qd::quick_two_sum(s, e, x[1]); |
| 177 |
return *this; |
| 178 |
#else |
| 179 |
double s1, s2, t1, t2; |
| 180 |
s1 = qd::two_diff(x[0], a.x[0], s2); |
| 181 |
t1 = qd::two_diff(x[1], a.x[1], t2); |
| 182 |
s2 += t1; |
| 183 |
s1 = qd::quick_two_sum(s1, s2, s2); |
| 184 |
s2 += t2; |
| 185 |
x[0] = qd::quick_two_sum(s1, s2, x[1]); |
| 186 |
return *this; |
| 187 |
#endif |
| 188 |
} |
| 189 |
|
| 190 |
/*********** Unary Minus ***********/ |
| 191 |
inline dd_real dd_real::operator-() const { |
| 192 |
return dd_real(-x[0], -x[1]); |
| 193 |
} |
| 194 |
/*********** Unary Plus ***********/ |
| 195 |
inline dd_real dd_real::operator+() const { |
| 196 |
return *this; |
| 197 |
} |
| 198 |
|
| 199 |
/*********** Multiplications ************/ |
| 200 |
/* double-double = double * double */ |
| 201 |
inline dd_real dd_real::mul(double a, double b) { |
| 202 |
double p, e; |
| 203 |
p = qd::two_prod(a, b, e); |
| 204 |
return dd_real(p, e); |
| 205 |
} |
| 206 |
|
| 207 |
/* double-double * (2.0 ^ exp) */ |
| 208 |
inline dd_real ldexp(const dd_real &a, int exp) { |
| 209 |
return dd_real(std::ldexp(a.x[0], exp), std::ldexp(a.x[1], exp)); |
| 210 |
} |
| 211 |
|
| 212 |
/* double-double * double, where double is a power of 2. */ |
| 213 |
inline dd_real mul_pwr2(const dd_real &a, double b) { |
| 214 |
return dd_real(a.x[0] * b, a.x[1] * b); |
| 215 |
} |
| 216 |
|
| 217 |
/* double-double * double */ |
| 218 |
inline dd_real operator*(const dd_real &a, double b) { |
| 219 |
double p1, p2; |
| 220 |
|
| 221 |
p1 = qd::two_prod(a.x[0], b, p2); |
| 222 |
p2 += (a.x[1] * b); |
| 223 |
p1 = qd::quick_two_sum(p1, p2, p2); |
| 224 |
return dd_real(p1, p2); |
| 225 |
} |
| 226 |
|
| 227 |
/* double-double * double-double */ |
| 228 |
inline dd_real operator*(const dd_real &a, const dd_real &b) { |
| 229 |
double p1, p2; |
| 230 |
|
| 231 |
p1 = qd::two_prod(a.x[0], b.x[0], p2); |
| 232 |
p2 += (a.x[0] * b.x[1] + a.x[1] * b.x[0]); |
| 233 |
p1 = qd::quick_two_sum(p1, p2, p2); |
| 234 |
return dd_real(p1, p2); |
| 235 |
} |
| 236 |
|
| 237 |
/* double * double-double */ |
| 238 |
inline dd_real operator*(double a, const dd_real &b) { |
| 239 |
return (b * a); |
| 240 |
} |
| 241 |
|
| 242 |
/*********** Self-Multiplications ************/ |
| 243 |
/* double-double *= double */ |
| 244 |
inline dd_real &dd_real::operator*=(double a) { |
| 245 |
double p1, p2; |
| 246 |
p1 = qd::two_prod(x[0], a, p2); |
| 247 |
p2 += x[1] * a; |
| 248 |
x[0] = qd::quick_two_sum(p1, p2, x[1]); |
| 249 |
return *this; |
| 250 |
} |
| 251 |
|
| 252 |
/* double-double *= double-double */ |
| 253 |
inline dd_real &dd_real::operator*=(const dd_real &a) { |
| 254 |
double p1, p2; |
| 255 |
p1 = qd::two_prod(x[0], a.x[0], p2); |
| 256 |
p2 += a.x[1] * x[0]; |
| 257 |
p2 += a.x[0] * x[1]; |
| 258 |
x[0] = qd::quick_two_sum(p1, p2, x[1]); |
| 259 |
return *this; |
| 260 |
} |
| 261 |
|
| 262 |
/*********** Divisions ************/ |
| 263 |
inline dd_real dd_real::div(double a, double b) { |
| 264 |
double q1, q2; |
| 265 |
double p1, p2; |
| 266 |
double s, e; |
| 267 |
|
| 268 |
q1 = a / b; |
| 269 |
|
| 270 |
/* Compute a - q1 * b */ |
| 271 |
p1 = qd::two_prod(q1, b, p2); |
| 272 |
s = qd::two_diff(a, p1, e); |
| 273 |
e -= p2; |
| 274 |
|
| 275 |
/* get next approximation */ |
| 276 |
q2 = (s + e) / b; |
| 277 |
|
| 278 |
s = qd::quick_two_sum(q1, q2, e); |
| 279 |
|
| 280 |
return dd_real(s, e); |
| 281 |
} |
| 282 |
|
| 283 |
/* double-double / double */ |
| 284 |
inline dd_real operator/(const dd_real &a, double b) { |
| 285 |
|
| 286 |
double q1, q2; |
| 287 |
double p1, p2; |
| 288 |
double s, e; |
| 289 |
dd_real r; |
| 290 |
|
| 291 |
q1 = a.x[0] / b; /* approximate quotient. */ |
| 292 |
|
| 293 |
/* Compute this - q1 * d */ |
| 294 |
p1 = qd::two_prod(q1, b, p2); |
| 295 |
s = qd::two_diff(a.x[0], p1, e); |
| 296 |
e += a.x[1]; |
| 297 |
e -= p2; |
| 298 |
|
| 299 |
/* get next approximation. */ |
| 300 |
q2 = (s + e) / b; |
| 301 |
|
| 302 |
/* renormalize */ |
| 303 |
r.x[0] = qd::quick_two_sum(q1, q2, r.x[1]); |
| 304 |
|
| 305 |
return r; |
| 306 |
} |
| 307 |
|
| 308 |
inline dd_real dd_real::sloppy_div(const dd_real &a, const dd_real &b) { |
| 309 |
double s1, s2; |
| 310 |
double q1, q2; |
| 311 |
dd_real r; |
| 312 |
|
| 313 |
q1 = a.x[0] / b.x[0]; /* approximate quotient */ |
| 314 |
|
| 315 |
/* compute this - q1 * dd */ |
| 316 |
r = b * q1; |
| 317 |
s1 = qd::two_diff(a.x[0], r.x[0], s2); |
| 318 |
s2 -= r.x[1]; |
| 319 |
s2 += a.x[1]; |
| 320 |
|
| 321 |
/* get next approximation */ |
| 322 |
q2 = (s1 + s2) / b.x[0]; |
| 323 |
|
| 324 |
/* renormalize */ |
| 325 |
r.x[0] = qd::quick_two_sum(q1, q2, r.x[1]); |
| 326 |
return r; |
| 327 |
} |
| 328 |
|
| 329 |
inline dd_real dd_real::accurate_div(const dd_real &a, const dd_real &b) { |
| 330 |
double q1, q2, q3; |
| 331 |
dd_real r; |
| 332 |
|
| 333 |
q1 = a.x[0] / b.x[0]; /* approximate quotient */ |
| 334 |
|
| 335 |
r = a - q1 * b; |
| 336 |
|
| 337 |
q2 = r.x[0] / b.x[0]; |
| 338 |
r -= (q2 * b); |
| 339 |
|
| 340 |
q3 = r.x[0] / b.x[0]; |
| 341 |
|
| 342 |
q1 = qd::quick_two_sum(q1, q2, q2); |
| 343 |
r = dd_real(q1, q2) + q3; |
| 344 |
return r; |
| 345 |
} |
| 346 |
|
| 347 |
/* double-double / double-double */ |
| 348 |
inline dd_real operator/(const dd_real &a, const dd_real &b) { |
| 349 |
#ifdef QD_SLOPPY_DIV |
| 350 |
return dd_real::sloppy_div(a, b); |
| 351 |
#else |
| 352 |
return dd_real::accurate_div(a, b); |
| 353 |
#endif |
| 354 |
} |
| 355 |
|
| 356 |
/* double / double-double */ |
| 357 |
inline dd_real operator/(double a, const dd_real &b) { |
| 358 |
return dd_real(a) / b; |
| 359 |
} |
| 360 |
|
| 361 |
inline dd_real inv(const dd_real &a) { |
| 362 |
return 1.0 / a; |
| 363 |
} |
| 364 |
|
| 365 |
/*********** Self-Divisions ************/ |
| 366 |
/* double-double /= double */ |
| 367 |
inline dd_real &dd_real::operator/=(double a) { |
| 368 |
*this = *this / a; |
| 369 |
return *this; |
| 370 |
} |
| 371 |
|
| 372 |
/* double-double /= double-double */ |
| 373 |
inline dd_real &dd_real::operator/=(const dd_real &a) { |
| 374 |
*this = *this / a; |
| 375 |
return *this; |
| 376 |
} |
| 377 |
|
| 378 |
/********** Remainder **********/ |
| 379 |
inline dd_real drem(const dd_real &a, const dd_real &b) { |
| 380 |
dd_real n = nint(a / b); |
| 381 |
return (a - n * b); |
| 382 |
} |
| 383 |
|
| 384 |
inline dd_real divrem(const dd_real &a, const dd_real &b, dd_real &r) { |
| 385 |
dd_real n = nint(a / b); |
| 386 |
r = a - n * b; |
| 387 |
return n; |
| 388 |
} |
| 389 |
|
| 390 |
/*********** Squaring **********/ |
| 391 |
inline dd_real sqr(const dd_real &a) { |
| 392 |
double p1, p2; |
| 393 |
double s1, s2; |
| 394 |
p1 = qd::two_sqr(a.x[0], p2); |
| 395 |
p2 += 2.0 * a.x[0] * a.x[1]; |
| 396 |
p2 += a.x[1] * a.x[1]; |
| 397 |
s1 = qd::quick_two_sum(p1, p2, s2); |
| 398 |
return dd_real(s1, s2); |
| 399 |
} |
| 400 |
|
| 401 |
inline dd_real dd_real::sqr(double a) { |
| 402 |
double p1, p2; |
| 403 |
p1 = qd::two_sqr(a, p2); |
| 404 |
return dd_real(p1, p2); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
/********** Exponentiation **********/ |
| 409 |
inline dd_real dd_real::operator^(int n) { |
| 410 |
return npwr(*this, n); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
/*********** Assignments ************/ |
| 415 |
/* double-double = double */ |
| 416 |
inline dd_real &dd_real::operator=(double a) { |
| 417 |
x[0] = a; |
| 418 |
x[1] = 0.0; |
| 419 |
return *this; |
| 420 |
} |
| 421 |
|
| 422 |
/*********** Equality Comparisons ************/ |
| 423 |
inline bool operator==(const dd_real &a, int b) { |
| 424 |
return (a.x[0] == double(b) && a.x[1] == 0.0); |
| 425 |
} |
| 426 |
inline bool operator==(int a, const dd_real &b) { |
| 427 |
return (double(a) == b.x[0] && b.x[1] == 0.0); |
| 428 |
} |
| 429 |
/* double-double == double */ |
| 430 |
inline bool operator==(const dd_real &a, double b) { |
| 431 |
return (a.x[0] == b && a.x[1] == 0.0); |
| 432 |
} |
| 433 |
|
| 434 |
/* double-double == double-double */ |
| 435 |
inline bool operator==(const dd_real &a, const dd_real &b) { |
| 436 |
return (a.x[0] == b.x[0] && a.x[1] == b.x[1]); |
| 437 |
} |
| 438 |
|
| 439 |
/* double == double-double */ |
| 440 |
inline bool operator==(double a, const dd_real &b) { |
| 441 |
return (a == b.x[0] && b.x[1] == 0.0); |
| 442 |
} |
| 443 |
|
| 444 |
/*********** Greater-Than Comparisons ************/ |
| 445 |
inline bool operator>(const dd_real &a, int b) { |
| 446 |
return (a.x[0] > double(b) || (a.x[0] == double(b) && a.x[1] > 0.0)); |
| 447 |
} |
| 448 |
inline bool operator>(int a, const dd_real &b) { |
| 449 |
return (double(a) > b.x[0] || (double(a) == b.x[0] && b.x[1] < 0.0)); |
| 450 |
} |
| 451 |
/* double-double > double */ |
| 452 |
inline bool operator>(const dd_real &a, double b) { |
| 453 |
return (a.x[0] > b || (a.x[0] == b && a.x[1] > 0.0)); |
| 454 |
} |
| 455 |
|
| 456 |
/* double-double > double-double */ |
| 457 |
inline bool operator>(const dd_real &a, const dd_real &b) { |
| 458 |
return (a.x[0] > b.x[0] || (a.x[0] == b.x[0] && a.x[1] > b.x[1])); |
| 459 |
} |
| 460 |
|
| 461 |
/* double > double-double */ |
| 462 |
inline bool operator>(double a, const dd_real &b) { |
| 463 |
return (a > b.x[0] || (a == b.x[0] && b.x[1] < 0.0)); |
| 464 |
} |
| 465 |
|
| 466 |
/*********** Less-Than Comparisons ************/ |
| 467 |
inline bool operator<(const dd_real &a, int b) { |
| 468 |
return (a.x[0] < double(b) || (a.x[0] == double(b) && a.x[1] < 0.0)); |
| 469 |
} |
| 470 |
inline bool operator<(int a, const dd_real &b) { |
| 471 |
return (double(a) < b.x[0] || (double(a) == b.x[0] && b.x[1] > 0.0)); |
| 472 |
} |
| 473 |
|
| 474 |
/* double-double < double */ |
| 475 |
inline bool operator<(const dd_real &a, double b) { |
| 476 |
return (a.x[0] < b || (a.x[0] == b && a.x[1] < 0.0)); |
| 477 |
} |
| 478 |
|
| 479 |
/* double-double < double-double */ |
| 480 |
inline bool operator<(const dd_real &a, const dd_real &b) { |
| 481 |
return (a.x[0] < b.x[0] || (a.x[0] == b.x[0] && a.x[1] < b.x[1])); |
| 482 |
} |
| 483 |
|
| 484 |
/* double < double-double */ |
| 485 |
inline bool operator<(double a, const dd_real &b) { |
| 486 |
return (a < b.x[0] || (a == b.x[0] && b.x[1] > 0.0)); |
| 487 |
} |
| 488 |
|
| 489 |
/*********** Greater-Than-Or-Equal-To Comparisons ************/ |
| 490 |
inline bool operator>=(const dd_real &a, int b) { |
| 491 |
return ((a > b) || (a == b)); |
| 492 |
} |
| 493 |
inline bool operator>=(int a, const dd_real &b) { |
| 494 |
return ((b < a) || (b == a)); |
| 495 |
} |
| 496 |
|
| 497 |
/* double-double >= double */ |
| 498 |
inline bool operator>=(const dd_real &a, double b) { |
| 499 |
return (a.x[0] > b || (a.x[0] == b && a.x[1] >= 0.0)); |
| 500 |
} |
| 501 |
|
| 502 |
/* double-double >= double-double */ |
| 503 |
inline bool operator>=(const dd_real &a, const dd_real &b) { |
| 504 |
return (a.x[0] > b.x[0] || (a.x[0] == b.x[0] && a.x[1] >= b.x[1])); |
| 505 |
} |
| 506 |
|
| 507 |
/* double >= double-double */ |
| 508 |
inline bool operator>=(double a, const dd_real &b) { |
| 509 |
return (b <= a); |
| 510 |
} |
| 511 |
|
| 512 |
/*********** Less-Than-Or-Equal-To Comparisons ************/ |
| 513 |
|
| 514 |
inline bool operator<=(const dd_real &a, int b) { |
| 515 |
return ((a < b) || (a == b)); |
| 516 |
} |
| 517 |
inline bool operator<=(int a, const dd_real &b) { |
| 518 |
return ((b > a) || (b == a)); |
| 519 |
} |
| 520 |
|
| 521 |
/* double-double <= double */ |
| 522 |
inline bool operator<=(const dd_real &a, double b) { |
| 523 |
return (a.x[0] < b || (a.x[0] == b && a.x[1] <= 0.0)); |
| 524 |
} |
| 525 |
|
| 526 |
/* double-double <= double-double */ |
| 527 |
inline bool operator<=(const dd_real &a, const dd_real &b) { |
| 528 |
return (a.x[0] < b.x[0] || (a.x[0] == b.x[0] && a.x[1] <= b.x[1])); |
| 529 |
} |
| 530 |
|
| 531 |
/* double <= double-double */ |
| 532 |
inline bool operator<=(double a, const dd_real &b) { |
| 533 |
return (b >= a); |
| 534 |
} |
| 535 |
|
| 536 |
/*********** Not-Equal-To Comparisons ************/ |
| 537 |
inline bool operator!=(const dd_real &a, int b) { |
| 538 |
return (a.x[0] != double(b) || a.x[1] != 0.0); |
| 539 |
} |
| 540 |
inline bool operator!=(int a, const dd_real &b) { |
| 541 |
return (double(a) != b.x[0] || b.x[1] != 0.0); |
| 542 |
} |
| 543 |
|
| 544 |
/* double-double != double */ |
| 545 |
inline bool operator!=(const dd_real &a, double b) { |
| 546 |
return (a.x[0] != b || a.x[1] != 0.0); |
| 547 |
} |
| 548 |
|
| 549 |
/* double-double != double-double */ |
| 550 |
inline bool operator!=(const dd_real &a, const dd_real &b) { |
| 551 |
return (a.x[0] != b.x[0] || a.x[1] != b.x[1]); |
| 552 |
} |
| 553 |
|
| 554 |
/* double != double-double */ |
| 555 |
inline bool operator!=(double a, const dd_real &b) { |
| 556 |
return (a != b.x[0] || b.x[1] != 0.0); |
| 557 |
} |
| 558 |
|
| 559 |
/*********** Micellaneous ************/ |
| 560 |
/* this == 0 */ |
| 561 |
inline bool dd_real::is_zero() const { |
| 562 |
return (x[0] == 0.0); |
| 563 |
} |
| 564 |
|
| 565 |
/* this == 1 */ |
| 566 |
inline bool dd_real::is_one() const { |
| 567 |
return (x[0] == 1.0 && x[1] == 0.0); |
| 568 |
} |
| 569 |
|
| 570 |
/* this > 0 */ |
| 571 |
inline bool dd_real::is_positive() const { |
| 572 |
return (x[0] > 0.0); |
| 573 |
} |
| 574 |
|
| 575 |
/* this < 0 */ |
| 576 |
inline bool dd_real::is_negative() const { |
| 577 |
return (x[0] < 0.0); |
| 578 |
} |
| 579 |
|
| 580 |
/* Absolute value */ |
| 581 |
inline dd_real abs(const dd_real &a) { |
| 582 |
return (a.x[0] < 0.0) ? -a : a; |
| 583 |
} |
| 584 |
|
| 585 |
inline dd_real fabs(const dd_real &a) { |
| 586 |
return abs(a); |
| 587 |
} |
| 588 |
|
| 589 |
/* Round to Nearest integer */ |
| 590 |
inline dd_real nint(const dd_real &a) { |
| 591 |
double hi = qd::nint(a.x[0]); |
| 592 |
double lo; |
| 593 |
|
| 594 |
if (hi == a.x[0]) { |
| 595 |
/* High word is an integer already. Round the low word.*/ |
| 596 |
lo = qd::nint(a.x[1]); |
| 597 |
|
| 598 |
/* Renormalize. This is needed if x[0] = some integer, x[1] = 1/2.*/ |
| 599 |
hi = qd::quick_two_sum(hi, lo, lo); |
| 600 |
} else { |
| 601 |
/* High word is not an integer. */ |
| 602 |
lo = 0.0; |
| 603 |
if (std::abs(hi-a.x[0]) == 0.5 && a.x[1] < 0.0) { |
| 604 |
/* There is a tie in the high word, consult the low word |
| 605 |
to break the tie. */ |
| 606 |
hi -= 1.0; /* NOTE: This does not cause INEXACT. */ |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
return dd_real(hi, lo); |
| 611 |
} |
| 612 |
|
| 613 |
inline dd_real floor(const dd_real &a) { |
| 614 |
double hi = std::floor(a.x[0]); |
| 615 |
double lo = 0.0; |
| 616 |
|
| 617 |
if (hi == a.x[0]) { |
| 618 |
/* High word is integer already. Round the low word. */ |
| 619 |
lo = std::floor(a.x[1]); |
| 620 |
hi = qd::quick_two_sum(hi, lo, lo); |
| 621 |
} |
| 622 |
|
| 623 |
return dd_real(hi, lo); |
| 624 |
} |
| 625 |
|
| 626 |
inline dd_real ceil(const dd_real &a) { |
| 627 |
double hi = std::ceil(a.x[0]); |
| 628 |
double lo = 0.0; |
| 629 |
|
| 630 |
if (hi == a.x[0]) { |
| 631 |
/* High word is integer already. Round the low word. */ |
| 632 |
lo = std::ceil(a.x[1]); |
| 633 |
hi = qd::quick_two_sum(hi, lo, lo); |
| 634 |
} |
| 635 |
|
| 636 |
return dd_real(hi, lo); |
| 637 |
} |
| 638 |
|
| 639 |
inline dd_real aint(const dd_real &a) { |
| 640 |
return (a.x[0] >= 0.0) ? floor(a) : ceil(a); |
| 641 |
} |
| 642 |
|
| 643 |
/* Cast to double. */ |
| 644 |
inline double to_double(const dd_real &a) { |
| 645 |
return a.x[0]; |
| 646 |
} |
| 647 |
|
| 648 |
/* Cast to int. */ |
| 649 |
inline int to_int(const dd_real &a) { |
| 650 |
return static_cast<int>(a.x[0]); |
| 651 |
} |
| 652 |
|
| 653 |
/* Random number generator */ |
| 654 |
inline dd_real dd_real::rand() { |
| 655 |
return ddrand(); |
| 656 |
} |
| 657 |
|
| 658 |
#endif /* _QD_DD_INLINE_H */ |