Public Member Functions | Public Attributes | List of all members
Celartem::Rational< Int > Struct Template Reference

#include <cel_rational.h>

Public Member Functions

 Rational ()
 
 Rational (const Int &n)
 
 Rational (const Int &n, const Int &d)
 
 Rational (const Rational &r)
 
Rationaloperator= (const Rational &r)
 
Rationaloperator= (const Int &n)
 
void assign (const Int &n, const Int &d)
 
void normalize ()
 
Rationaloperator+= (const Rational &r)
 
Rationaloperator-= (const Rational &r)
 
Rationaloperator*= (const Rational &r)
 
Rationaloperator/= (const Rational &r)
 
const Rationaloperator++ ()
 
const Rationaloperator-- ()
 
Rational operator+ (const Rational< Int > &r) const
 
Rational operator- (const Rational< Int > &r) const
 
Rational operator* (const Rational< Int > &r) const
 
Rational operator/ (const Rational< Int > &r) const
 
bool operator< (const Rational &r) const
 
bool operator<= (const Rational &r) const
 
bool operator== (const Rational &r) const
 
bool operator!= (const Rational &r) const
 
bool operator> (const Rational &r) const
 
bool operator>= (const Rational &r) const
 
Rational reciprocal () const
 
void serialize (Stream *inStream, size_t inLevel, Endian inEndian) const
 
void deserialize (Stream *inStream, size_t inLevel, Endian inEndian)
 

Public Attributes

Int num
 
Int den
 

Detailed Description

template<typename Int>
struct Celartem::Rational< Int >

Rational template struct stores a fraction value.
The following is a sample use of this struct.

Rational<int> a(1,2), b(2,3), c(2);
Rational<int> d = (a + b) / c;
Rational<int> e = d * 3;

The following is not allowed due to C++'s restriction:

Rational<int> e = 3 + d;

You should place the Rational instance first:

Rational<int> e = d + 3;

This struct also provides serialize / deserialize methods and its own DataTraits to support serialization/deserialization. This struct could be used with SimpleArray and DataArray.
For data compatibility with TIFF 6.0 Specification, this library provides TIFF_RATIONAL and TIFF_SRATIONAL structure.

See Also
TIFF_RATIONAL, TIFF_SRATIONAL

Portion of this code is inspired by Boost C++ libraries:
© Copyright Paul Moore 1999. Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.

Constructor & Destructor Documentation

template<typename Int>
Celartem::Rational< Int >::Rational ( )
inline

Default constructor; it initializes the value to 0 (0/1).

Referenced by Celartem::Rational< Int >::reciprocal().

template<typename Int>
Celartem::Rational< Int >::Rational ( const Int &  n)
inline

This constructor initializes the value with the integral value.

Parameters
nThe value.
template<typename Int>
Celartem::Rational< Int >::Rational ( const Int &  n,
const Int &  d 
)
inline

This constructor initializes the value with the specified numerator/denominator value.

Parameters
nThe numerator value.
dThe denominator value.
template<typename Int>
Celartem::Rational< Int >::Rational ( const Rational< Int > &  r)
inline

This constructor initializes the value with the specified Rational value.

Parameters
rThe value.

Member Function Documentation

template<typename Int>
void Celartem::Rational< Int >::assign ( const Int &  n,
const Int &  d 
)
inline

This method assigns the specified numerator/denominator value.
This method is slightly faster than initializing the value by copy-operator.

Parameters
nThe numerator value.
dThe denominator value.
template<typename Int>
void Celartem::Rational< Int >::deserialize ( Stream inStream,
size_t  inLevel,
Endian  inEndian 
)
inline

This method is just a helper function to deal with SerializableData template. For more information, see SerializableData.

See Also
SerializableData,Serializable, DataStore
template<typename Int>
void Celartem::Rational< Int >::normalize ( )
inline

This method normalizes the value stored in this instance.
This function is useful when you directly overwrite the value of num and/or den.
Please note that all assignment methods (constructors, copy-operators and assign method) internally do normalization and you don't have to call this method after calling them.
If the denominator is 0, this method throws an Exception with errInvalidState .

Referenced by Celartem::Rational< Int >::assign(), and Celartem::Rational< Int >::Rational().

template<typename Int>
bool Celartem::Rational< Int >::operator!= ( const Rational< Int > &  r) const
inline

This method is provided for the purpose of comparing Rational values.

Returns
ture if the value is not equal to r.

This method does not work if either of the Rational instances are not normalized. If the value is not normalized, you should call normalize before calling this method.

template<typename Int>
Rational Celartem::Rational< Int >::operator* ( const Rational< Int > &  r) const
inline

This function multiplies the value by the specified Rational value.

Parameters
rThe values to multiply with.
Returns
The result.
template<typename Int>
Rational& Celartem::Rational< Int >::operator*= ( const Rational< Int > &  r)
inline

This method multiplies this value and the specified Rational value together and store the product into this instance.

Parameters
rThe value to multiply with.
Returns
The reference to this Rational instance.
template<typename Int>
Rational Celartem::Rational< Int >::operator+ ( const Rational< Int > &  r) const
inline

This function adds the Rational values.

Parameters
rThe values to add.
Returns
The result.
template<typename Int>
const Rational& Celartem::Rational< Int >::operator++ ( )
inline

This method add 1 to the value.

Returns
The reference to this Rational instance.
template<typename Int>
Rational& Celartem::Rational< Int >::operator+= ( const Rational< Int > &  r)
inline

This method add the specified Rational value to this value.

Parameters
rThe value to add.
Returns
The reference to this Rational instance.
template<typename Int>
Rational Celartem::Rational< Int >::operator- ( const Rational< Int > &  r) const
inline

This function subtract the Rational value from this value.

Parameters
rThe value to subtract.
Returns
The result.
template<typename Int>
const Rational& Celartem::Rational< Int >::operator-- ( )
inline

This method subtract 1 from the value.

Returns
The reference to this Rational instance.
template<typename Int>
Rational& Celartem::Rational< Int >::operator-= ( const Rational< Int > &  r)
inline

This method subtract the specified Rational value from this value.

Parameters
rThe value to subtract.
Returns
The reference to this Rational instance.
template<typename Int>
Rational Celartem::Rational< Int >::operator/ ( const Rational< Int > &  r) const
inline

This function divides the value by the specified Rational value.

Parameters
rThe divider.
Returns
The result.
template<typename Int>
Rational& Celartem::Rational< Int >::operator/= ( const Rational< Int > &  r)
inline

This method divides this value by the specified Rational value and store the product into this instance.
If the divider value is 0, this method throws an Exception with errDivByZero .

Parameters
rThe divider value.
Returns
The reference to this Rational instance.
template<typename Int>
bool Celartem::Rational< Int >::operator< ( const Rational< Int > &  r) const
inline

This method is provided for the purpose of comparing Rational values.

Returns
ture if the value is less than r.

This method does not work if either of the Rational instances are not normalized. If the value is not normalized, you should call normalize before calling this method.

template<typename Int>
bool Celartem::Rational< Int >::operator<= ( const Rational< Int > &  r) const
inline

This method is provided for the purpose of comparing Rational values.

Returns
ture if the value is no more than r.

This method does not work if either of the Rational instances are not normalized. If the value is not normalized, you should call normalize before calling this method.

template<typename Int>
Rational& Celartem::Rational< Int >::operator= ( const Rational< Int > &  r)
inline

This method set the specified value.

Parameters
rThe value.
Returns
The reference to this Rational instance.
template<typename Int>
Rational& Celartem::Rational< Int >::operator= ( const Int &  n)
inline

This constructor initializes the value with the specified integral value.

Parameters
nThe value.
Returns
The reference to this Rational instance.
template<typename Int>
bool Celartem::Rational< Int >::operator== ( const Rational< Int > &  r) const
inline

This method is provided for the purpose of comparing Rational values.

Returns
ture if the value is equal to r.

This method does not work if either of the Rational instances are not normalized. If the value is not normalized, you should call normalize before calling this method.

template<typename Int>
bool Celartem::Rational< Int >::operator> ( const Rational< Int > &  r) const
inline

This method is provided for the purpose of comparing Rational values.

Returns
ture if the value is greater than r.

This method does not work if either of the Rational instances are not normalized. If the value is not normalized, you should call normalize before calling this method.

template<typename Int>
bool Celartem::Rational< Int >::operator>= ( const Rational< Int > &  r) const
inline

This method is provided for the purpose of comparing Rational values.

Returns
ture if the value is no less than r.

This method does not work if either of the Rational instances are not normalized. If the value is not normalized, you should call normalize before calling this method.

template<typename Int>
Rational Celartem::Rational< Int >::reciprocal ( ) const
inline

This method calculates the reciprocal value.

Returns
The reciprocal value.
template<typename Int>
void Celartem::Rational< Int >::serialize ( Stream inStream,
size_t  inLevel,
Endian  inEndian 
) const
inline

This method is just a helper function to deal with SerializableData template. For more information, see SerializableData.

See Also
SerializableData, Serializable, DataStore

Member Data Documentation

template<typename Int>
Int Celartem::Rational< Int >::den
template<typename Int>
Int Celartem::Rational< Int >::num

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

Cuminas DjVu SDK 3.0.33103
This document is made with doxygen 1.8.5 at Sun Dec 15 2013 19:38:06.
Cuminas Logo