High-precision rational numbers. More...
#include <vnl_rational.h>
Public Member Functions | |
| vnl_rational () | |
| Creates a rational with given numerator and denominator. More... | |
| vnl_rational (long num) | |
| vnl_rational (long num, long den) | |
| vnl_rational (unsigned long num) | |
| vnl_rational (unsigned long num, unsigned long den) | |
| vnl_rational (int num) | |
| vnl_rational (int num, int den) | |
| vnl_rational (unsigned int num) | |
| vnl_rational (unsigned int num, unsigned int den) | |
| vnl_rational (short num) | |
| vnl_rational (short num, short den) | |
| vnl_rational (unsigned short num) | |
| vnl_rational (unsigned short num, unsigned short den) | |
| vnl_rational (double d) | |
| Creates a rational from a double. More... | |
| vnl_rational (float d) | |
| Creates a rational from a double. More... | |
| vnl_rational (vnl_rational const &from) | |
| ~vnl_rational ()=default | |
| void | set (long num, long den) |
| long | numerator () const |
| Return the numerator of the (simplified) rational number representation. More... | |
| long | denominator () const |
| Return the denominator of the (simplified) rational number representation. More... | |
| vnl_rational & | operator= (vnl_rational const &rhs) |
| Copies the contents and state of rhs rational over to the lhs. More... | |
| bool | operator== (vnl_rational const &rhs) const |
| Returns true if the two rationals have the same representation. More... | |
| bool | operator!= (vnl_rational const &rhs) const |
| bool | operator== (long rhs) const |
| bool | operator!= (long rhs) const |
| bool | operator== (int rhs) const |
| bool | operator!= (int rhs) const |
| vnl_rational | operator- () const |
| Unary minus - returns the negation of the current rational. More... | |
| vnl_rational | operator+ () const |
| Unary plus - returns the current rational. More... | |
| bool | operator! () const |
| Unary not - returns true if rational is equal to zero. More... | |
| vnl_rational | abs () const |
| Returns the absolute value of the current rational. More... | |
| vnl_rational & | invert () |
| Replaces rational with 1/rational and returns it. More... | |
| vnl_rational & | operator+= (vnl_rational const &r) |
| Plus/assign: replace lhs by lhs + rhs. More... | |
| vnl_rational & | operator+= (long r) |
| vnl_rational & | operator-= (vnl_rational const &r) |
| Minus/assign: replace lhs by lhs - rhs. More... | |
| vnl_rational & | operator-= (long r) |
| vnl_rational & | operator *= (vnl_rational const &r) |
| Multiply/assign: replace lhs by lhs * rhs. More... | |
| vnl_rational & | operator *= (long r) |
| Multiply/assign: replace lhs by lhs * rhs. More... | |
| vnl_rational & | operator/= (vnl_rational const &r) |
| Divide/assign: replace lhs by lhs / rhs. More... | |
| vnl_rational & | operator/= (long r) |
| Divide/assign: replace lhs by lhs / rhs. More... | |
| vnl_rational & | operator%= (vnl_rational const &r) |
| Modulus/assign: replace lhs by lhs % rhs. More... | |
| vnl_rational & | operator%= (long r) |
| vnl_rational & | operator++ () |
| Pre-increment (++r). No-op when +-Inf. More... | |
| vnl_rational & | operator-- () |
| Pre-decrement (–r). No-op when +-Inf. More... | |
| vnl_rational | operator++ (int) |
| Post-increment (r++). No-op when +-Inf. More... | |
| vnl_rational | operator-- (int) |
| Post-decrement (r–). No-op when +-Inf. More... | |
| bool | operator< (vnl_rational const &rhs) const |
| bool | operator> (vnl_rational const &r) const |
| bool | operator<= (vnl_rational const &r) const |
| bool | operator>= (vnl_rational const &r) const |
| bool | operator< (long r) const |
| bool | operator> (long r) const |
| bool | operator<= (long r) const |
| bool | operator>= (long r) const |
| bool | operator< (int r) const |
| bool | operator> (int r) const |
| bool | operator<= (int r) const |
| bool | operator>= (int r) const |
| bool | operator< (double r) const |
| bool | operator> (double r) const |
| bool | operator<= (double r) const |
| bool | operator>= (double r) const |
| long | truncate () const |
| Converts rational value to integer by truncating towards zero. More... | |
| long | floor () const |
| Converts rational value to integer by truncating towards negative infinity. More... | |
| long | ceil () const |
| Converts rational value to integer by truncating towards positive infinity. More... | |
| long | round () const |
| Rounds rational to nearest integer. More... | |
| operator short () | |
| operator int () | |
| operator long () const | |
| operator long () | |
| operator float () const | |
| operator float () | |
| operator double () const | |
| operator double () | |
Static Public Member Functions | |
| static long | gcd (long l1, long l2) |
| Calculate greatest common divisor of two integers. More... | |
Private Member Functions | |
| void | normalize () |
| Private function to normalize numerator/denominator of rational number. More... | |
Private Attributes | |
| long | num_ |
| Numerator portion. More... | |
| long | den_ |
| Denominator portion. More... | |
Related Functions | |
(Note that these are not member functions.) | |
| void | vsl_b_write (vsl_b_ostream &os, vnl_rational const &v) |
| Binary save vnl_rational to stream. More... | |
| void | vsl_b_read (vsl_b_istream &is, vnl_rational &v) |
| Binary load vnl_rational from stream. More... | |
| void | vsl_print_summary (std::ostream &os, vnl_rational const &b) |
| Print human readable summary of object to a stream. More... | |
| std::ostream & | operator<< (std::ostream &s, vnl_rational const &r) |
| formatted output. More... | |
| std::istream & | operator>> (std::istream &s, vnl_rational &r) |
| simple input. More... | |
| vnl_rational | operator+ (vnl_rational const &r1, vnl_rational const &r2) |
| Returns the sum of two rational numbers. More... | |
| vnl_rational | operator- (vnl_rational const &r1, vnl_rational const &r2) |
| Returns the difference of two rational numbers. More... | |
| vnl_rational | operator * (vnl_rational const &r1, vnl_rational const &r2) |
| Returns the product of two rational numbers. More... | |
| vnl_rational | operator/ (vnl_rational const &r1, vnl_rational const &r2) |
| Returns the quotient of two rational numbers. More... | |
| vnl_rational | operator% (vnl_rational const &r1, vnl_rational const &r2) |
| Returns the remainder of r1 divided by r2. More... | |
High-precision rational numbers.
The vnl_rational class provides high-precision rational numbers and arithmetic, using the built-in type long, for the numerator and denominator. Implicit conversion to the system defined types short, int, long, float, and double is supported by overloaded operator member functions. Although the rational class makes judicious use of inline functions and deals only with integral values, the user is warned that the rational integer arithmetic class is still considerably slower than the built-in integer data types. If the range of values anticipated will fit into a built-in type, use that instead.
In addition to the original COOL Rational class, vnl_rational is able to represent plus and minus infinity. An other interesting addition is the possibility to construct a rational from a double. This allows for lossless conversion from e.g. double 1.0/3.0 to the rational number 1/3, hence no more rounding errors. This is implemented with continued fraction approximations.
Definition at line 73 of file vnl_rational.h.
|
inline |
Creates a rational with given numerator and denominator.
Default constructor gives 0. Also serves as automatic cast from long to vnl_rational. The only input which is not allowed is (0,0); the denominator is allowed to be 0, to represent +Inf or -Inf.
Definition at line 86 of file vnl_rational.h.
|
inline |
Definition at line 89 of file vnl_rational.h.
|
inline |
Definition at line 91 of file vnl_rational.h.
|
inline |
Definition at line 94 of file vnl_rational.h.
|
inline |
Definition at line 96 of file vnl_rational.h.
|
inline |
Definition at line 99 of file vnl_rational.h.
|
inline |
Definition at line 101 of file vnl_rational.h.
|
inline |
Definition at line 104 of file vnl_rational.h.
|
inline |
Definition at line 106 of file vnl_rational.h.
|
inline |
Definition at line 109 of file vnl_rational.h.
|
inline |
Definition at line 111 of file vnl_rational.h.
|
inline |
Definition at line 114 of file vnl_rational.h.
|
inline |
Definition at line 116 of file vnl_rational.h.
| vnl_rational::vnl_rational | ( | double | d | ) |
Creates a rational from a double.
This is done by computing the continued fraction approximation for d. Note that this is explicitly not an automatic type conversion.
This is done by computing the continued fraction approximation for d.
Definition at line 33 of file vnl_rational.cxx.
| vnl_rational::vnl_rational | ( | float | f | ) |
Creates a rational from a double.
This is done by computing the continued fraction approximation for d.
Definition at line 40 of file vnl_rational.cxx.
|
inline |
Definition at line 125 of file vnl_rational.h.
|
inlinedefault |
|
inline |
Returns the absolute value of the current rational.
Definition at line 157 of file vnl_rational.h.
|
inline |
Converts rational value to integer by truncating towards positive infinity.
Definition at line 255 of file vnl_rational.h.
|
inline |
Return the denominator of the (simplified) rational number representation.
Definition at line 135 of file vnl_rational.h.
|
inline |
Converts rational value to integer by truncating towards negative infinity.
Definition at line 252 of file vnl_rational.h.
|
inlinestatic |
Calculate greatest common divisor of two integers.
Used to simplify rational number.
Definition at line 283 of file vnl_rational.h.
|
inline |
Replaces rational with 1/rational and returns it.
Inverting 0 gives +Inf, inverting +-Inf gives 0.
Definition at line 160 of file vnl_rational.h.
|
inlineprivate |
Private function to normalize numerator/denominator of rational number.
If num_ and den_ are both nonzero, their gcd is made 1 and den_ made positive. Otherwise, the nonzero den_ is set to 1 or the nonzero num_ to +1 or -1.
Definition at line 292 of file vnl_rational.h.
|
inline |
Return the numerator of the (simplified) rational number representation.
Definition at line 133 of file vnl_rational.h.
| vnl_rational & vnl_rational::operator *= | ( | vnl_rational const & | r | ) |
Multiply/assign: replace lhs by lhs * rhs.
Note that 0 * Inf and Inf * 0 are undefined. Also note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.
Definition at line 49 of file vnl_rational.cxx.
| vnl_rational & vnl_rational::operator *= | ( | long | r | ) |
Multiply/assign: replace lhs by lhs * rhs.
Note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.
Definition at line 68 of file vnl_rational.cxx.
|
inline |
Definition at line 278 of file vnl_rational.h.
|
inline |
Definition at line 279 of file vnl_rational.h.
|
inline |
Definition at line 276 of file vnl_rational.h.
|
inline |
Definition at line 277 of file vnl_rational.h.
|
inline |
Definition at line 269 of file vnl_rational.h.
|
inline |
Definition at line 274 of file vnl_rational.h.
|
inline |
Definition at line 275 of file vnl_rational.h.
|
inline |
Definition at line 264 of file vnl_rational.h.
|
inline |
Unary not - returns true if rational is equal to zero.
Definition at line 155 of file vnl_rational.h.
|
inline |
Definition at line 144 of file vnl_rational.h.
|
inline |
Definition at line 146 of file vnl_rational.h.
|
inline |
Definition at line 148 of file vnl_rational.h.
|
inline |
Modulus/assign: replace lhs by lhs % rhs.
Note that r % Inf is r, and that r % 0 and Inf % r are undefined.
Definition at line 206 of file vnl_rational.h.
|
inline |
Definition at line 215 of file vnl_rational.h.
|
inline |
Unary plus - returns the current rational.
Definition at line 153 of file vnl_rational.h.
|
inline |
Pre-increment (++r). No-op when +-Inf.
Definition at line 218 of file vnl_rational.h.
|
inline |
Post-increment (r++). No-op when +-Inf.
Definition at line 222 of file vnl_rational.h.
|
inline |
Plus/assign: replace lhs by lhs + rhs.
Note that +Inf + -Inf and -Inf + +Inf are undefined.
Definition at line 165 of file vnl_rational.h.
|
inline |
Definition at line 173 of file vnl_rational.h.
|
inline |
Unary minus - returns the negation of the current rational.
Definition at line 151 of file vnl_rational.h.
|
inline |
Pre-decrement (–r). No-op when +-Inf.
Definition at line 220 of file vnl_rational.h.
|
inline |
Post-decrement (r–). No-op when +-Inf.
Definition at line 224 of file vnl_rational.h.
|
inline |
Minus/assign: replace lhs by lhs - rhs.
Note that +Inf - +Inf and -Inf - -Inf are undefined.
Definition at line 176 of file vnl_rational.h.
|
inline |
Definition at line 184 of file vnl_rational.h.
| vnl_rational & vnl_rational::operator/= | ( | vnl_rational const & | r | ) |
Divide/assign: replace lhs by lhs / rhs.
Note that 0 / 0 and Inf / Inf are undefined. Also note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.
Definition at line 84 of file vnl_rational.cxx.
| vnl_rational & vnl_rational::operator/= | ( | long | r | ) |
Divide/assign: replace lhs by lhs / rhs.
Note that 0 / 0 is undefined. Also note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.
Definition at line 104 of file vnl_rational.cxx.
|
inline |
Definition at line 226 of file vnl_rational.h.
|
inline |
Definition at line 236 of file vnl_rational.h.
|
inline |
Definition at line 240 of file vnl_rational.h.
|
inline |
Definition at line 244 of file vnl_rational.h.
|
inline |
Definition at line 234 of file vnl_rational.h.
|
inline |
Definition at line 238 of file vnl_rational.h.
|
inline |
Definition at line 242 of file vnl_rational.h.
|
inline |
Definition at line 246 of file vnl_rational.h.
|
inline |
Copies the contents and state of rhs rational over to the lhs.
Definition at line 138 of file vnl_rational.h.
|
inline |
Returns true if the two rationals have the same representation.
Definition at line 142 of file vnl_rational.h.
|
inline |
Definition at line 145 of file vnl_rational.h.
|
inline |
Definition at line 147 of file vnl_rational.h.
|
inline |
Definition at line 233 of file vnl_rational.h.
|
inline |
Definition at line 237 of file vnl_rational.h.
|
inline |
Definition at line 241 of file vnl_rational.h.
|
inline |
Definition at line 245 of file vnl_rational.h.
|
inline |
Definition at line 235 of file vnl_rational.h.
|
inline |
Definition at line 239 of file vnl_rational.h.
|
inline |
Definition at line 243 of file vnl_rational.h.
|
inline |
Definition at line 247 of file vnl_rational.h.
|
inline |
Rounds rational to nearest integer.
Definition at line 258 of file vnl_rational.h.
|
inline |
Definition at line 130 of file vnl_rational.h.
|
inline |
Converts rational value to integer by truncating towards zero.
Definition at line 250 of file vnl_rational.h.
|
related |
Returns the product of two rational numbers.
Definition at line 375 of file vnl_rational.h.
|
related |
Returns the remainder of r1 divided by r2.
Definition at line 429 of file vnl_rational.h.
|
related |
Returns the sum of two rational numbers.
Definition at line 321 of file vnl_rational.h.
|
related |
Returns the difference of two rational numbers.
Definition at line 348 of file vnl_rational.h.
|
related |
Returns the quotient of two rational numbers.
Definition at line 402 of file vnl_rational.h.
|
related |
formatted output.
Definition at line 306 of file vnl_rational.h.
|
related |
simple input.
Definition at line 313 of file vnl_rational.h.
|
related |
Binary load vnl_rational from stream.
Definition at line 20 of file vnl_io_rational.cxx.
|
related |
Binary save vnl_rational to stream.
Definition at line 10 of file vnl_io_rational.cxx.
|
related |
Print human readable summary of object to a stream.
Definition at line 44 of file vnl_io_rational.cxx.
|
private |
Denominator portion.
Definition at line 77 of file vnl_rational.h.
|
private |
Numerator portion.
Definition at line 76 of file vnl_rational.h.
1.8.15