Calculator
Step 6
|
Represent a rational number (fraction) as a numerator and denominator. More...
#include <rational.hpp>
Public Types | |
typedef T | value_type |
Convenience typedef for the integral type of the numerator and denominator. More... | |
Public Member Functions | |
rational (value_type num=0) | |
rational (value_type num, value_type den) | |
rational (double r) | |
template<class U > | |
rational (rational< U > const &that) | |
Copy from a different type of rational. More... | |
value_type | numerator () const |
Return the numerator. More... | |
value_type | denominator () const |
Return the denominator. More... | |
template<class U , class EnableIfFloat = typename std::enable_if<std::is_floating_point<U>::value>::type> | |
operator U () const | |
Convert the rational number to a floating-point type. More... | |
rational & | operator= (value_type) |
Assignment of an integer. More... | |
template<class U > | |
rational & | operator= (rational< U > const &rhs) |
Assignment of a rational with a different size. More... | |
rational & | operator+= (rational const &rhs) |
Addition assignment operator. More... | |
rational & | operator+= (value_type const &rhs) |
Addition assignment operator. More... | |
rational & | operator-= (rational const &rhs) |
Subtraction assignment operator. More... | |
rational & | operator-= (value_type const &rhs) |
Subtraction assignment operator. More... | |
rational & | operator*= (rational const &rhs) |
Multiplication assignment operator. More... | |
rational & | operator*= (value_type const &rhs) |
Multiplication assignment operator. More... | |
rational & | operator/= (rational const &rhs) |
Division assignment operator. More... | |
rational & | operator/= (value_type const &rhs) |
Division assignment operator. More... | |
rational & | operator++ () |
Pre-increment. More... | |
rational & | operator-- () |
Pre-decrement. More... | |
rational | operator++ (int) |
Post-increment. More... | |
rational | operator-- (int) |
Post-decrement. More... | |
template<class U > | |
T | scale (U value) |
template<class U > | |
rational< T > & | operator= (rational< U > const &rhs) |
Private Member Functions | |
void | reduce () |
Reduce the numerator and denominator by their GCD. More... | |
void | normalize () |
template<class U > | |
value_type | scale (U value) |
Private Attributes | |
value_type | numerator_ |
value_type | denominator_ |
Static Private Attributes | |
static constexpr value_type | double_divisor = power10<value_type>(std::numeric_limits<value_type>::digits10 - 1) |
Represent a rational number (fraction) as a numerator and denominator.
Definition at line 26 of file rational.hpp.
typedef T rational< T >::value_type |
Convenience typedef for the integral type of the numerator and denominator.
Definition at line 30 of file rational.hpp.
|
inline |
Default constructor and constructor from a single value. As a default constructor, initializes to zero. Otherwise, initializes to the integer num
.
num | The integer value to use as the initial value |
Definition at line 36 of file rational.hpp.
rational< T >::rational | ( | value_type | num, |
value_type | den | ||
) |
Construct a rational number
num | numerator |
den | denominator |
zero_denominator | if den == 0 |
Definition at line 113 of file rational.hpp.
Initialize the rational number with an approximation of r
r | the initial value |
Definition at line 121 of file rational.hpp.
References rational< T >::double_divisor.
Copy from a different type of rational.
Definition at line 127 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, rational< T >::numerator(), and rational< T >::reduce().
|
inline |
Return the denominator.
Definition at line 52 of file rational.hpp.
Referenced by absval(), number_rational::do_divide(), rational< long long >::operator U(), rational< T >::operator*=(), rational< T >::operator++(), rational< T >::operator+=(), operator-(), rational< T >::operator--(), rational< T >::operator-=(), operator/(), rational< T >::operator/=(), rational< T >::operator=(), operator==(), rational< T >::rational(), and rational< T >::reduce().
|
private |
Reduce the numerator and denominator, and normalize the signs of both, that is, ensure denominator is not negative.
Definition at line 144 of file rational.hpp.
References rational< T >::denominator_, rational< T >::numerator_, and rational< T >::reduce().
Referenced by rational< T >::operator/=().
|
inline |
Return the numerator.
Definition at line 50 of file rational.hpp.
Referenced by absval(), number_rational::do_divide(), rational< long long >::operator U(), rational< T >::operator*=(), rational< T >::operator+=(), operator-(), rational< T >::operator-=(), operator/(), rational< T >::operator/=(), operator<(), operator<<(), rational< T >::operator=(), operator==(), rational< T >::rational(), and rational< T >::reduce().
|
inlineexplicit |
Convert the rational number to a floating-point type.
Definition at line 55 of file rational.hpp.
Multiplication assignment operator.
Definition at line 217 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, rational< T >::numerator(), rational< T >::numerator_, and rational< T >::reduce().
rational< T > & rational< T >::operator*= | ( | value_type const & | rhs | ) |
Multiplication assignment operator.
Definition at line 226 of file rational.hpp.
References rational< T >::numerator_, and rational< T >::reduce().
Pre-increment.
Definition at line 255 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator_.
Post-increment.
Definition at line 262 of file rational.hpp.
Addition assignment operator.
Definition at line 183 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, rational< T >::numerator(), rational< T >::numerator_, and rational< T >::reduce().
rational< T > & rational< T >::operator+= | ( | value_type const & | rhs | ) |
Addition assignment operator.
Definition at line 192 of file rational.hpp.
References rational< T >::denominator(), rational< T >::numerator(), rational< T >::numerator_, and rational< T >::reduce().
Pre-decrement.
Definition at line 270 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator_.
Post-decrement.
Definition at line 277 of file rational.hpp.
Subtraction assignment operator.
Definition at line 200 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, rational< T >::numerator(), rational< T >::numerator_, and rational< T >::reduce().
rational< T > & rational< T >::operator-= | ( | value_type const & | rhs | ) |
Subtraction assignment operator.
Definition at line 209 of file rational.hpp.
References rational< T >::denominator(), rational< T >::numerator(), rational< T >::numerator_, and rational< T >::reduce().
Division assignment operator.
Definition at line 234 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, rational< T >::normalize(), rational< T >::numerator(), and rational< T >::numerator_.
rational< T > & rational< T >::operator/= | ( | value_type const & | rhs | ) |
Division assignment operator.
Definition at line 245 of file rational.hpp.
References rational< T >::denominator_, and rational< T >::normalize().
rational< T > & rational< T >::operator= | ( | value_type | num | ) |
Assignment of an integer.
Definition at line 165 of file rational.hpp.
References rational< T >::denominator_, and rational< T >::numerator_.
rational& rational< T >::operator= | ( | rational< U > const & | rhs | ) |
Assignment of a rational with a different size.
rational<T>& rational< T >::operator= | ( | rational< U > const & | rhs | ) |
Definition at line 174 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, rational< T >::numerator(), rational< T >::numerator_, and rational< T >::reduce().
|
private |
Reduce the numerator and denominator by their GCD.
Definition at line 155 of file rational.hpp.
References rational< T >::denominator(), rational< T >::denominator_, gcd(), rational< T >::numerator(), and rational< T >::numerator_.
Referenced by rational< T >::normalize(), rational< T >::operator*=(), rational< T >::operator+=(), rational< T >::operator-=(), rational< T >::operator=(), and rational< T >::rational().
Scale an integer of type U
to the value_type. If U
has more digits than value_type
shift value
to the right.
Definition at line 135 of file rational.hpp.
|
private |
Definition at line 109 of file rational.hpp.
Referenced by rational< long long >::denominator(), rational< T >::normalize(), rational< T >::operator*=(), rational< T >::operator+=(), rational< T >::operator-=(), rational< T >::operator/=(), rational< T >::operator=(), rational< T >::rational(), and rational< T >::reduce().
|
staticprivate |
Compute the divisor to use when converting a double
to rational
. This is the largest power of 10 that value_type
can represent.
Definition at line 106 of file rational.hpp.
Referenced by rational< T >::rational().
|
private |
Definition at line 108 of file rational.hpp.
Referenced by rational< T >::normalize(), rational< long long >::numerator(), rational< T >::operator*=(), rational< T >::operator++(), rational< T >::operator+=(), rational< T >::operator--(), rational< T >::operator-=(), rational< T >::operator/=(), rational< T >::operator=(), and rational< T >::reduce().