Class to simplify calculations of finite rationals at runtime. More...

#include <Rational.h>

Inheritance diagram for Ratio< T >:
Collaboration diagram for Ratio< T >:

Public Member Functions

 Ratio ()=default
 
 Ratio (const BasicRatio< T > &r)
 
 Ratio (T num, T den=1)
 
template<T num, T den>
void set ()
 Method template to set ratio and minimise. More...
 
void set (T num, T den=1)
 Set ratio and minimise. More...
 
template<typename ValueType >
 operator ValueType () const
 
- Public Member Functions inherited from BasicRatio< T >
 operator String () const
 

Friends

Ratio operator* (const Ratio &lhs, const Ratio &rhs)
 Evaluate ratio = ratio1 * ratio2 and minimise. More...
 
template<typename ValueType >
ValueType operator* (ValueType lhs, const Ratio &rhs)
 Evaluate value = value * ratio More...
 
template<typename ValueType >
ValueType operator* (const Ratio &lhs, const ValueType &rhs)
 Evaluate value = ratio * value More...
 
Ratio operator/ (const Ratio &lhs, const Ratio &rhs)
 Evaluate ratio = ratio1 / ratio2 and minimise. More...
 
template<typename ValueType >
Ratio operator/ (Ratio lhs, const ValueType &rhs)
 Evaluate value = Ratio / value More...
 
template<typename ValueType >
ValueType operator/ (ValueType lhs, const Ratio &rhs)
 Evaluate value = value / ratio More...
 

Additional Inherited Members

- Public Attributes inherited from BasicRatio< T >
num
 
den
 

Detailed Description

template<typename T>
struct Ratio< T >

Class to simplify calculations of finite rationals at runtime.

Note
Operations are restricted to multiplication and division using unsigned integer operations. Multiplication and division are rounded to the nearest whole number. Overflows are handled by returning the maximum value rather than truncating.
Addition and subtraction are not supported directly as this would require overflow detection and probably signed arithmetic, which is slower and limits calculation range.
See also
See muldiv for further details.
Note
Ratio values are minimised in constructor

Constructor & Destructor Documentation

◆ Ratio() [1/3]

template<typename T >
Ratio< T >::Ratio ( )
default

◆ Ratio() [2/3]

template<typename T >
Ratio< T >::Ratio ( const BasicRatio< T > &  r)
inline

◆ Ratio() [3/3]

template<typename T >
Ratio< T >::Ratio ( num,
den = 1 
)
inlineexplicit

Member Function Documentation

◆ operator ValueType()

template<typename T >
template<typename ValueType >
Ratio< T >::operator ValueType ( ) const
inlineexplicit

◆ set() [1/2]

template<typename T >
template<T num, T den>
void Ratio< T >::set ( )
inline

Method template to set ratio and minimise.

Note
Use this method where values are known at compile-time

◆ set() [2/2]

template<typename T >
void Ratio< T >::set ( num,
den = 1 
)
inline

Set ratio and minimise.

Note
Use this method for variable ratios

Friends And Related Function Documentation

◆ operator* [1/3]

template<typename T >
Ratio operator* ( const Ratio< T > &  lhs,
const Ratio< T > &  rhs 
)
friend

Evaluate ratio = ratio1 * ratio2 and minimise.

◆ operator* [2/3]

template<typename T >
template<typename ValueType >
ValueType operator* ( const Ratio< T > &  lhs,
const ValueType &  rhs 
)
friend

Evaluate value = ratio * value

◆ operator* [3/3]

template<typename T >
template<typename ValueType >
ValueType operator* ( ValueType  lhs,
const Ratio< T > &  rhs 
)
friend

Evaluate value = value * ratio

◆ operator/ [1/3]

template<typename T >
Ratio operator/ ( const Ratio< T > &  lhs,
const Ratio< T > &  rhs 
)
friend

Evaluate ratio = ratio1 / ratio2 and minimise.

◆ operator/ [2/3]

template<typename T >
template<typename ValueType >
Ratio operator/ ( Ratio< T >  lhs,
const ValueType &  rhs 
)
friend

Evaluate value = Ratio / value

◆ operator/ [3/3]

template<typename T >
template<typename ValueType >
ValueType operator/ ( ValueType  lhs,
const Ratio< T > &  rhs 
)
friend

Evaluate value = value / ratio


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