2 #ifndef vnl_rational_h_ 3 #define vnl_rational_h_ 50 # include <vcl_msvc_warnings.h> 53 #include "vnl/vnl_export.h" 90 : num_(num), den_(1L) { assert(num!=0||den_!=0);
normalize(); }
92 : num_(num), den_(den) { assert(num!=0||den!=0);
normalize(); }
95 : num_(num), den_(1L) { assert(num!=0||den_!=0);
normalize(); }
97 : num_(num), den_(den) { assert(num!=0||den!=0);
normalize(); }
100 : num_(num), den_(1L) { assert(num!=0||den_!=0);
normalize(); }
102 : num_(num), den_(den) { assert(num!=0||den!=0);
normalize(); }
105 : num_((long)num), den_(1L) { assert(num!=0||den_!=0);
normalize(); }
107 : num_((long)num), den_((long)den) { assert(num!=0||den!=0);
normalize(); }
110 : num_(num), den_(1L) { assert(num!=0||den_!=0);
normalize(); }
112 : num_(num), den_(den) { assert(num!=0||den!=0);
normalize(); }
115 : num_(num), den_(1L) { assert(num!=0||den_!=0);
normalize(); }
117 : num_(num), den_(den) { assert(num!=0||den!=0);
normalize(); }
126 : num_(from.numerator()), den_(from.denominator()) {}
130 inline void set(
long num,
long den) { assert(num!=0||den!=0); num_=num; den_=den;
normalize(); }
145 inline bool operator==(
long rhs)
const {
return num_ == rhs && den_ == 1; }
147 inline bool operator==(
int rhs)
const {
return num_ == rhs && den_ == 1; }
161 long t = num_; num_ = den_; den_ = t;
normalize();
return *
this; }
170 assert(num_!=0 || den_ != 0);
181 assert(num_!=0 || den_ != 0);
236 inline bool operator<(
long r)
const {
return num_ < den_ * r; }
237 inline bool operator>(
long r)
const {
return num_ > den_ * r; }
240 inline bool operator<(
int r)
const {
return num_ < den_ * r; }
241 inline bool operator>(
int r)
const {
return num_ > den_ * r; }
244 inline bool operator<(
double r)
const {
return num_ < den_ * r; }
245 inline bool operator>(
double r)
const {
return num_ > den_ * r; }
250 inline long truncate()
const { assert(den_ != 0);
return num_/den_; }
253 return num_<0L && (num_%den_) != 0 ? t-1 : t; }
256 return num_>0L && (num_%den_) != 0 ? t+1 : t; }
259 if (num_ < 0)
return ((-num_)%den_) >= 0.5*den_ ? t-1 : t;
260 else return (num_ %den_) >= 0.5*den_ ? t+1 : t;
264 inline operator short() {
265 long t =
truncate();
short r = (short)t;
269 inline operator int() {
270 long t =
truncate();
int r = (int)t;
274 inline operator long()
const {
return truncate(); }
276 inline operator float()
const {
return ((
float)num_)/((float)den_); }
277 inline operator float() {
return ((
float)num_)/((float)den_); }
278 inline operator double()
const {
return ((
double)num_)/((double)den_); }
279 inline operator double() {
return ((
double)num_)/((double)den_); }
283 static inline long gcd (
long l1,
long l2) {
284 while (l2!=0) {
long t = l2; l2 = l1 % l2; l1 = t; }
285 return l1<0 ? (-l1) : l1;
293 if (num_ == 0) { den_ = 1;
return; }
294 if (den_ == 0) { num_ = (num_>0) ? 1 : -1;
return; }
295 if (num_ != 1 && num_ != -1 && den_ != 1) {
297 if (common != 1) { num_ /= common; den_ /= common; }
300 if (den_ < 0) { num_ *= -1; den_ *= -1; }
315 long n, d; s >> n >> d;
316 r.
set(n,d);
return s;
481 #endif // vnl_rational_h_ bool operator>=(int r1, vnl_rational const &r2)
bool operator==(long rhs) const
bool operator<=(long r) const
void set(long num, long den)
vnl_rational operator+() const
Unary plus - returns the current rational.
long num_
Numerator portion.
vnl_bignum operator+(vnl_bignum const &r1, long r2)
Returns the sum of two bignum numbers.
long ceil(vnl_rational const &r)
vnl_rational(short num, short den)
bool operator<=(vnl_rational const &r) const
vnl_rational operator-() const
Unary minus - returns the negation of the current rational.
bool operator!() const
Unary not - returns true if rational is equal to zero.
vnl_rational(int num, int den)
static long gcd(long l1, long l2)
Calculate greatest common divisor of two integers.
vnl_rational operator--(int)
Post-decrement (r–). No-op when +-Inf.
bool operator!=(int rhs) const
bool operator>(double r) const
vnl_rational & operator%=(long r)
bool operator<(vnl_rational const &rhs) const
vnl_rational(unsigned long num)
bool operator<=(int r1, vnl_rational const &r2)
long round(vnl_rational const &r)
bool operator>=(double r) const
bool operator>(int r1, vnl_rational const &r2)
vnl_rational operator++(int)
Post-increment (r++). No-op when +-Inf.
long floor(vnl_rational const &r)
vnl_rational & operator-=(vnl_rational const &r)
Minus/assign: replace lhs by lhs - rhs.
vnl_rational(unsigned long num, unsigned long den)
bool operator>(int r) const
vnl_rational & operator--()
Pre-decrement (–r). No-op when +-Inf.
long round() const
Rounds rational to nearest integer.
bool operator<=(double r) const
vnl_rational & operator%=(vnl_rational const &r)
Modulus/assign: replace lhs by lhs % rhs.
real numerical constants.
long ceil() const
Converts rational value to integer by truncating towards positive infinity.
vnl_bignum squared_magnitude(vnl_bignum const &x)
long truncate() const
Converts rational value to integer by truncating towards zero.
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
vnl_rational(vnl_rational const &from)
bool isnan(vnl_bignum const &)
bool operator!=(vnl_rational const &rhs) const
bool operator==(int rhs) const
bool operator<(int r) const
bool operator<(double r) const
bool operator==(int r1, vnl_finite_int< N > const &r2)
Data normalize(const vnl_bignum &b1, const vnl_bignum &b2, vnl_bignum &u, vnl_bignum &v)
normalize two vnl_bignums.
bool operator==(vnl_rational const &rhs) const
Returns true if the two rationals have the same representation.
bool operator!=(long rhs) const
long truncate(vnl_rational const &r)
bool isfinite(vnl_bignum const &x)
vnl_rational(unsigned int num, unsigned int den)
vnl_rational abs() const
Returns the absolute value of the current rational.
long numerator() const
Return the numerator of the (simplified) rational number representation.
long floor() const
Converts rational value to integer by truncating towards negative infinity.
vnl_rational & operator+=(vnl_rational const &r)
Plus/assign: replace lhs by lhs + rhs.
vnl_bignum operator%(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the remainder of r1 divided by r2.
long den_
Denominator portion.
bool operator<(long r) const
bool operator>=(int r) const
vnl_bignum abs(vnl_bignum const &x)
vnl_rational & operator+=(long r)
vnl_rational & operator-=(long r)
VNL_EXPORT std::istream & operator>>(std::istream &s, vnl_decnum &r)
decimal input.
vnl_bignum operator-(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the difference of two bignum numbers.
bool operator!=(int r1, vnl_finite_int< N > const &r2)
long denominator() const
Return the denominator of the (simplified) rational number representation.
vnl_rational(long num, long den)
vnl_rational & invert()
Replaces rational with 1/rational and returns it.
vnl_rational(unsigned short num)
vnl_rational & operator++()
Pre-increment (++r). No-op when +-Inf.
vnl_bignum operator/(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the division of two bignum numbers.
bool operator>(vnl_rational const &r) const
vnl_rational(unsigned short num, unsigned short den)
vnl_rational(unsigned int num)
bool operator>=(vnl_rational const &r) const
vnl_bignum operator *(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the product of two bignum numbers.
bool operator>=(long r) const
void normalize()
Private function to normalize numerator/denominator of rational number.
vnl_bignum sqr(vnl_bignum const &x)
bool operator<=(int r) const
bool operator==(int r1, vnl_rational const &r2)
vnl_rational()
Creates a rational with given numerator and denominator.
VNL_EXPORT bool operator<(vnl_vector< T > const &lhs, vnl_vector< T > const &rhs)
Define a complete ordering on vnl_vector.
vnl_rational & operator=(vnl_rational const &rhs)
Copies the contents and state of rhs rational over to the lhs.
bool operator>(long r) const
High-precision rational numbers.