| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2023 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifndef GEOGRAM_BASIC_RATIONALG | ||
| 41 | #define GEOGRAM_BASIC_RATIONALG | ||
| 42 | |||
| 43 | #include <geogram/basic/common.h> | ||
| 44 | #include <geogram/basic/numeric.h> | ||
| 45 | |||
| 46 | /** | ||
| 47 | * \file geogram/basic/rationalg.h | ||
| 48 | * \brief Generic implementation of rational type | ||
| 49 | */ | ||
| 50 | |||
| 51 | namespace GEO { | ||
| 52 | |||
| 53 | /** | ||
| 54 | * \brief rationalg (generic rational) is used to compute the | ||
| 55 | * sign of rational fractions exactly. | ||
| 56 | * \details rationalg can be used like float and double. It supports | ||
| 57 | * four arithmetic operations (+,-,*,/), comparisons (>,>=,<,<=,==,!=) | ||
| 58 | * and exact sign computation. | ||
| 59 | */ | ||
| 60 | template <class T> class rationalg { | ||
| 61 | public: | ||
| 62 | typedef T value_type; | ||
| 63 | |||
| 64 | ✗ | rationalg() = default; | |
| 65 | |||
| 66 | /** | ||
| 67 | * \brief Constructs a new rationalg from a double. | ||
| 68 | * \param[in] x the value to initialize this rationalg. | ||
| 69 | */ | ||
| 70 | ✗ | explicit rationalg(double x) : num_(x), denom_(1.0) { | |
| 71 | ✗ | } | |
| 72 | |||
| 73 | /** | ||
| 74 | * \brief Constructs a new rationalg from an T. | ||
| 75 | * \param[in] x the value to initialize this rationalg. | ||
| 76 | */ | ||
| 77 | explicit rationalg(const T& x) : num_(x), denom_(1.0) { | ||
| 78 | } | ||
| 79 | |||
| 80 | /** | ||
| 81 | * \brief Constructs a new rationalg from an T | ||
| 82 | * with move semantics | ||
| 83 | * \param[in] x the victim T | ||
| 84 | */ | ||
| 85 | explicit rationalg(T&& x) : num_(x), denom_(1.0) { | ||
| 86 | } | ||
| 87 | |||
| 88 | /** | ||
| 89 | * \brief Constructs a new rationalg from two doubles. | ||
| 90 | * \param[in] num the numerator | ||
| 91 | * \param[in] denom the denominator | ||
| 92 | */ | ||
| 93 | explicit rationalg(double num, double denom) | ||
| 94 | : num_(num), denom_(denom) { | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * \brief Constructs a new rationalg from two T. | ||
| 99 | * \param[in] num the numerator | ||
| 100 | * \param[in] denom the denominator | ||
| 101 | */ | ||
| 102 | 222821 | explicit rationalg(const T& num, const T& denom) | |
| 103 |
1/2✓ Branch 2 taken 222821 times.
✗ Branch 3 not taken.
|
222821 | : num_(num), denom_(denom) { |
| 104 | 222821 | } | |
| 105 | |||
| 106 | /** | ||
| 107 | * \brief Constructs a new rationalg from two T with | ||
| 108 | * move semantics | ||
| 109 | * \param[in] num the numerator | ||
| 110 | * \param[in] denom the denominator | ||
| 111 | */ | ||
| 112 | 81 | explicit rationalg( | |
| 113 | T&& num, T&& denom | ||
| 114 |
1/2✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
|
81 | ) : num_(num), denom_(denom) { |
| 115 | 81 | } | |
| 116 | |||
| 117 | /** | ||
| 118 | * \brief Copy-constructor. | ||
| 119 | * \param[in] rhs the rational to be copied | ||
| 120 | */ | ||
| 121 | rationalg(const rationalg<T>& rhs) = default; | ||
| 122 | |||
| 123 | /** | ||
| 124 | * \brief Move-constructor. | ||
| 125 | * \param[in] rhs the rational to be copied | ||
| 126 | */ | ||
| 127 | rationalg(rationalg<T>&& rhs) = default; | ||
| 128 | |||
| 129 | /** | ||
| 130 | * \brief Assignment operator. | ||
| 131 | * \param[in] rhs the rational to be copied | ||
| 132 | * \return the new value of this rational (rhs) | ||
| 133 | */ | ||
| 134 | rationalg<T>& operator= (const rationalg<T>& rhs) = default; | ||
| 135 | |||
| 136 | /** | ||
| 137 | * \brief Assignment operator with move semantics | ||
| 138 | * \param[in] rhs the victim rationalg | ||
| 139 | * \return the new value of this rational (rhs) | ||
| 140 | */ | ||
| 141 | rationalg<T>& operator= (rationalg<T>&& rhs) = default; | ||
| 142 | |||
| 143 | /** | ||
| 144 | * \brief gets the numerator. | ||
| 145 | * \return a const reference to the numerator. | ||
| 146 | */ | ||
| 147 | 222983 | const T& num() const { | |
| 148 | 222983 | return num_; | |
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * \brief gets the denominator. | ||
| 153 | * \return a const reference to the denominator. | ||
| 154 | */ | ||
| 155 | 222983 | const T& denom() const { | |
| 156 | 222983 | return denom_; | |
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * \brief gets the numerator. | ||
| 161 | * \return a reference to the numerator. | ||
| 162 | */ | ||
| 163 | T& num() { | ||
| 164 | return num_; | ||
| 165 | } | ||
| 166 | |||
| 167 | /** | ||
| 168 | * \brief gets the denominator. | ||
| 169 | * \return a reference to the denominator. | ||
| 170 | */ | ||
| 171 | T& denom() { | ||
| 172 | return denom_; | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 176 | * \brief Optimizes the internal representation without changing the | ||
| 177 | * represented value | ||
| 178 | */ | ||
| 179 | void optimize() { | ||
| 180 | Numeric::optimize_number_representation(num_); | ||
| 181 | Numeric::optimize_number_representation(denom_); | ||
| 182 | } | ||
| 183 | |||
| 184 | /********************************************************************/ | ||
| 185 | |||
| 186 | /** | ||
| 187 | * \brief Adds a rationalg to this rationalg | ||
| 188 | * \param[in] rhs the rationalg to be added to this rationalg | ||
| 189 | * \return the new value of this rationalg | ||
| 190 | */ | ||
| 191 | rationalg<T>& operator+= (const rationalg<T>& rhs) { | ||
| 192 | if(has_same_denom(rhs)) { | ||
| 193 | num_ += rhs.num_; | ||
| 194 | } else { | ||
| 195 | num_ = num_ * rhs.denom_ + rhs.num_ * denom_; | ||
| 196 | denom_ *= rhs.denom_; | ||
| 197 | } | ||
| 198 | return *this; | ||
| 199 | } | ||
| 200 | |||
| 201 | /** | ||
| 202 | * \brief Subtracts a rationalg to this rationalg | ||
| 203 | * \param[in] rhs the rationalg to be subtracted | ||
| 204 | * \return the new value of this rationalg | ||
| 205 | */ | ||
| 206 | rationalg<T>& operator-= (const rationalg<T>& rhs) { | ||
| 207 | if(has_same_denom(rhs)) { | ||
| 208 | num_ -= rhs.num_; | ||
| 209 | } else { | ||
| 210 | num_ = num_ * rhs.denom_ - rhs.num_ * denom_; | ||
| 211 | denom_ *= rhs.denom_; | ||
| 212 | } | ||
| 213 | return *this; | ||
| 214 | } | ||
| 215 | |||
| 216 | /** | ||
| 217 | * \brief Multiplies this rationalg by a rationalg | ||
| 218 | * \param[in] rhs the rationalg to multiply this rationalg by | ||
| 219 | * \return the new value of this rationalg | ||
| 220 | */ | ||
| 221 | rationalg<T>& operator*= (const rationalg<T>& rhs) { | ||
| 222 | num_ *= rhs.num_; | ||
| 223 | denom_ *= rhs.denom_; | ||
| 224 | return *this; | ||
| 225 | } | ||
| 226 | |||
| 227 | /** | ||
| 228 | * \brief Divides this rationalg by a rationalg | ||
| 229 | * \param[in] rhs the rationalg to divide this rationalg by | ||
| 230 | * \return the new value of this rationalg | ||
| 231 | */ | ||
| 232 | rationalg<T>& operator/= (const rationalg<T>& rhs) { | ||
| 233 | num_ *= rhs.denom_; | ||
| 234 | denom_ *= rhs.num_; | ||
| 235 | return *this; | ||
| 236 | } | ||
| 237 | |||
| 238 | /** | ||
| 239 | * \brief Adds a double to this rationalg | ||
| 240 | * \param[in] rhs the double to be added to this rationalg | ||
| 241 | * \return the new value of this rationalg | ||
| 242 | */ | ||
| 243 | rationalg<T>& operator+= (double rhs) { | ||
| 244 | num_ += denom_ * T(rhs); | ||
| 245 | return *this; | ||
| 246 | } | ||
| 247 | |||
| 248 | /** | ||
| 249 | * \brief Subtracts a double from this rationalg | ||
| 250 | * \param[in] rhs the double to be subtracted from this rationalg | ||
| 251 | * \return the new value of this rationalg | ||
| 252 | */ | ||
| 253 | rationalg<T>& operator-= (double rhs) { | ||
| 254 | num_ -= denom_ * T(rhs); | ||
| 255 | return *this; | ||
| 256 | } | ||
| 257 | |||
| 258 | /** | ||
| 259 | * \brief Multiplies this rationalg by a double | ||
| 260 | * \details If the double is a constant (possibly negative) power of | ||
| 261 | * two (e.g. 0.125, 0.5, 2.0, 4.0 ...), one may use | ||
| 262 | * num().scale_fast() / denom().scale_fast() instead. | ||
| 263 | * \param[in] rhs the double to multiply this rationalg with | ||
| 264 | * \return the new value of this rationalg | ||
| 265 | */ | ||
| 266 | rationalg<T>& operator*= (double rhs) { | ||
| 267 | num_ *= T(rhs); | ||
| 268 | return *this; | ||
| 269 | } | ||
| 270 | |||
| 271 | /** | ||
| 272 | * \brief Divides this rationalg by a double | ||
| 273 | * \details If the double is a constant (possibly negative) power of | ||
| 274 | * two (e.g. 0.125, 0.5, 2.0, 4.0 ...), one may use | ||
| 275 | * num().scale_fast() / denom().scale_fast() instead. | ||
| 276 | * \param[in] rhs the double to multiply this rationalg with | ||
| 277 | * \return the new value of this rationalg | ||
| 278 | */ | ||
| 279 | rationalg<T>& operator/= (double rhs) { | ||
| 280 | denom_ *= T(rhs); | ||
| 281 | return *this; | ||
| 282 | } | ||
| 283 | |||
| 284 | /********************************************************************/ | ||
| 285 | |||
| 286 | /** | ||
| 287 | * \brief Computes the sum of two rationalg%s | ||
| 288 | * \param[in] rhs the rationalg to be added to this rationalg | ||
| 289 | * \return the sum of this rationalg and \p rhs | ||
| 290 | */ | ||
| 291 | ✗ | rationalg<T> operator+ (const rationalg<T>& rhs) const { | |
| 292 | ✗ | if(has_same_denom(rhs)) { | |
| 293 | return rationalg( | ||
| 294 | ✗ | num_ + rhs.num_, | |
| 295 | ✗ | denom_ | |
| 296 | ✗ | ); | |
| 297 | } | ||
| 298 | return rationalg( | ||
| 299 | ✗ | num_ * rhs.denom_ + rhs.num_ * denom_, | |
| 300 | ✗ | denom_ * rhs.denom_ | |
| 301 | ✗ | ); | |
| 302 | } | ||
| 303 | |||
| 304 | /** | ||
| 305 | * \brief Computes the difference between two rationalg%s | ||
| 306 | * \param[in] rhs the rationalg to be subtracted from | ||
| 307 | * this rationalg | ||
| 308 | * \return the difference between this rationalg and \p rhs | ||
| 309 | */ | ||
| 310 | ✗ | rationalg<T> operator- (const rationalg<T>& rhs) const { | |
| 311 | ✗ | if(has_same_denom(rhs)) { | |
| 312 | return rationalg( | ||
| 313 | ✗ | num_ - rhs.num_, | |
| 314 | ✗ | denom_ | |
| 315 | ✗ | ); | |
| 316 | } | ||
| 317 | return rationalg( | ||
| 318 | ✗ | num_ * rhs.denom_ - rhs.num_ * denom_, | |
| 319 | ✗ | denom_ * rhs.denom_ | |
| 320 | ✗ | ); | |
| 321 | } | ||
| 322 | |||
| 323 | /** | ||
| 324 | * \brief Computes the product between two rationalg%s | ||
| 325 | * \param[in] rhs the rationalg to be multiplied by | ||
| 326 | * this rationalg | ||
| 327 | * \return the product between this rationalg and \p rhs | ||
| 328 | */ | ||
| 329 | rationalg<T> operator* (const rationalg<T>& rhs) const { | ||
| 330 | return rationalg( | ||
| 331 | num_ * rhs.num_, | ||
| 332 | denom_ * rhs.denom_ | ||
| 333 | ); | ||
| 334 | } | ||
| 335 | |||
| 336 | /** | ||
| 337 | * \brief Computes the ratio between two rationalg%s | ||
| 338 | * \param[in] rhs the rationalg to be multiplied by | ||
| 339 | * this rationalg | ||
| 340 | * \return the ratio between this rationalg and \p rhs | ||
| 341 | */ | ||
| 342 | ✗ | rationalg<T> operator/ (const rationalg<T>& rhs) const { | |
| 343 | return rationalg( | ||
| 344 | ✗ | num_ * rhs.denom_, | |
| 345 | ✗ | denom_ * rhs.num_ | |
| 346 | ✗ | ); | |
| 347 | } | ||
| 348 | |||
| 349 | |||
| 350 | /** | ||
| 351 | * \brief Computes the sum of a rationalg and a double. | ||
| 352 | * \param[in] rhs the double to be added to this rationalg | ||
| 353 | * \return the sum of this rationalg and \p rhs | ||
| 354 | */ | ||
| 355 | rationalg<T> operator+ (double rhs) const { | ||
| 356 | return rationalg( | ||
| 357 | num_ + T(rhs) * denom_, | ||
| 358 | denom_ | ||
| 359 | ); | ||
| 360 | } | ||
| 361 | |||
| 362 | /** | ||
| 363 | * \brief Computes the difference between a rationalg and a double. | ||
| 364 | * \param[in] rhs the double to be subtracted from this rationalg | ||
| 365 | * \return the difference between this rationalg and \p rhs | ||
| 366 | */ | ||
| 367 | rationalg<T> operator- (double rhs) const { | ||
| 368 | return rationalg( | ||
| 369 | num_ - T(rhs) * denom_, | ||
| 370 | denom_ | ||
| 371 | ); | ||
| 372 | } | ||
| 373 | |||
| 374 | /** | ||
| 375 | * \brief Computes the product between a rationalg and a double. | ||
| 376 | * \param[in] rhs the double to be multiplied by this rationalg | ||
| 377 | * \return the product between this rationalg and \p rhs | ||
| 378 | */ | ||
| 379 | rationalg<T> operator* (double rhs) const { | ||
| 380 | return rationalg( | ||
| 381 | num_ * T(rhs), | ||
| 382 | denom_ | ||
| 383 | ); | ||
| 384 | } | ||
| 385 | |||
| 386 | /** | ||
| 387 | * \brief Computes the ratio between a rationalg and a double. | ||
| 388 | * \param[in] rhs the double to be multiplied by this rationalg | ||
| 389 | * \return the ratio between this rationalg and \p rhs | ||
| 390 | */ | ||
| 391 | rationalg<T> operator/ (double rhs) const { | ||
| 392 | return rationalg( | ||
| 393 | num_, | ||
| 394 | denom_* T(rhs) | ||
| 395 | ); | ||
| 396 | } | ||
| 397 | |||
| 398 | /********************************************************************/ | ||
| 399 | |||
| 400 | /** | ||
| 401 | * \brief Computes the opposite of this rationalg. | ||
| 402 | * \return the opposite of this rationalg | ||
| 403 | */ | ||
| 404 | rationalg<T> operator- () const { | ||
| 405 | return rationalg( | ||
| 406 | -num_, | ||
| 407 | denom_ | ||
| 408 | ); | ||
| 409 | } | ||
| 410 | |||
| 411 | /********************************************************************/ | ||
| 412 | |||
| 413 | /** | ||
| 414 | * \brief Gets the sign of a rationalg | ||
| 415 | * \return one of POSITIVE,ZERO,NEGATIVE | ||
| 416 | */ | ||
| 417 | ✗ | Sign sign() const { | |
| 418 | ✗ | geo_debug_assert(denom_.sign() != ZERO); | |
| 419 | ✗ | return Sign(num_.sign() * denom_.sign()); | |
| 420 | } | ||
| 421 | |||
| 422 | /********************************************************************/ | ||
| 423 | |||
| 424 | /** | ||
| 425 | * \brief Compares two rationalg | ||
| 426 | * \return the sign of this rationalg minus rhs | ||
| 427 | */ | ||
| 428 | ✗ | Sign compare(const rationalg<T>& rhs) const { | |
| 429 | ✗ | if(sign() != rhs.sign()){ | |
| 430 | ✗ | return Sign(sign()-rhs.sign()); | |
| 431 | } | ||
| 432 | ✗ | if(has_same_denom(rhs)) { | |
| 433 | ✗ | return Sign(num_.compare(rhs.num_) * denom_.sign()); | |
| 434 | } | ||
| 435 | return Sign( | ||
| 436 | ✗ | (num_ * rhs.denom_).compare(rhs.num_ * denom_) * | |
| 437 | ✗ | denom_.sign() * rhs.denom_.sign() | |
| 438 | ✗ | ); | |
| 439 | } | ||
| 440 | |||
| 441 | /** | ||
| 442 | * \brief Compares a rationalg with a double | ||
| 443 | * \return the sign of this expansion minus rhs | ||
| 444 | */ | ||
| 445 | Sign compare(double rhs) const { | ||
| 446 | return Sign( | ||
| 447 | num_.compare(T(rhs)*denom_) * denom_.sign() | ||
| 448 | ); | ||
| 449 | } | ||
| 450 | |||
| 451 | /** | ||
| 452 | * \brief Compares this rationalg with another one. | ||
| 453 | * \details Internally computes the sign of the difference | ||
| 454 | * between this rationalg and \p rhs. | ||
| 455 | * \return true if this rationalg is greater than \p rhs, | ||
| 456 | * false otherwise | ||
| 457 | */ | ||
| 458 | bool operator> (const rationalg<T>& rhs) const { | ||
| 459 | return (int(compare(rhs))>0); | ||
| 460 | } | ||
| 461 | |||
| 462 | /** | ||
| 463 | * \brief Compares this rationalg with another one. | ||
| 464 | * \details Internally computes the sign of the difference | ||
| 465 | * between this rationalg and \p rhs. | ||
| 466 | * \return true if this rationalg is greater or equal than \p rhs, | ||
| 467 | * false otherwise | ||
| 468 | */ | ||
| 469 | bool operator>= (const rationalg<T>& rhs) const { | ||
| 470 | return (int(compare(rhs))>=0); | ||
| 471 | } | ||
| 472 | |||
| 473 | /** | ||
| 474 | * \brief Compares this rationalg with another one. | ||
| 475 | * \details Internally computes the sign of the difference | ||
| 476 | * between this rationalg and \p rhs. | ||
| 477 | * \return true if this rationalg is smaller than \p rhs, | ||
| 478 | * false otherwise | ||
| 479 | */ | ||
| 480 | bool operator< (const rationalg<T>& rhs) const { | ||
| 481 | return (int(compare(rhs))<0); | ||
| 482 | } | ||
| 483 | |||
| 484 | /** | ||
| 485 | * \brief Compares this rationalg with another one. | ||
| 486 | * \details Internally computes the sign of the difference | ||
| 487 | * between this rationalg and \p rhs. | ||
| 488 | * \return true if this rationalg is smaller or equal than \p rhs, | ||
| 489 | * false otherwise | ||
| 490 | */ | ||
| 491 | bool operator<= (const rationalg<T>& rhs) const { | ||
| 492 | return (int(compare(rhs))<=0); | ||
| 493 | } | ||
| 494 | |||
| 495 | /** | ||
| 496 | * \brief Compares this rationalg with another one. | ||
| 497 | * \details Internally computes the sign of the difference | ||
| 498 | * between this rationalg and \p rhs. | ||
| 499 | * \return true if this rationalg is greater than \p rhs, | ||
| 500 | * false otherwise | ||
| 501 | */ | ||
| 502 | bool operator> (double rhs) const { | ||
| 503 | return (int(compare(rhs))>0); | ||
| 504 | } | ||
| 505 | |||
| 506 | /** | ||
| 507 | * \brief Compares this rationalg with another one. | ||
| 508 | * \details Internally computes the sign of the difference | ||
| 509 | * between this rationalg and \p rhs. | ||
| 510 | * \return true if this rationalg is greater or equal than \p rhs, | ||
| 511 | * false otherwise | ||
| 512 | */ | ||
| 513 | bool operator>= (double rhs) const { | ||
| 514 | return (int(compare(rhs))>=0); | ||
| 515 | } | ||
| 516 | |||
| 517 | /** | ||
| 518 | * \brief Compares this rationalg with another one. | ||
| 519 | * \details Internally computes the sign of the difference | ||
| 520 | * between this rationalg and \p rhs. | ||
| 521 | * \return true if this rationalg is smaller than \p rhs, | ||
| 522 | * false otherwise | ||
| 523 | */ | ||
| 524 | bool operator< (double rhs) const { | ||
| 525 | return (int(compare(rhs))<0); | ||
| 526 | } | ||
| 527 | |||
| 528 | /** | ||
| 529 | * \brief Compares this rationalg with another one. | ||
| 530 | * \details Internally computes the sign of the difference | ||
| 531 | * between this rationalg and \p rhs. | ||
| 532 | * \return true if this rationalg is smaller or equal than \p rhs, | ||
| 533 | * false otherwise | ||
| 534 | */ | ||
| 535 | bool operator<= (double rhs) const { | ||
| 536 | return (int(compare(rhs))<=0); | ||
| 537 | } | ||
| 538 | |||
| 539 | /********************************************************************/ | ||
| 540 | |||
| 541 | /** | ||
| 542 | * \brief Computes an approximation of the stored | ||
| 543 | * value in this rational. | ||
| 544 | * \return an approximation of the stored value. | ||
| 545 | */ | ||
| 546 | ✗ | double estimate() const { | |
| 547 | ✗ | return num_.estimate() / denom_.estimate(); | |
| 548 | } | ||
| 549 | |||
| 550 | protected: | ||
| 551 | /** | ||
| 552 | * \brief Copies a rational into this one. | ||
| 553 | * \param[in] rhs a const reference to the rational to be copied | ||
| 554 | */ | ||
| 555 | void copy(const rationalg<T>& rhs) { | ||
| 556 | num_ = rhs.num_; | ||
| 557 | denom_ = rhs.denom_; | ||
| 558 | } | ||
| 559 | |||
| 560 | /** | ||
| 561 | * \brief Tests whether a rationalg has trivially the | ||
| 562 | * same denominator this rationalg. | ||
| 563 | * \details This function is used to implement faster addition, | ||
| 564 | * subtraction and tests when it can be quickly determined that both | ||
| 565 | * operands have the same denominator. | ||
| 566 | * \retval true if it is trivial that \p rhs has the same denominator | ||
| 567 | * as this rationalg. | ||
| 568 | * \retval false otherwise. | ||
| 569 | */ | ||
| 570 | ✗ | bool has_same_denom(const rationalg<T>& rhs) const { | |
| 571 | ✗ | return denom_ == rhs.denom_; | |
| 572 | } | ||
| 573 | |||
| 574 | private: | ||
| 575 | T num_; | ||
| 576 | T denom_; | ||
| 577 | }; | ||
| 578 | |||
| 579 | /**************************************************************************/ | ||
| 580 | |||
| 581 | /** | ||
| 582 | * \brief Computes the sum of a double and a rationalg | ||
| 583 | * \param[in] a the double to be added | ||
| 584 | * \param[in] b the rationalg to be added | ||
| 585 | * \return a rationalg that represents \p a + \p b | ||
| 586 | * \relates rationalg | ||
| 587 | */ | ||
| 588 | template <class T> | ||
| 589 | inline rationalg<T> operator+ (double a, const rationalg<T>& b) { | ||
| 590 | return b + a; | ||
| 591 | } | ||
| 592 | |||
| 593 | /** | ||
| 594 | * \brief Computes the difference between a double and a rationalg | ||
| 595 | * \param[in] a the double | ||
| 596 | * \param[in] b the rationalg to be subtracted | ||
| 597 | * \return a rationalg that represents \p a - \p b | ||
| 598 | * \relates rationalg | ||
| 599 | */ | ||
| 600 | template <class T> | ||
| 601 | inline rationalg<T> operator- (double a, const rationalg<T>& b) { | ||
| 602 | rationalg<T> result = b - a; | ||
| 603 | result.num().negate(); | ||
| 604 | return result; | ||
| 605 | } | ||
| 606 | |||
| 607 | /** | ||
| 608 | * \brief Computes the product of a double and a rationalg | ||
| 609 | * \param[in] a the double | ||
| 610 | * \param[in] b the rationalg to be multiplied | ||
| 611 | * \return a rationalg that represents \p a * \p b | ||
| 612 | * \relates rationalg | ||
| 613 | */ | ||
| 614 | template <class T> | ||
| 615 | inline rationalg<T> operator* (double a, const rationalg<T>& b) { | ||
| 616 | return b * a; | ||
| 617 | } | ||
| 618 | |||
| 619 | /** | ||
| 620 | * \brief Computes the ratio between a double and a rationalg | ||
| 621 | * \param[in] a the double | ||
| 622 | * \param[in] b the rationalg to be divided | ||
| 623 | * \return a rationalg that represents \p a / \p b | ||
| 624 | * \relates rationalg | ||
| 625 | */ | ||
| 626 | template <class T> | ||
| 627 | inline rationalg<T> operator/ (double a, const rationalg<T>& b) { | ||
| 628 | return rationalg<T>( | ||
| 629 | T(a)*b.denom(), | ||
| 630 | b.num() | ||
| 631 | ); | ||
| 632 | } | ||
| 633 | |||
| 634 | /** | ||
| 635 | * \brief Tests equality between two rationalg%s. | ||
| 636 | * \details Implemented by testing whether the difference between | ||
| 637 | * \p a and \p b is 0. | ||
| 638 | * \return true if \p a and \p b represent exactly the same value, false | ||
| 639 | * otherwise | ||
| 640 | * \relates rationalg | ||
| 641 | */ | ||
| 642 | template <class T> | ||
| 643 | inline bool operator== (const rationalg<T>& a, const rationalg<T>& b) { | ||
| 644 | return (a.compare(b) == ZERO); | ||
| 645 | } | ||
| 646 | |||
| 647 | /** | ||
| 648 | * \brief Tests equality between a rationalg and a double. | ||
| 649 | * \details Implemented by testing whether the difference between | ||
| 650 | * \p a and \p b is 0. | ||
| 651 | * \return true if \p a and \p b represent exactly the same value, false | ||
| 652 | * otherwise | ||
| 653 | * \relates rationalg | ||
| 654 | */ | ||
| 655 | template <class T> | ||
| 656 | inline bool operator== (const rationalg<T>& a, double b) { | ||
| 657 | return (a.compare(b) == ZERO); | ||
| 658 | } | ||
| 659 | |||
| 660 | /** | ||
| 661 | * \brief Tests equality between a double and a rationalg. | ||
| 662 | * \details Implemented by testing whether the difference between | ||
| 663 | * \p a and \p b is 0. | ||
| 664 | * \return true if \p a and \p b represent exactly the same value, false | ||
| 665 | * otherwise | ||
| 666 | * \relates rationalg | ||
| 667 | */ | ||
| 668 | template <class T> | ||
| 669 | inline bool operator== (double a, const rationalg<T>& b) { | ||
| 670 | return (b.compare(a) == ZERO); | ||
| 671 | } | ||
| 672 | |||
| 673 | /** | ||
| 674 | * \brief Tests whether two rationalg%s differ. | ||
| 675 | * \details Implemented by testing whether the difference between | ||
| 676 | * \p a and \p b is different from 0. | ||
| 677 | * \return true if \p a and \p b do not represent the same exact value, | ||
| 678 | * false otherwise | ||
| 679 | * \relates rationalg | ||
| 680 | */ | ||
| 681 | template <class T> | ||
| 682 | inline bool operator!= (const rationalg<T>& a, const rationalg<T>& b) { | ||
| 683 | return (a.compare(b) != ZERO); | ||
| 684 | } | ||
| 685 | |||
| 686 | /** | ||
| 687 | * \brief Tests whether a rationalg differs from a double. | ||
| 688 | * \details Implemented by testing whether the difference between | ||
| 689 | * \p a and \p b is different from 0. | ||
| 690 | * \return true if \p a and \p b do not represent the same exact value, | ||
| 691 | * false otherwise | ||
| 692 | * \relates rationalg | ||
| 693 | */ | ||
| 694 | template <class T> | ||
| 695 | inline bool operator!= (const rationalg<T>& a, double b) { | ||
| 696 | return (a.compare(b) != ZERO); | ||
| 697 | } | ||
| 698 | |||
| 699 | /** | ||
| 700 | * \brief Tests whether a double differs from a rationalg. | ||
| 701 | * \details Implemented by testing whether the difference between | ||
| 702 | * \p a and \p b is different from 0. | ||
| 703 | * \return true if \p a and \p b do not represent the same exact value, | ||
| 704 | * false otherwise | ||
| 705 | * \relates rationalg | ||
| 706 | */ | ||
| 707 | template <class T> | ||
| 708 | inline bool operator!= (double a, const rationalg<T>& b) { | ||
| 709 | return (b.compare(a) != ZERO); | ||
| 710 | } | ||
| 711 | |||
| 712 | /**************************************************************************/ | ||
| 713 | |||
| 714 | /** | ||
| 715 | * \brief Specialization of geo_sgn() for rationalg | ||
| 716 | * \param x a const reference to a rationalg | ||
| 717 | * \return the (exact) sign of x (one of POSITIVE, ZERO, NEGATIVE) | ||
| 718 | */ | ||
| 719 | ✗ | template <class T> inline Sign geo_sgn(const rationalg<T>& x) { | |
| 720 | ✗ | return x.sign(); | |
| 721 | } | ||
| 722 | |||
| 723 | /** | ||
| 724 | * \brief Specialization of geo_cmp() for rationalg | ||
| 725 | * \param a , b const references to two rationalg | ||
| 726 | * \retval POSITIVE if a > b | ||
| 727 | * \retval ZERO if a == b | ||
| 728 | * \retval NEGATIVE if a < b | ||
| 729 | */ | ||
| 730 | ✗ | template <class T> inline Sign geo_cmp( | |
| 731 | const rationalg<T>& a, const rationalg<T>& b | ||
| 732 | ) { | ||
| 733 | ✗ | return a.compare(b); | |
| 734 | } | ||
| 735 | |||
| 736 | namespace Numeric { | ||
| 737 | |||
| 738 | template <class T> inline void optimize_number_representation( | ||
| 739 | rationalg<T>& x | ||
| 740 | ) { | ||
| 741 | x.optimize(); | ||
| 742 | } | ||
| 743 | |||
| 744 | } | ||
| 745 | |||
| 746 | /**************************************************************************/ | ||
| 747 | |||
| 748 | /** \brief Specialization of GEO::is_scalar */ | ||
| 749 | template <class T> struct is_scalar<rationalg<T> > { | ||
| 750 | typedef rationalg<T> type; | ||
| 751 | static constexpr bool value = true; | ||
| 752 | }; | ||
| 753 | |||
| 754 | } | ||
| 755 | |||
| 756 | #endif | ||
| 757 |