vnl_rational.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_rational.h
2 #ifndef vnl_rational_h_
3 #define vnl_rational_h_
4 //:
5 // \file
6 // \brief High-precision rational numbers
7 //
8 // The vnl_rational class provides high-precision rational numbers and
9 // arithmetic, using the built-in type long, for the numerator and denominator.
10 // Implicit conversion to the system defined types short, int, long, float, and
11 // double is supported by overloaded operator member functions. Although the
12 // rational class makes judicious use of inline functions and deals only with
13 // integral values, the user is warned that the rational integer arithmetic
14 // class is still considerably slower than the built-in integer data types. If
15 // the range of values anticipated will fit into a built-in type, use that
16 // instead.
17 //
18 // In addition to the original COOL Rational class, vnl_rational is able to
19 // represent plus and minus infinity. An other interesting addition is the
20 // possibility to construct a rational from a double. This allows for lossless
21 // conversion from e.g. double 1.0/3.0 to the rational number 1/3, hence no more
22 // rounding errors. This is implemented with continued fraction approximations.
23 //
24 // \author
25 // Copyright (C) 1991 Texas Instruments Incorporated.
26 //
27 // Permission is granted to any individual or institution to use, copy, modify,
28 // and distribute this software, provided that this complete copyright and
29 // permission notice is maintained, intact, in all copies and supporting
30 // documentation.
31 //
32 // Texas Instruments Incorporated provides this software "as is" without
33 // express or implied warranty.
34 //
35 // \verbatim
36 // Modifications
37 // Peter Vanroose, 13 July 2001: Added continued fraction cnstrctr from double
38 // Peter Vanroose, 10 July 2001: corrected operator%=()
39 // Peter Vanroose, 10 July 2001: corrected ceil() and floor() for negative args
40 // Peter Vanroose, 10 July 2001: extended operability range of += by using gcd
41 // Peter Vanroose, 10 July 2001: added abs().
42 // Peter Vanroose, 10 July 2001: removed state data member and added Inf repres
43 // Peter Vanroose, 9 July 2001: ported to vnl from COOL
44 // Peter Vanroose, 11 June 2009: made "*" and "/" robust against int overflow
45 // (actually a full re-implementation, using gcd)
46 // \endverbatim
47 
48 #include <iostream>
49 #ifdef _MSC_VER
50 # include <vcl_msvc_warnings.h>
51 #endif
52 #include <cassert>
53 #include "vnl/vnl_export.h"
54 
55 //: High-precision rational numbers
56 //
57 // The vnl_rational class provides high-precision rational numbers and
58 // arithmetic, using the built-in type long, for the numerator and denominator.
59 // Implicit conversion to the system defined types short, int, long, float, and
60 // double is supported by overloaded operator member functions. Although the
61 // rational class makes judicious use of inline functions and deals only with
62 // integral values, the user is warned that the rational integer arithmetic
63 // class is still considerably slower than the built-in integer data types. If
64 // the range of values anticipated will fit into a built-in type, use that
65 // instead.
66 //
67 // In addition to the original COOL Rational class, vnl_rational is able to
68 // represent plus and minus infinity. An other interesting addition is the
69 // possibility to construct a rational from a double. This allows for lossless
70 // conversion from e.g. double 1.0/3.0 to the rational number 1/3, hence no more
71 // rounding errors. This is implemented with continued fraction approximations.
72 //
73 class VNL_EXPORT vnl_rational
74 {
75  private:
76  long num_; //!< Numerator portion
77  long den_; //!< Denominator portion
78 
79  public:
80  //: Creates a rational with given numerator and denominator.
81  // Default constructor gives 0.
82  // Also serves as automatic cast from long to vnl_rational.
83  // The only input which is not allowed is (0,0);
84  // the denominator is allowed to be 0, to represent +Inf or -Inf.
85 
86  inline vnl_rational()
87  : num_(0L), den_(1L) { normalize(); }
88 
89  inline vnl_rational(long num)
90  : num_(num), den_(1L) { assert(num!=0||den_!=0); normalize(); }
91  inline vnl_rational(long num, long den)
92  : num_(num), den_(den) { assert(num!=0||den!=0); normalize(); }
93 
94  inline vnl_rational(unsigned long num)
95  : num_(num), den_(1L) { assert(num!=0||den_!=0); normalize(); }
96  inline vnl_rational(unsigned long num, unsigned long den)
97  : num_(num), den_(den) { assert(num!=0||den!=0); normalize(); }
98 
99  inline vnl_rational(int num)
100  : num_(num), den_(1L) { assert(num!=0||den_!=0); normalize(); }
101  inline vnl_rational(int num, int den)
102  : num_(num), den_(den) { assert(num!=0||den!=0); normalize(); }
103 
104  inline vnl_rational(unsigned int num)
105  : num_((long)num), den_(1L) { assert(num!=0||den_!=0); normalize(); }
106  inline vnl_rational(unsigned int num, unsigned int den)
107  : num_((long)num), den_((long)den) { assert(num!=0||den!=0); normalize(); }
108 
109  inline vnl_rational(short num)
110  : num_(num), den_(1L) { assert(num!=0||den_!=0); normalize(); }
111  inline vnl_rational(short num, short den)
112  : num_(num), den_(den) { assert(num!=0||den!=0); normalize(); }
113 
114  inline vnl_rational(unsigned short num)
115  : num_(num), den_(1L) { assert(num!=0||den_!=0); normalize(); }
116  inline vnl_rational(unsigned short num, unsigned short den)
117  : num_(num), den_(den) { assert(num!=0||den!=0); normalize(); }
118 
119  //: Creates a rational from a double.
120  // This is done by computing the continued fraction approximation for d.
121  // Note that this is explicitly \e not an automatic type conversion.
122  vnl_rational(double d);
123  vnl_rational(float d);
124  // Copy constructor
125  inline vnl_rational(vnl_rational const& from)
126  : num_(from.numerator()), den_(from.denominator()) {}
127  // Destructor
128  inline ~vnl_rational() = default;
129  // Assignment: overwrite an existing vnl_rational
130  inline void set(long num, long den) { assert(num!=0||den!=0); num_=num; den_=den; normalize(); }
131 
132  //: Return the numerator of the (simplified) rational number representation
133  inline long numerator() const { return num_; }
134  //: Return the denominator of the (simplified) rational number representation
135  inline long denominator() const { return den_; }
136 
137  //: Copies the contents and state of rhs rational over to the lhs
138  inline vnl_rational& operator=(vnl_rational const& rhs) {
139  num_ = rhs.numerator(); den_ = rhs.denominator(); return *this; }
140 
141  //: Returns true if the two rationals have the same representation
142  inline bool operator==(vnl_rational const& rhs) const {
143  return num_ == rhs.numerator() && den_ == rhs.denominator(); }
144  inline bool operator!=(vnl_rational const& rhs) const { return !operator==(rhs); }
145  inline bool operator==(long rhs) const { return num_ == rhs && den_ == 1; }
146  inline bool operator!=(long rhs) const { return !operator==(rhs); }
147  inline bool operator==(int rhs) const { return num_ == rhs && den_ == 1; }
148  inline bool operator!=(int rhs) const { return !operator==(rhs); }
149 
150  //: Unary minus - returns the negation of the current rational.
151  inline vnl_rational operator-() const { return vnl_rational(-num_, den_); }
152  //: Unary plus - returns the current rational.
153  inline vnl_rational operator+() const { return *this; }
154  //: Unary not - returns true if rational is equal to zero.
155  inline bool operator!() const { return num_ == 0L; }
156  //: Returns the absolute value of the current rational.
157  inline vnl_rational abs() const { return vnl_rational(num_<0?-num_:num_, den_); }
158  //: Replaces rational with 1/rational and returns it.
159  // Inverting 0 gives +Inf, inverting +-Inf gives 0.
161  long t = num_; num_ = den_; den_ = t; normalize(); return *this; }
162 
163  //: Plus/assign: replace lhs by lhs + rhs
164  // Note that +Inf + -Inf and -Inf + +Inf are undefined.
166  if (den_ == r.denominator()) num_ += r.numerator();
167  else { long c = vnl_rational::gcd(den_,r.denominator()); if (c==0) c=1;
168  num_ = num_*(r.denominator()/c) + (den_/c)*r.numerator();
169  den_ *= r.denominator()/c; }
170  assert(num_!=0 || den_ != 0); // +Inf + -Inf is undefined
171  normalize(); return *this;
172  }
173  inline vnl_rational& operator+=(long r) { num_ += den_*r; return *this; }
174  //: Minus/assign: replace lhs by lhs - rhs
175  // Note that +Inf - +Inf and -Inf - -Inf are undefined.
177  if (den_ == r.denominator()) num_ -= r.num_;
178  else { long c = vnl_rational::gcd(den_,r.denominator()); if (c==0) c=1;
179  num_ = num_*(r.denominator()/c) - (den_/c)*r.numerator();
180  den_ *= r.denominator()/c; }
181  assert(num_!=0 || den_ != 0); // +Inf - +Inf is undefined
182  normalize(); return *this;
183  }
184  inline vnl_rational& operator-=(long r) { num_ -= den_*r; return *this; }
185  //: Multiply/assign: replace lhs by lhs * rhs
186  // Note that 0 * Inf and Inf * 0 are undefined.
187  // Also note that there could be integer overflow during this calculation!
188  // In that case, an approximate result will be returned.
189  vnl_rational& operator*=(vnl_rational const& r);
190  //: Multiply/assign: replace lhs by lhs * rhs
191  // Note that there could be integer overflow during this calculation!
192  // In that case, an approximate result will be returned.
193  vnl_rational& operator*=(long r);
194  //: Divide/assign: replace lhs by lhs / rhs
195  // Note that 0 / 0 and Inf / Inf are undefined.
196  // Also note that there could be integer overflow during this calculation!
197  // In that case, an approximate result will be returned.
198  vnl_rational& operator/=(vnl_rational const& r);
199  //: Divide/assign: replace lhs by lhs / rhs
200  // Note that 0 / 0 is undefined.
201  // Also note that there could be integer overflow during this calculation!
202  // In that case, an approximate result will be returned.
203  vnl_rational& operator/=(long r);
204  //: Modulus/assign: replace lhs by lhs % rhs
205  // Note that r % Inf is r, and that r % 0 and Inf % r are undefined.
207  assert(r.numerator() != 0);
208  if (den_ == r.denominator()) num_ %= r.numerator();
209  else { long c = vnl_rational::gcd(den_,r.denominator()); if (c==0) c=1;
210  num_ *= r.denominator()/c;
211  num_ %= (den_/c)*r.numerator();
212  den_ *= r.denominator()/c; }
213  normalize(); return *this;
214  }
215  inline vnl_rational& operator%=(long r) {assert(r);num_%=den_*r;normalize();return *this;}
216 
217  //: Pre-increment (++r). No-op when +-Inf.
218  inline vnl_rational& operator++() { num_ += den_; return *this; }
219  //: Pre-decrement (--r). No-op when +-Inf.
220  inline vnl_rational& operator--() { num_ -= den_; return *this; }
221  //: Post-increment (r++). No-op when +-Inf.
222  inline vnl_rational operator++(int) {vnl_rational b=*this;num_+=den_;return b;}
223  //: Post-decrement (r--). No-op when +-Inf.
224  inline vnl_rational operator--(int) {vnl_rational b=*this;num_-=den_;return b;}
225 
226  inline bool operator<(vnl_rational const& rhs) const {
227  if (den_ == rhs.denominator()) // If same denominator
228  return num_ < rhs.numerator(); // includes the case -Inf < +Inf
229  // note that denominator is always >= 0:
230  else
231  return num_ * rhs.denominator() < den_ * rhs.numerator();
232  }
233  inline bool operator>(vnl_rational const& r) const { return r < *this; }
234  inline bool operator<=(vnl_rational const& r) const { return !operator>(r); }
235  inline bool operator>=(vnl_rational const& r) const { return !operator<(r); }
236  inline bool operator<(long r) const { return num_ < den_ * r; }
237  inline bool operator>(long r) const { return num_ > den_ * r; }
238  inline bool operator<=(long r) const { return !operator>(r); }
239  inline bool operator>=(long r) const { return !operator<(r); }
240  inline bool operator<(int r) const { return num_ < den_ * r; }
241  inline bool operator>(int r) const { return num_ > den_ * r; }
242  inline bool operator<=(int r) const { return !operator>(r); }
243  inline bool operator>=(int r) const { return !operator<(r); }
244  inline bool operator<(double r) const { return num_ < den_ * r; }
245  inline bool operator>(double r) const { return num_ > den_ * r; }
246  inline bool operator<=(double r) const { return !operator>(r); }
247  inline bool operator>=(double r) const { return !operator<(r); }
248 
249  //: Converts rational value to integer by truncating towards zero.
250  inline long truncate() const { assert(den_ != 0); return num_/den_; }
251  //: Converts rational value to integer by truncating towards negative infinity.
252  inline long floor() const { long t = truncate();
253  return num_<0L && (num_%den_) != 0 ? t-1 : t; }
254  //: Converts rational value to integer by truncating towards positive infinity.
255  inline long ceil() const { long t = truncate();
256  return num_>0L && (num_%den_) != 0 ? t+1 : t; }
257  //: Rounds rational to nearest integer.
258  inline long round() const { long t = truncate();
259  if (num_ < 0) return ((-num_)%den_) >= 0.5*den_ ? t-1 : t;
260  else return (num_ %den_) >= 0.5*den_ ? t+1 : t;
261  }
262 
263  // Implicit conversions
264  inline operator short() {
265  long t = truncate(); short r = (short)t;
266  assert(r == t); // abort on underflow or overflow
267  return r;
268  }
269  inline operator int() {
270  long t = truncate(); int r = (int)t;
271  assert(r == t); // abort on underflow or overflow
272  return r;
273  }
274  inline operator long() const { return truncate(); }
275  inline operator long() { 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_); }
280 
281  //: Calculate greatest common divisor of two integers.
282  // Used to simplify rational number.
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;
286  }
287 
288  private:
289  //: Private function to normalize numerator/denominator of rational number.
290  // If num_ and den_ are both nonzero, their gcd is made 1 and den_ made positive.
291  // Otherwise, the nonzero den_ is set to 1 or the nonzero num_ to +1 or -1.
292  inline void normalize() {
293  if (num_ == 0) { den_ = 1; return; } // zero
294  if (den_ == 0) { num_ = (num_>0) ? 1 : -1; return; } // +-Inf
295  if (num_ != 1 && num_ != -1 && den_ != 1) {
296  long common = vnl_rational::gcd(num_, den_);
297  if (common != 1) { num_ /= common; den_ /= common; }
298  }
299  // if negative, put sign in numerator:
300  if (den_ < 0) { num_ *= -1; den_ *= -1; }
301  }
302 };
303 
304 //: formatted output
305 // \relatesalso vnl_rational
306 inline std::ostream& operator<<(std::ostream& s, vnl_rational const& r)
307 {
308  return s << r.numerator() << '/' << r.denominator();
309 }
310 
311 //: simple input
312 // \relatesalso vnl_rational
313 inline std::istream& operator>>(std::istream& s, vnl_rational& r)
314 {
315  long n, d; s >> n >> d;
316  r.set(n,d); return s;
317 }
318 
319 //: Returns the sum of two rational numbers.
320 // \relatesalso vnl_rational
321 inline vnl_rational operator+(vnl_rational const& r1, vnl_rational const& r2)
322 {
323  vnl_rational result(r1); return result += r2;
324 }
325 
326 inline vnl_rational operator+(vnl_rational const& r1, long r2)
327 {
328  vnl_rational result(r1); return result += r2;
329 }
330 
331 inline vnl_rational operator+(vnl_rational const& r1, int r2)
332 {
333  vnl_rational result(r1); return result += (long)r2;
334 }
335 
336 inline vnl_rational operator+(long r2, vnl_rational const& r1)
337 {
338  vnl_rational result(r1); return result += r2;
339 }
340 
341 inline vnl_rational operator+(int r2, vnl_rational const& r1)
342 {
343  vnl_rational result(r1); return result += (long)r2;
344 }
345 
346 //: Returns the difference of two rational numbers.
347 // \relatesalso vnl_rational
348 inline vnl_rational operator-(vnl_rational const& r1, vnl_rational const& r2)
349 {
350  vnl_rational result(r1); return result -= r2;
351 }
352 
353 inline vnl_rational operator-(vnl_rational const& r1, long r2)
354 {
355  vnl_rational result(r1); return result -= r2;
356 }
357 
358 inline vnl_rational operator-(vnl_rational const& r1, int r2)
359 {
360  vnl_rational result(r1); return result -= (long)r2;
361 }
362 
363 inline vnl_rational operator-(long r2, vnl_rational const& r1)
364 {
365  vnl_rational result(-r1); return result += r2;
366 }
367 
368 inline vnl_rational operator-(int r2, vnl_rational const& r1)
369 {
370  vnl_rational result(-r1); return result += (long)r2;
371 }
372 
373 //: Returns the product of two rational numbers.
374 // \relatesalso vnl_rational
375 inline vnl_rational operator*(vnl_rational const& r1, vnl_rational const& r2)
376 {
377  vnl_rational result(r1); return result *= r2;
378 }
379 
380 inline vnl_rational operator*(vnl_rational const& r1, long r2)
381 {
382  vnl_rational result(r1); return result *= r2;
383 }
384 
385 inline vnl_rational operator*(vnl_rational const& r1, int r2)
386 {
387  vnl_rational result(r1); return result *= (long)r2;
388 }
389 
390 inline vnl_rational operator*(long r2, vnl_rational const& r1)
391 {
392  vnl_rational result(r1); return result *= r2;
393 }
394 
395 inline vnl_rational operator*(int r2, vnl_rational const& r1)
396 {
397  vnl_rational result(r1); return result *= (long)r2;
398 }
399 
400 //: Returns the quotient of two rational numbers.
401 // \relatesalso vnl_rational
402 inline vnl_rational operator/(vnl_rational const& r1, vnl_rational const& r2)
403 {
404  vnl_rational result(r1); return result /= r2;
405 }
406 
407 inline vnl_rational operator/(vnl_rational const& r1, long r2)
408 {
409  vnl_rational result(r1); return result /= r2;
410 }
411 
412 inline vnl_rational operator/(vnl_rational const& r1, int r2)
413 {
414  vnl_rational result(r1); return result /= (long)r2;
415 }
416 
417 inline vnl_rational operator/(long r1, vnl_rational const& r2)
418 {
419  vnl_rational result(r1); return result /= r2;
420 }
421 
422 inline vnl_rational operator/(int r1, vnl_rational const& r2)
423 {
424  vnl_rational result((long)r1); return result /= r2;
425 }
426 
427 //: Returns the remainder of r1 divided by r2.
428 // \relatesalso vnl_rational
429 inline vnl_rational operator%(vnl_rational const& r1, vnl_rational const& r2)
430 {
431  vnl_rational result(r1); return result %= r2;
432 }
433 
434 inline vnl_rational operator%(vnl_rational const& r1, long r2)
435 {
436  vnl_rational result(r1); return result %= r2;
437 }
438 
439 inline vnl_rational operator%(vnl_rational const& r1, int r2)
440 {
441  vnl_rational result(r1); return result %= (long)r2;
442 }
443 
444 inline vnl_rational operator%(long r1, vnl_rational const& r2)
445 {
446  vnl_rational result(r1); return result %= r2;
447 }
448 
449 inline vnl_rational operator%(int r1, vnl_rational const& r2)
450 {
451  vnl_rational result((long)r1); return result %= r2;
452 }
453 
454 inline bool operator==(int r1, vnl_rational const& r2) { return r2==r1; }
455 inline bool operator==(long r1, vnl_rational const& r2) { return r2==r1; }
456 inline bool operator!=(int r1, vnl_rational const& r2) { return r2!=r1; }
457 inline bool operator!=(long r1, vnl_rational const& r2) { return r2!=r1; }
458 inline bool operator< (int r1, vnl_rational const& r2) { return r2> r1; }
459 inline bool operator< (long r1, vnl_rational const& r2) { return r2> r1; }
460 inline bool operator> (int r1, vnl_rational const& r2) { return r2< r1; }
461 inline bool operator> (long r1, vnl_rational const& r2) { return r2< r1; }
462 inline bool operator<=(int r1, vnl_rational const& r2) { return r2>=r1; }
463 inline bool operator<=(long r1, vnl_rational const& r2) { return r2>=r1; }
464 inline bool operator>=(int r1, vnl_rational const& r2) { return r2<=r1; }
465 inline bool operator>=(long r1, vnl_rational const& r2) { return r2<=r1; }
466 
467 inline long truncate(vnl_rational const& r) { return r.truncate(); }
468 inline long floor(vnl_rational const& r) { return r.floor(); }
469 inline long ceil(vnl_rational const& r) { return r.ceil(); }
470 inline long round(vnl_rational const& r) { return r.round(); }
471 
472 namespace vnl_math
473 {
474  inline vnl_rational abs(vnl_rational const& x) { return x<0L ? -x : x; }
475  inline vnl_rational squared_magnitude(vnl_rational const& x) { return x*x; }
476  inline vnl_rational sqr(vnl_rational const& x) { return x*x; }
477  inline bool isnan(vnl_rational const& ) {return false;}
478  inline bool isfinite(vnl_rational const& x) {return x.denominator() != 0L;}
479 }
480 
481 #endif // vnl_rational_h_
bool operator>=(int r1, vnl_rational const &r2)
Definition: vnl_rational.h:464
bool operator==(long rhs) const
Definition: vnl_rational.h:145
bool operator<=(long r) const
Definition: vnl_rational.h:238
void set(long num, long den)
Definition: vnl_rational.h:130
vnl_rational operator+() const
Unary plus - returns the current rational.
Definition: vnl_rational.h:153
long num_
Numerator portion.
Definition: vnl_rational.h:76
vnl_bignum operator+(vnl_bignum const &r1, long r2)
Returns the sum of two bignum numbers.
Definition: vnl_bignum.h:279
long ceil(vnl_rational const &r)
Definition: vnl_rational.h:469
vnl_rational(short num, short den)
Definition: vnl_rational.h:111
bool operator<=(vnl_rational const &r) const
Definition: vnl_rational.h:234
vnl_rational operator-() const
Unary minus - returns the negation of the current rational.
Definition: vnl_rational.h:151
bool operator!() const
Unary not - returns true if rational is equal to zero.
Definition: vnl_rational.h:155
vnl_rational(int num)
Definition: vnl_rational.h:99
vnl_rational(int num, int den)
Definition: vnl_rational.h:101
static long gcd(long l1, long l2)
Calculate greatest common divisor of two integers.
Definition: vnl_rational.h:283
vnl_rational operator--(int)
Post-decrement (r–). No-op when +-Inf.
Definition: vnl_rational.h:224
bool operator!=(int rhs) const
Definition: vnl_rational.h:148
bool operator>(double r) const
Definition: vnl_rational.h:245
vnl_rational & operator%=(long r)
Definition: vnl_rational.h:215
bool operator<(vnl_rational const &rhs) const
Definition: vnl_rational.h:226
vnl_rational(unsigned long num)
Definition: vnl_rational.h:94
bool operator<=(int r1, vnl_rational const &r2)
Definition: vnl_rational.h:462
long round(vnl_rational const &r)
Definition: vnl_rational.h:470
bool operator>=(double r) const
Definition: vnl_rational.h:247
bool operator>(int r1, vnl_rational const &r2)
Definition: vnl_rational.h:460
vnl_rational operator++(int)
Post-increment (r++). No-op when +-Inf.
Definition: vnl_rational.h:222
long floor(vnl_rational const &r)
Definition: vnl_rational.h:468
vnl_rational & operator-=(vnl_rational const &r)
Minus/assign: replace lhs by lhs - rhs.
Definition: vnl_rational.h:176
vnl_rational(unsigned long num, unsigned long den)
Definition: vnl_rational.h:96
bool operator>(int r) const
Definition: vnl_rational.h:241
vnl_rational & operator--()
Pre-decrement (–r). No-op when +-Inf.
Definition: vnl_rational.h:220
long round() const
Rounds rational to nearest integer.
Definition: vnl_rational.h:258
bool operator<=(double r) const
Definition: vnl_rational.h:246
vnl_rational & operator%=(vnl_rational const &r)
Modulus/assign: replace lhs by lhs % rhs.
Definition: vnl_rational.h:206
real numerical constants.
Definition: vnl_bignum.h:430
long ceil() const
Converts rational value to integer by truncating towards positive infinity.
Definition: vnl_rational.h:255
vnl_bignum squared_magnitude(vnl_bignum const &x)
Definition: vnl_bignum.h:433
long truncate() const
Converts rational value to integer by truncating towards zero.
Definition: vnl_rational.h:250
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
Definition: vnl_decnum.h:393
vnl_rational(long num)
Definition: vnl_rational.h:89
vnl_rational(vnl_rational const &from)
Definition: vnl_rational.h:125
bool isnan(vnl_bignum const &)
Definition: vnl_bignum.h:435
bool operator!=(vnl_rational const &rhs) const
Definition: vnl_rational.h:144
bool operator==(int rhs) const
Definition: vnl_rational.h:147
bool operator<(int r) const
Definition: vnl_rational.h:240
bool operator<(double r) const
Definition: vnl_rational.h:244
bool operator==(int r1, vnl_finite_int< N > const &r2)
Definition: vnl_finite.h:385
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.
Definition: vnl_rational.h:142
bool operator!=(long rhs) const
Definition: vnl_rational.h:146
long truncate(vnl_rational const &r)
Definition: vnl_rational.h:467
bool isfinite(vnl_bignum const &x)
Definition: vnl_bignum.h:436
vnl_rational(unsigned int num, unsigned int den)
Definition: vnl_rational.h:106
vnl_rational abs() const
Returns the absolute value of the current rational.
Definition: vnl_rational.h:157
long numerator() const
Return the numerator of the (simplified) rational number representation.
Definition: vnl_rational.h:133
long floor() const
Converts rational value to integer by truncating towards negative infinity.
Definition: vnl_rational.h:252
vnl_rational & operator+=(vnl_rational const &r)
Plus/assign: replace lhs by lhs + rhs.
Definition: vnl_rational.h:165
vnl_bignum operator%(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the remainder of r1 divided by r2.
Definition: vnl_bignum.h:396
long den_
Denominator portion.
Definition: vnl_rational.h:77
bool operator<(long r) const
Definition: vnl_rational.h:236
bool operator>=(int r) const
Definition: vnl_rational.h:243
vnl_bignum abs(vnl_bignum const &x)
Definition: vnl_bignum.h:432
vnl_rational & operator+=(long r)
Definition: vnl_rational.h:173
vnl_rational & operator-=(long r)
Definition: vnl_rational.h:184
vnl_rational(short num)
Definition: vnl_rational.h:109
VNL_EXPORT std::istream & operator>>(std::istream &s, vnl_decnum &r)
decimal input.
Definition: vnl_decnum.cxx:411
vnl_bignum operator-(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the difference of two bignum numbers.
Definition: vnl_bignum.h:290
bool operator!=(int r1, vnl_finite_int< N > const &r2)
Definition: vnl_finite.h:390
long denominator() const
Return the denominator of the (simplified) rational number representation.
Definition: vnl_rational.h:135
vnl_rational(long num, long den)
Definition: vnl_rational.h:91
vnl_rational & invert()
Replaces rational with 1/rational and returns it.
Definition: vnl_rational.h:160
vnl_rational(unsigned short num)
Definition: vnl_rational.h:114
vnl_rational & operator++()
Pre-increment (++r). No-op when +-Inf.
Definition: vnl_rational.h:218
vnl_bignum operator/(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the division of two bignum numbers.
Definition: vnl_bignum.h:349
bool operator>(vnl_rational const &r) const
Definition: vnl_rational.h:233
vnl_rational(unsigned short num, unsigned short den)
Definition: vnl_rational.h:116
vnl_rational(unsigned int num)
Definition: vnl_rational.h:104
bool operator>=(vnl_rational const &r) const
Definition: vnl_rational.h:235
vnl_bignum operator *(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the product of two bignum numbers.
Definition: vnl_bignum.h:302
bool operator>=(long r) const
Definition: vnl_rational.h:239
void normalize()
Private function to normalize numerator/denominator of rational number.
Definition: vnl_rational.h:292
vnl_bignum sqr(vnl_bignum const &x)
Definition: vnl_bignum.h:434
bool operator<=(int r) const
Definition: vnl_rational.h:242
bool operator==(int r1, vnl_rational const &r2)
Definition: vnl_rational.h:454
vnl_rational()
Creates a rational with given numerator and denominator.
Definition: vnl_rational.h:86
VNL_EXPORT bool operator<(vnl_vector< T > const &lhs, vnl_vector< T > const &rhs)
Define a complete ordering on vnl_vector.
Definition: vnl_operators.h:24
vnl_rational & operator=(vnl_rational const &rhs)
Copies the contents and state of rhs rational over to the lhs.
Definition: vnl_rational.h:138
bool operator>(long r) const
Definition: vnl_rational.h:237
High-precision rational numbers.
Definition: vnl_rational.h:73