1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2020 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
* \file
* \brief Rational number classes.
*/
#ifndef RATIONAL_H
#define RATIONAL_H
#include <algorithm>
#include <cmath>
static inline int gcd(int a, int b)
{
while (b != 0)
{
a %= b;
std::swap(a, b);
}
return abs(a);
}
/// Simple rational numbers, not bignum.
struct Rational
{
Rational() {}
Rational(int numerator, int denominator = 1)
{
int g = gcd(numerator, denominator);
g *= denominator > 0 ? 1 : -1;
n = numerator / g;
d = denominator / g;
}
bool operator ==(Rational const &b) const
{
return n * b.d == b.n * d;
}
bool operator !=(Rational const &b) const
{
return n * b.d != b.n * d;
}
bool operator < (Rational const &b) const
{
return n * b.d < b.n * d;
}
bool operator <=(Rational const &b) const
{
return n * b.d <= b.n * d;
}
bool operator > (Rational const &b) const
{
return n * b.d > b.n * d;
}
bool operator >=(Rational const &b) const
{
return n * b.d >= b.n * d;
}
Rational operator +(Rational const &b) const
{
return Rational(n * b.d + b.n * d, d * b.d);
}
Rational operator -(Rational const &b) const
{
return Rational(n * b.d - b.n * d, d * b.d);
}
Rational operator *(Rational const &b) const
{
return Rational(n * b.n, d * b.d);
}
Rational operator /(Rational const &b) const
{
return Rational(n * b.d, d * b.n);
}
Rational operator -() const
{
return Rational(-n, d);
}
Rational &operator +=(Rational const &b)
{
return *this = *this + b;
}
Rational &operator -=(Rational const &b)
{
return *this = *this - b;
}
Rational &operator *=(Rational const &b)
{
return *this = *this * b;
}
Rational &operator /=(Rational const &b)
{
return *this = *this / b;
}
int floor() const
{
return n >= 0 ? n / d : (n - (d - 1)) / d;
}
int ceil() const
{
return n >= 0 ? (n + (d - 1)) / d : n / d;
}
// We can't do this with a operator as it ambiguates the comparison operators
double asDouble() const
{
if (0 == d)
{
return nan("");
}
return (double)n / (double)d;
}
// If int16_t isn't big enough, the comparison operations might overflow.
int16_t n;
int16_t d;
private:
Rational(float); // Disable.
Rational(double); // Disable.
};
#endif //RATIONAL_H
|