PSCF v1.1
List of all members
Util::Rational Class Reference

A Rational number (a ratio of integers). More...

#include <Rational.h>

Public Member Functions

Constructors
 Rational ()
 Default constructor. More...
 
 Rational (int num, int den)
 Constructor, explicit numerator and denominator. More...
 
 Rational (int number)
 Constructor, construct from integer. More...
 
 Rational (Rational const &v)
 Copy constructor. More...
 
 ~Rational ()
 Destructor. More...
 
Assignment and Conversion.
Rationaloperator= (Rational const &other)
 Copy assignment from another Rational. More...
 
Rationaloperator= (int other)
 Assignment from integer. More...
 
Arithmetic Assignment Operators
Rationaloperator+= (Rational const &a)
 Add another rational to this one. More...
 
Rationaloperator+= (int a)
 Add an integer to this rational. More...
 
Rationaloperator-= (Rational const &a)
 Subtract another rational from this one. More...
 
Rationaloperator-= (int)
 Subtract an integer from this rational. More...
 
Rationaloperator*= (Rational const &a)
 Multiply this rational by another. More...
 
Rationaloperator*= (int a)
 Multiply this rational by an integer. More...
 
Rationaloperator/= (Rational const &a)
 Divide this rational by another. More...
 
Rationaloperator/= (int a)
 Divide this rational by an integer. More...
 

Accessors

int num () const
 Return numerator. More...
 
int den () const
 Return denominator. More...
 
 operator double () const
 Cast (convert) to double precision floating point. More...
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 Serialize to/from an archive. More...
 
static void commitMpiType ()
 Commit MPI datatype MpiTraits<Rational>::type. More...
 
Rational operator+ (Rational const &a, Rational const &b)
 Compute sum of two rationals. More...
 
Rational operator+ (Rational const &a, int b)
 Compute sum of rational and integer. More...
 
Rational operator- (Rational const &a, Rational const &b)
 Compute difference of rationals. More...
 
Rational operator- (Rational const &a, int b)
 Compute difference of rational and integer. More...
 
Rational operator- (int b, Rational const &a)
 Compute difference of integer and rational. More...
 
Rational operator* (Rational const &a, Rational const &b)
 Compute product of rationals. More...
 
Rational operator* (Rational const &a, int b)
 Compute product of rational and integer. More...
 
Rational operator/ (Rational const &a, Rational const &b)
 Compute quotient of two rationals. More...
 
Rational operator/ (Rational const &a, int b)
 Compute quotient Rational divided by integer. More...
 
Rational operator/ (int b, Rational const &a)
 Compute quotient integer divided by Rational. More...
 
bool operator== (Rational const &a, Rational const &b)
 Equality operators. More...
 
bool operator== (Rational const &a, int b)
 Equality operator for a Rational and an integer. More...
 
Rational operator- (Rational const &a)
 Unary negation of Rational. More...
 
std::ostream & operator<< (std::ostream &out, Rational const &rational)
 Output stream inserter for a Rational. More...
 

Detailed Description

A Rational number (a ratio of integers).

A rational is always stored in a standard reduced form in which the denominator is a positive integer and the numerator and denominator have no common divisors other than unity. All integers, including zero, are stored with a denominator of 1.

Definition at line 33 of file Rational.h.

Constructor & Destructor Documentation

◆ Rational() [1/4]

Util::Rational::Rational ( )
inline

Default constructor.

Definition at line 271 of file Rational.h.

◆ Rational() [2/4]

Util::Rational::Rational ( int  num,
int  den 
)
inline

Constructor, explicit numerator and denominator.

Denominator is reduced to greatest common divisor before return.

Parameters
numnumerator
dendenominator

Definition at line 280 of file Rational.h.

◆ Rational() [3/4]

Util::Rational::Rational ( int  number)
inline

Constructor, construct from integer.

Creates a rational with a denominator == 1.

Parameters
numberinteger number.

Definition at line 289 of file Rational.h.

◆ Rational() [4/4]

Util::Rational::Rational ( Rational const &  v)
inline

Copy constructor.

Parameters
vRational to be copied

Definition at line 298 of file Rational.h.

◆ ~Rational()

Util::Rational::~Rational ( )
inline

Destructor.

Definition at line 75 of file Rational.h.

Member Function Documentation

◆ operator=() [1/2]

Rational & Util::Rational::operator= ( Rational const &  other)
inline

Copy assignment from another Rational.

Parameters
otherRational to assign.

Definition at line 307 of file Rational.h.

◆ operator=() [2/2]

Rational & Util::Rational::operator= ( int  other)
inline

Assignment from integer.

Creates an integer using a denominator == 1.

Parameters
otherinteger to assign.

Definition at line 318 of file Rational.h.

◆ operator+=() [1/2]

Rational & Util::Rational::operator+= ( Rational const &  a)
inline

Add another rational to this one.

Upon return, *this = this + a.

Parameters
aincrement (input)

Definition at line 331 of file Rational.h.

◆ operator+=() [2/2]

Rational & Util::Rational::operator+= ( int  a)
inline

Add an integer to this rational.

Upon return, *this = this + a.

Parameters
aincrement (input)

Definition at line 343 of file Rational.h.

◆ operator-=() [1/2]

Rational & Util::Rational::operator-= ( Rational const &  a)
inline

Subtract another rational from this one.

Upon return, *this = this + a.

Parameters
arational decrement (input)

Definition at line 354 of file Rational.h.

◆ operator-=() [2/2]

Rational & Util::Rational::operator-= ( int  a)
inline

Subtract an integer from this rational.

Upon return, *this = this + a.

Parameters
ainteger decrement (input)

Definition at line 366 of file Rational.h.

◆ operator*=() [1/2]

Rational & Util::Rational::operator*= ( Rational const &  a)
inline

Multiply this rational by another.

Upon return, *this = this*a.

Parameters
aRational number to multiply this by (input)

Definition at line 377 of file Rational.h.

◆ operator*=() [2/2]

Rational & Util::Rational::operator*= ( int  a)
inline

Multiply this rational by an integer.

Upon return, *this = this*a.

Parameters
ainteger to multiply this by (input)

Definition at line 389 of file Rational.h.

◆ operator/=() [1/2]

Rational & Util::Rational::operator/= ( Rational const &  a)
inline

Divide this rational by another.

Upon return, *this = this*a.

Parameters
arational number to divide this by (input)

Definition at line 400 of file Rational.h.

References UTIL_THROW.

◆ operator/=() [2/2]

Rational & Util::Rational::operator/= ( int  a)
inline

Divide this rational by an integer.

Upon return, *this = this*a.

Parameters
ainteger to divide this by (input)

Definition at line 415 of file Rational.h.

References UTIL_THROW.

◆ num()

int Util::Rational::num ( ) const
inline

Return numerator.

Definition at line 431 of file Rational.h.

◆ den()

int Util::Rational::den ( ) const
inline

Return denominator.

Definition at line 438 of file Rational.h.

◆ operator double()

Util::Rational::operator double ( ) const
inline

Cast (convert) to double precision floating point.

Returns
double precision representation of this.

Definition at line 445 of file Rational.h.

◆ serialize()

template<class Archive >
void Util::Rational::serialize ( Archive &  ar,
const unsigned int  version 
)
inline

Serialize to/from an archive.

Parameters
ararchive
versionarchive version id

Definition at line 454 of file Rational.h.

◆ commitMpiType()

static void Util::Rational::commitMpiType ( )
static

Commit MPI datatype MpiTraits<Rational>::type.

Friends And Related Function Documentation

◆ operator+ [1/2]

Rational operator+ ( Rational const &  a,
Rational const &  b 
)
friend

Compute sum of two rationals.

Parameters
a1st argument
b2st argument
Returns
sum a + b

Definition at line 490 of file Rational.h.

◆ operator+ [2/2]

Rational operator+ ( Rational const &  a,
int  b 
)
friend

Compute sum of rational and integer.

Parameters
aRational argument
binteger argument
Returns
sum a + b

Definition at line 505 of file Rational.h.

◆ operator- [1/4]

Rational operator- ( Rational const &  a,
Rational const &  b 
)
friend

Compute difference of rationals.

Parameters
a1st argument
b2st argument
Returns
difference a - b

Definition at line 530 of file Rational.h.

◆ operator- [2/4]

Rational operator- ( Rational const &  a,
int  b 
)
friend

Compute difference of rational and integer.

Parameters
aRational argument
binteger argument
Returns
difference a - b

Definition at line 545 of file Rational.h.

◆ operator- [3/4]

Rational operator- ( int  b,
Rational const &  a 
)
friend

Compute difference of integer and rational.

Parameters
binteger argument
aRational argument
Returns
difference b - a

Definition at line 559 of file Rational.h.

◆ operator* [1/2]

Rational operator* ( Rational const &  a,
Rational const &  b 
)
friend

Compute product of rationals.

Parameters
a1st Rational argument
b2st Rational argument
Returns
product a*b

Definition at line 573 of file Rational.h.

◆ operator* [2/2]

Rational operator* ( Rational const &  a,
int  b 
)
friend

Compute product of rational and integer.

Parameters
aRational argument
binteger argument
Returns
product a*b

Definition at line 588 of file Rational.h.

◆ operator/ [1/3]

Rational operator/ ( Rational const &  a,
Rational const &  b 
)
friend

Compute quotient of two rationals.

Parameters
a1st Rational argument (numerator)
b2st Rational argument (denominator)
Returns
ratio a/b

Definition at line 610 of file Rational.h.

◆ operator/ [2/3]

Rational operator/ ( Rational const &  a,
int  b 
)
friend

Compute quotient Rational divided by integer.

Parameters
aRational argument (numerator)
binteger argument (denominator)
Returns
ratio a/b

Definition at line 628 of file Rational.h.

◆ operator/ [3/3]

Rational operator/ ( int  b,
Rational const &  a 
)
friend

Compute quotient integer divided by Rational.

Parameters
binteger argument (numerator)
aRational argument (denominator)
Returns
ratio b/a

Definition at line 644 of file Rational.h.

◆ operator== [1/2]

bool operator== ( Rational const &  a,
Rational const &  b 
)
friend

Equality operators.

Equality operator for two Rational numbers.

Parameters
a1st Rational
b2nd Rational
Returns
true if equal, false otherwise

Definition at line 674 of file Rational.h.

◆ operator== [2/2]

bool operator== ( Rational const &  a,
int  b 
)
friend

Equality operator for a Rational and an integer.

Parameters
aRational number
binteger number
Returns
true if equal, false otherwise

Definition at line 684 of file Rational.h.

◆ operator- [4/4]

Rational operator- ( Rational const &  a)
friend

Unary negation of Rational.

Parameters
aRational number
Returns
negation -a

Definition at line 661 of file Rational.h.

◆ operator<<

std::ostream & operator<< ( std::ostream &  out,
Rational const &  rational 
)
friend

Output stream inserter for a Rational.

Output elements of a rational to stream, without line breaks.

Parameters
outoutput stream
rationalRational to be written to stream
Returns
modified output stream

Definition at line 16 of file Rational.cpp.


The documentation for this class was generated from the following file: