libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
Rational< T > Class Template Reference

Rational number with numerator and denominator. More...

#include <rational.h>

Public Member Functions

 Rational (T n=0, T d=1)
 Constructs a rational number.
 
bool isValid () const
 Returns true if the denominator is non-zero.
 
Rational operator+ (const Rational &rhs) const
 Adds two rationals.
 
Rational operator- (const Rational &rhs) const
 Subtracts two rationals.
 
Rational operator* (const Rational &rhs) const
 Multiplies two rationals.
 
Rational operator/ (const Rational &rhs) const
 Divides two rationals.
 
bool operator== (const Rational &rhs) const
 Equality comparison.
 
bool operator!= (const Rational &rhs) const
 Inequality comparison.
 
T numerator () const
 Returns the numerator.
 
T denominator () const
 Returns the denominator.
 
double toDouble () const
 Converts to double.
 
String toString () const
 Converts to a String in "num/den" format.
 
 operator String () const
 Implicit conversion to String.
 

Detailed Description

template<typename T = int>
class Rational< T >

Rational number with numerator and denominator.

Represents an exact fraction. Automatically simplifies on construction. A zero denominator produces an invalid Rational

Example
Rational<int> fps(24000, 1001); // 23.976 fps
double val = fps.toDouble(); // 23.976...
String s = fps.toString(); // "24000/1001"
auto sum = fps + half;
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
that can be detected with isValid().
Template Parameters
TUnderlying integer type (default: int).

Constructor & Destructor Documentation

◆ Rational()

template<typename T = int>
Rational< T >::Rational ( T  n = 0,
T  d = 1 
)
inline

Constructs a rational number.

If d is zero the Rational is marked invalid.

Parameters
nNumerator.
dDenominator.

Member Function Documentation

◆ operator*()

template<typename T = int>
Rational Rational< T >::operator* ( const Rational< T > &  rhs) const
inline

Multiplies two rationals.

Parameters
rhsRight-hand operand.
Returns
Product, or invalid Rational if either operand is invalid.

◆ operator+()

template<typename T = int>
Rational Rational< T >::operator+ ( const Rational< T > &  rhs) const
inline

Adds two rationals.

Parameters
rhsRight-hand operand.
Returns
Sum, or invalid Rational if either operand is invalid.

◆ operator-()

template<typename T = int>
Rational Rational< T >::operator- ( const Rational< T > &  rhs) const
inline

Subtracts two rationals.

Parameters
rhsRight-hand operand.
Returns
Difference, or invalid Rational if either operand is invalid.

◆ operator/()

template<typename T = int>
Rational Rational< T >::operator/ ( const Rational< T > &  rhs) const
inline

Divides two rationals.

Parameters
rhsRight-hand operand.
Returns
Quotient, or invalid Rational if rhs is zero or either is invalid.

◆ toDouble()

template<typename T = int>
double Rational< T >::toDouble ( ) const
inline

Converts to double.

Returns
The rational as a floating-point value, or 0.0 if invalid.

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