40#ifndef GEOGRAM_BASIC_RATIONALG
41#define GEOGRAM_BASIC_RATIONALG
60 template <
class T>
class rationalg {
64 rationalg() =
default;
70 explicit rationalg(
double x) : num_(x), denom_(1.0) {
77 explicit rationalg(
const T& x) : num_(x), denom_(1.0) {
134 rationalg<T>&
operator= (
const rationalg<T>& rhs) =
default;
195 num_ = num_ * rhs.denom_ + rhs.num_ * denom_;
196 denom_ *= rhs.denom_;
210 num_ = num_ * rhs.denom_ - rhs.num_ * denom_;
211 denom_ *= rhs.denom_;
223 denom_ *= rhs.denom_;
244 num_ += denom_ * T(rhs);
254 num_ -= denom_ * T(rhs);
291 rationalg<T>
operator+ (
const rationalg<T>& rhs)
const {
299 num_ * rhs.denom_ + rhs.num_ * denom_,
310 rationalg<T>
operator- (
const rationalg<T>& rhs)
const {
318 num_ * rhs.denom_ - rhs.num_ * denom_,
329 rationalg<T>
operator* (
const rationalg<T>& rhs)
const {
342 rationalg<T>
operator/ (
const rationalg<T>& rhs)
const {
357 num_ + T(rhs) * denom_,
369 num_ - T(rhs) * denom_,
419 return Sign(num_.sign() * denom_.sign());
433 return Sign(num_.compare(rhs.num_) * denom_.sign());
436 (num_ * rhs.denom_).compare(rhs.num_ * denom_) *
437 denom_.sign() * rhs.denom_.sign()
447 num_.compare(T(rhs)*denom_) * denom_.sign()
547 return num_.estimate() / denom_.estimate();
555 void copy(
const rationalg<T>& rhs) {
571 return denom_ == rhs.denom_;
589 inline rationalg<T>
operator+ (
double a,
const rationalg<T>& b) {
601 inline rationalg<T>
operator- (
double a,
const rationalg<T>& b) {
602 rationalg<T> result = b - a;
603 result.
num().negate();
615 inline rationalg<T> operator* (
double a,
const rationalg<T>& b) {
627 inline rationalg<T>
operator/ (
double a,
const rationalg<T>& b) {
643 inline bool operator== (
const rationalg<T>& a,
const rationalg<T>& b) {
682 inline bool operator!= (
const rationalg<T>& a,
const rationalg<T>& b) {
751 static constexpr bool value =
true;
#define geo_debug_assert(x)
Verifies that a condition is met.
rationalg (generic rational) is used to compute the sign of rational fractions exactly.
rationalg< T > operator-() const
Computes the opposite of this rationalg.
rationalg(const T &x)
Constructs a new rationalg from an T.
Sign sign() const
Gets the sign of a rationalg.
bool operator==(const rationalg< T > &a, const rationalg< T > &b)
Tests equality between two rationalgs.
rationalg(const T &num, const T &denom)
Constructs a new rationalg from two T.
bool operator<=(const rationalg< T > &rhs) const
Compares this rationalg with another one.
bool operator>=(const rationalg< T > &rhs) const
Compares this rationalg with another one.
rationalg< T > operator*(const rationalg< T > &rhs) const
Computes the product between two rationalgs.
rationalg< T > operator/(double a, const rationalg< T > &b)
Computes the ratio between a double and a rationalg.
T & num()
gets the numerator.
rationalg< T > & operator/=(const rationalg< T > &rhs)
Divides this rationalg by a rationalg.
rationalg< T > & operator+=(const rationalg< T > &rhs)
Adds a rationalg to this rationalg.
rationalg< T > & operator*=(const rationalg< T > &rhs)
Multiplies this rationalg by a rationalg.
rationalg< T > operator/(const rationalg< T > &rhs) const
Computes the ratio between two rationalgs.
rationalg(T &&x)
Constructs a new rationalg from an T with move semantics.
const scalar & num() const
Sign compare(double rhs) const
Compares a rationalg with a double.
T & denom()
gets the denominator.
void copy(const rationalg< T > &rhs)
Copies a rational into this one.
rationalg< T > operator+(const rationalg< T > &rhs) const
Computes the sum of two rationalgs.
rationalg(double x)
Constructs a new rationalg from a double.
rationalg< T > & operator=(const rationalg< T > &rhs)=default
Assignment operator.
rationalg(T &&num, T &&denom)
Constructs a new rationalg from two T with move semantics.
bool has_same_denom(const rationalg< T > &rhs) const
Tests whether a rationalg has trivially the same denominator this rationalg.
rationalg< T > & operator-=(const rationalg< T > &rhs)
Subtracts a rationalg to this rationalg.
Sign compare(const rationalg< T > &rhs) const
Compares two rationalg.
double estimate() const
Computes an approximation of the stored value in this rational.
rationalg(rationalg< T > &&rhs)=default
Move-constructor.
rationalg(double num, double denom)
Constructs a new rationalg from two doubles.
bool operator!=(const rationalg< T > &a, const rationalg< T > &b)
Tests whether two rationalgs differ.
bool operator>(const rationalg< T > &rhs) const
Compares this rationalg with another one.
const scalar & denom() const
rationalg(const rationalg< T > &rhs)=default
Copy-constructor.
void optimize()
Optimizes the internal representation without changing the represented value.
bool operator<(const rationalg< T > &rhs) const
Compares this rationalg with another one.
Common include file, providing basic definitions. Should be included before anything else by all head...
void optimize_number_representation(T &x)
place holder for optimizing internal number representation
Global Vorpaline namespace.
Quaternion operator-(const Quaternion &a, const Quaternion &b)
Computes the difference between two Quaternion.
Sign geo_sgn(const T &x)
Gets the sign of a value.
Sign
Integer constants that represent the sign of a value.
Sign geo_cmp(const T &a, const T &b)
Compares two values.
Quaternion operator+(const Quaternion &a, const Quaternion &b)
Computes the sum of two Quaternion.
Types and functions for numbers manipulation.