Calculator  Step 6
Classes | Functions
rational.hpp File Reference
#include <istream>
#include <limits>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include "gcd.hpp"
#include "ioflags.hpp"
#include "power10.hpp"

Go to the source code of this file.

Classes

class  zero_denominator
 Exception class if the denominator is ever zero. More...
 
class  rational< T >
 Represent a rational number (fraction) as a numerator and denominator. More...
 

Functions

template<class T >
rational< T > operator- (rational< T > const &r)
 Negate a rational number. More...
 
template<class T >
rational< T > absval (rational< T > const &r)
 
template<class T >
rational< T > operator+ (rational< T > lhs, rational< T > const &rhs)
 Addition. More...
 
template<class T >
rational< T > operator+ (rational< T > lhs, T const &rhs)
 Addition. More...
 
template<class T >
rational< T > operator+ (T const &lhs, rational< T > rhs)
 Addition. More...
 
template<class T >
rational< T > operator- (rational< T > lhs, rational< T > const &rhs)
 Subtraction. More...
 
template<class T >
rational< T > operator- (rational< T > lhs, T const &rhs)
 Subtraction. More...
 
template<class T >
rational< T > operator- (T const &lhs, rational< T > rhs)
 Subtraction. More...
 
template<class T >
rational< T > operator* (rational< T > lhs, rational< T > const &rhs)
 Multiplication. More...
 
template<class T >
rational< T > operator* (rational< T > lhs, T const &rhs)
 Multiplication. More...
 
template<class T >
rational< T > operator* (T const &lhs, rational< T > rhs)
 Multiplication. More...
 
template<class T >
rational< T > operator/ (rational< T > lhs, rational< T > const &rhs)
 Division. More...
 
template<class T >
rational< T > operator/ (rational< T > lhs, T const &rhs)
 Division. More...
 
template<class T >
rational< T > operator/ (T const &lhs, rational< T > rhs)
 Division. More...
 
template<class T , class U >
bool operator== (rational< T > const &a, rational< U > const &b)
 Equality comparison. More...
 
template<class T >
bool operator== (rational< T > const &lhs, T rhs)
 Equality comparison. More...
 
template<class T >
bool operator== (T lhs, rational< T > const &rhs)
 Equality comparison. More...
 
template<class T >
bool operator< (rational< T > const &a, rational< T > const &b)
 Less-than comparison. More...
 
template<class T >
bool operator< (rational< T > const &a, T const &b)
 Less-than comparison. More...
 
template<class T >
bool operator< (T const &a, rational< T > const &b)
 Less-than comparison. More...
 
template<class T , class U >
bool operator!= (rational< T > const &a, rational< U > const &b)
 Inequality comparison. More...
 
template<class T >
bool operator!= (rational< T > const &a, T b)
 Inequality comparison. More...
 
template<class T >
bool operator!= (T a, rational< T > const &b)
 Inequality comparison. More...
 
template<class T >
bool operator<= (rational< T > const &a, rational< T > const &b)
 Less-than-or-equal comparison. More...
 
template<class T >
bool operator<= (rational< T > const &a, T const &b)
 Less-than-or-equal comparison. More...
 
template<class T >
bool operator<= (T const &a, rational< T > const &b)
 Less-than-or-equal comparison. More...
 
template<class T >
bool operator> (rational< T > const &a, rational< T > const &b)
 Greater-than comparison. More...
 
template<class T >
bool operator> (rational< T > const &a, T const &b)
 Greater-than comparison. More...
 
template<class T >
bool operator> (T const &a, rational< T > const &b)
 Greater-than comparison. More...
 
template<class T >
bool operator>= (rational< T > const &a, rational< T > const &b)
 Greater-than-or-equal comparison. More...
 
template<class T >
bool operator>= (rational< T > const &a, T const &b)
 Greater-than-or-equal comparison. More...
 
template<class T >
bool operator>= (T const &a, rational< T > const &b)
 Greater-than-or-equal comparison. More...
 
template<class T , class Char , class Traits >
std::basic_istream< Char,
Traits > & 
operator>> (std::basic_istream< Char, Traits > &in, rational< T > &rat)
 Input operator. More...
 
template<class T , class Char , class Traits >
std::basic_ostream< Char,
Traits > & 
operator<< (std::basic_ostream< Char, Traits > &out, rational< T > const &rat)
 Output operator. More...
 

Function Documentation

template<class T >
rational<T> absval ( rational< T > const &  r)

Definition at line 292 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

293 {
294  using std::abs;
295  return rational<T>{abs(r.numerator()), r.denominator()};
296 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
template<class T , class U >
bool operator!= ( rational< T > const &  a,
rational< U > const &  b 
)
inline

Inequality comparison.

Definition at line 442 of file rational.hpp.

443 {
444  return not (a == b);
445 }
template<class T >
bool operator!= ( rational< T > const &  a,
b 
)
inline

Inequality comparison.

Definition at line 449 of file rational.hpp.

450 {
451  return not (a == b);
452 }
template<class T >
bool operator!= ( a,
rational< T > const &  b 
)
inline

Inequality comparison.

Definition at line 456 of file rational.hpp.

457 {
458  return not (a == b);
459 }
template<class T >
rational<T> operator* ( rational< T >  lhs,
rational< T > const &  rhs 
)

Multiplication.

Definition at line 349 of file rational.hpp.

350 {
351  lhs *= rhs;
352  return lhs;
353 }
template<class T >
rational<T> operator* ( rational< T >  lhs,
T const &  rhs 
)

Multiplication.

Definition at line 357 of file rational.hpp.

358 {
359  lhs *= rhs;
360  return lhs;
361 }
template<class T >
rational<T> operator* ( T const &  lhs,
rational< T >  rhs 
)

Multiplication.

Definition at line 365 of file rational.hpp.

366 {
367  rhs *= lhs;
368  return rhs;
369 }
template<class T >
rational<T> operator+ ( rational< T >  lhs,
rational< T > const &  rhs 
)

Addition.

Definition at line 300 of file rational.hpp.

301 {
302  lhs += rhs;
303  return lhs;
304 }
template<class T >
rational<T> operator+ ( rational< T >  lhs,
T const &  rhs 
)

Addition.

Definition at line 308 of file rational.hpp.

309 {
310  lhs += rhs;
311  return lhs;
312 }
template<class T >
rational<T> operator+ ( T const &  lhs,
rational< T >  rhs 
)

Addition.

Definition at line 316 of file rational.hpp.

317 {
318  rhs += lhs;
319  return rhs;
320 }
template<class T >
rational<T> operator- ( rational< T > const &  r)

Negate a rational number.

Definition at line 286 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

287 {
288  return rational<T>{-r.numerator(), r.denominator()};
289 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
template<class T >
rational<T> operator- ( rational< T >  lhs,
rational< T > const &  rhs 
)

Subtraction.

Definition at line 324 of file rational.hpp.

325 {
326  lhs -= rhs;
327  return lhs;
328 }
template<class T >
rational<T> operator- ( rational< T >  lhs,
T const &  rhs 
)

Subtraction.

Definition at line 332 of file rational.hpp.

333 {
334  lhs -= rhs;
335  return lhs;
336 }
template<class T >
rational<T> operator- ( T const &  lhs,
rational< T >  rhs 
)

Subtraction.

Definition at line 340 of file rational.hpp.

341 {
342  // Gotta be a little tricky.
343  rhs += -lhs;
344  return -rhs;
345 }
template<class T >
rational<T> operator/ ( rational< T >  lhs,
rational< T > const &  rhs 
)

Division.

Definition at line 373 of file rational.hpp.

374 {
375  lhs /= rhs;
376  return lhs;
377 }
template<class T >
rational<T> operator/ ( rational< T >  lhs,
T const &  rhs 
)

Division.

Definition at line 381 of file rational.hpp.

382 {
383  lhs /= rhs;
384  return lhs;
385 }
template<class T >
rational<T> operator/ ( T const &  lhs,
rational< T >  rhs 
)

Division.

Definition at line 389 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

390 {
391  return rational<T>{lhs * rhs.denominator(), rhs.numerator()};
392 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
template<class T >
bool operator< ( rational< T > const &  a,
rational< T > const &  b 
)

Less-than comparison.

Definition at line 421 of file rational.hpp.

References rational< T >::numerator().

422 {
423  return a.numerator() * b.denominator() < b.numerator() * a.denominator();
424 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >
bool operator< ( rational< T > const &  a,
T const &  b 
)

Less-than comparison.

Definition at line 428 of file rational.hpp.

429 {
430  return a.numerator() < b * a.denominator();
431 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >
bool operator< ( T const &  a,
rational< T > const &  b 
)

Less-than comparison.

Definition at line 435 of file rational.hpp.

436 {
437  return a * b.denominator() < b.numerator();
438 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T , class Char , class Traits >
std::basic_ostream<Char, Traits>& operator<< ( std::basic_ostream< Char, Traits > &  out,
rational< T > const &  rat 
)

Output operator.

Definition at line 562 of file rational.hpp.

References rational< T >::numerator().

563 {
564  typename std::basic_ostream<Char, Traits>::sentry sentry{out};
565  std::ostringstream stream;
566  stream << rat.numerator() << '/' << rat.denominator();
567  out << stream.str();
568  return out;
569 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >
bool operator<= ( rational< T > const &  a,
rational< T > const &  b 
)
inline

Less-than-or-equal comparison.

Definition at line 463 of file rational.hpp.

464 {
465  return not (b < a);
466 }
template<class T >
bool operator<= ( rational< T > const &  a,
T const &  b 
)
inline

Less-than-or-equal comparison.

Definition at line 470 of file rational.hpp.

471 {
472  return not (b < a);
473 }
template<class T >
bool operator<= ( T const &  a,
rational< T > const &  b 
)
inline

Less-than-or-equal comparison.

Definition at line 477 of file rational.hpp.

478 {
479  return not (b < a);
480 }
template<class T , class U >
bool operator== ( rational< T > const &  a,
rational< U > const &  b 
)

Equality comparison.

Definition at line 397 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

398 {
399  return a.numerator() == b.numerator() and
400  a.denominator() == b.denominator();
401 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >
bool operator== ( rational< T > const &  lhs,
rhs 
)

Equality comparison.

Definition at line 405 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

406 {
407  return lhs.denominator() == 1 and
408  lhs.numerator() == rhs;
409 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >
bool operator== ( lhs,
rational< T > const &  rhs 
)

Equality comparison.

Definition at line 413 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

414 {
415  return rhs.denominator() == 1 and
416  rhs.numerator() == lhs;
417 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >
bool operator> ( rational< T > const &  a,
rational< T > const &  b 
)
inline

Greater-than comparison.

Definition at line 484 of file rational.hpp.

485 {
486  return b < a;
487 }
template<class T >
bool operator> ( rational< T > const &  a,
T const &  b 
)
inline

Greater-than comparison.

Definition at line 491 of file rational.hpp.

492 {
493  return b < a;
494 }
template<class T >
bool operator> ( T const &  a,
rational< T > const &  b 
)
inline

Greater-than comparison.

Definition at line 498 of file rational.hpp.

499 {
500  return b < a;
501 }
template<class T >
bool operator>= ( rational< T > const &  a,
rational< T > const &  b 
)
inline

Greater-than-or-equal comparison.

Definition at line 505 of file rational.hpp.

506 {
507  return not (b > a);
508 }
template<class T >
bool operator>= ( rational< T > const &  a,
T const &  b 
)
inline

Greater-than-or-equal comparison.

Definition at line 512 of file rational.hpp.

513 {
514  return not (b > a);
515 }
template<class T >
bool operator>= ( T const &  a,
rational< T > const &  b 
)
inline

Greater-than-or-equal comparison.

Definition at line 519 of file rational.hpp.

520 {
521  return not (b > a);
522 }
template<class T , class Char , class Traits >
std::basic_istream<Char, Traits>& operator>> ( std::basic_istream< Char, Traits > &  in,
rational< T > &  rat 
)

Input operator.

Definition at line 526 of file rational.hpp.

527 {
528  typename std::basic_istream<Char, Traits>::sentry sentry{in, false};
529  ioflags flags{in};
530 
531  T n{};
532  if (not (in >> n))
533  // Error reading the numerator.
534  return in;
535 
536  in >> std::noskipws;
537  char sep{};
538  if (not (in >> sep))
539  // Error reading the separator character.
540  return in;
541  else if (sep != '/')
542  {
543  // Push sep back into the input stream, so the next input operation
544  // will read it.
545  in.unget();
546  rat = n;
547  return in;
548  }
549  else
550  {
551  T d{};
552  if (in >> d)
553  // Successfully read numerator, separator, and denominator.
554  rat = rational<T>{n, d};
555  }
556 
557  return in;
558 }
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26