Theoretica
A C++ numerical and automatic mathematical library
theoretica::ratio< T1, T2 > Class Template Reference

representing a ratio between two objects, like a fraction or a rational polynomial. More...

Public Member Functions

 ratio ()
 Construct the object from the default constructors of the two types.
 
 ratio (const T1 &n, const T2 &d)
 Construct the object from two objects. More...
 
ratio operator* (const ratio &r) const
 Multiply two ratios.
 
ratio operator/ (const ratio &r) const
 Divide two ratios (without explicitly using division)
 
ratio operator+ (const ratio &r) const
 Add two ratios.
 
ratio operator- (const ratio &r) const
 Subtract two ratios.
 
ratio operator* (const T1 &a) const
 Multiply the ratio by a factor.
 
ratio operator/ (const T2 &b) const
 Divide the ratio by a factor.
 
template<typename T >
eval_as ()
 Evaluate the ratio as the division between numerator and denominator cast to the specified type. More...
 
T2 eval ()
 Evaluate the ratio as the division between numerator and denominator cast to the type of the denominator.
 
 operator T2 ()
 Evaluate the ratio as the division between numerator and denominator cast to the type of the denominator. More...
 
std::string to_string () const
 Convert the ratio to string representation.
 
 operator std::string ()
 Convert the ratio to string representation.
 

Public Attributes

T1 num
 The numerator.
 
T2 den
 The denominator.
 

Friends

std::ostream & operator<< (std::ostream &out, const ratio< T1, T2 > &obj)
 Stream the ratio in string representation to an output stream (std::ostream)
 

Detailed Description

template<typename T1, typename T2 = T1>
class theoretica::ratio< T1, T2 >

representing a ratio between two objects, like a fraction or a rational polynomial.

Note
For the class to properly work, the template types need to have operator*, operator+ and operator- defined.

Constructor & Destructor Documentation

◆ ratio()

template<typename T1 , typename T2 = T1>
theoretica::ratio< T1, T2 >::ratio ( const T1 &  n,
const T2 &  d 
)
inline

Construct the object from two objects.

Parameters
nThe numerator
dThe denominator

Member Function Documentation

◆ eval_as()

template<typename T1 , typename T2 = T1>
template<typename T >
T theoretica::ratio< T1, T2 >::eval_as ( )
inline

Evaluate the ratio as the division between numerator and denominator cast to the specified type.

This function is well-defined only if division between objects of type T is defined. A static cast is performed before division.

◆ operator T2()

template<typename T1 , typename T2 = T1>
theoretica::ratio< T1, T2 >::operator T2 ( )
inline

Evaluate the ratio as the division between numerator and denominator cast to the type of the denominator.

See also
eval

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