Calculator Step 6

- Main Page
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
rational< T > Class Template Reference

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)

## Detailed Description

### template<class T>
class rational< T >

Represent a rational number (fraction) as a numerator and denominator.

Definition at line 26 of file rational.hpp.

## Member Typedef Documentation

template<class T>

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.

## Constructor & Destructor Documentation

template<class T>

rational< T >::rational | ( | value_type | num = `0` | ) |
| inline

Default constructor and constructor from a single value. As a default constructor, initializes to zero. Otherwise, initializes to the integer `num`.

Parameters

  num | The integer value to use as the initial value

Definition at line 36 of file rational.hpp.

36 : numerator_{num}, denominator_{1} {}
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
template<class T >

rational< T >::rational | ( | value_type | num,
|  | value_type | den
| ) |  |

Construct a rational number

Parameters

  num | numerator
  den | denominator

Exceptions

  zero_denominator | if `den` == 0

Definition at line 113 of file rational.hpp.

114 : numerator_{num},
115 denominator_{den == value_type{} ? throw zero_denominator{"zero denominator"} : den}
116 {
117 normalize();
118 }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::normalize
void normalize()
Definition: rational.hpp:144
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::value_type
T value_type
Convenience typedef for the integral type of the numerator and denominator.
Definition: rational.hpp:30
zero_denominator
Exception class if the denominator is ever zero.
Definition: rational.hpp:17
template<class T >

rational< T >::rational | ( | double | r | ) |

Initialize the rational number with an approximation of `r`

Parameters

  r | the initial value

Definition at line 121 of file rational.hpp.

References rational< T >::double_divisor.

122 : numerator_{static_cast<T>(r / double_divisor + 0.5)}, denominator_{double_divisor}
123 {}
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::double_divisor
static constexpr value_type double_divisor
Definition: rational.hpp:106
template<class T >
template<class U >

rational< T >::rational | ( | rational< U > const & | that | ) |

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().

128 : numerator_{scale<U>(that.numerator())}, denominator_{scale<U>(that.denominator())}
129 {
130 reduce();
131 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155

## Member Function Documentation

template<class T>

value_type rational< T >::denominator | ( |  | ) | const
| 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().

52 { return denominator_; }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
template<class T >

void rational< T >::normalize | ( |  | ) |
| 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/=().

145 {
146 if (denominator_ < value_type{})
147 {
148 denominator_ = -denominator_;
149 numerator_ = -numerator_;
150 }
151 reduce();
152 }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::value_type
T value_type
Convenience typedef for the integral type of the numerator and denominator.
Definition: rational.hpp:30
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T>

value_type rational< T >::numerator | ( |  | ) | const
| 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().

50 { return numerator_; }
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
template<class T>
template<class U , class EnableIfFloat = typename std::enable_if<std::is_floating_point<U>::value>::type>

rational< T >::operator U | ( |  | ) | const
| inline explicit

Convert the rational number to a floating-point type.

Definition at line 55 of file rational.hpp.

55 { return static_cast<U>(numerator()) / denominator(); }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
template<class T >

rational< T > & rational< T >::operator*= | ( | rational< T > const & | rhs | ) |

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().

218 {
219 numerator_ *= rhs.numerator();
220 denominator_ *= rhs.denominator();
221 reduce();
222 return *this;
223 }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

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().

227 {
228 numerator_ *= rhs;
229 reduce();
230 return *this;
231 }
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

rational< T > & rational< T >::operator++ | ( |  | ) |

Pre-increment.

Definition at line 255 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator_.

256 {
257 numerator_ += denominator();
258 return *this;
259 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
template<class T >

rational< T > rational< T >::operator++ | ( | int |  | ) |

Post-increment.

Definition at line 262 of file rational.hpp.

263 {
264 rational result(*this);
265 ++*this;
266 return result;
267 }
rational
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
template<class T >

rational< T > & rational< T >::operator+= | ( | rational< T > const & | rhs | ) |

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().

184 {
185 numerator_ = numerator() * rhs.denominator() + rhs.numerator() * denominator();
186 denominator_ *= rhs.denominator();
187 reduce();
188 return *this;
189 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

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().

193 {
194 numerator_ = numerator() + rhs * denominator();
195 reduce();
196 return *this;
197 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

rational< T > & rational< T >::operator-- | ( |  | ) |

Pre-decrement.

Definition at line 270 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator_.

271 {
272 numerator_ -= denominator();
273 return *this;
274 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
template<class T >

rational< T > rational< T >::operator-- | ( | int |  | ) |

Post-decrement.

Definition at line 277 of file rational.hpp.

278 {
279 rational result(*this);
280 --*this;
281 return result;
282 }
rational
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
template<class T >

rational< T > & rational< T >::operator-= | ( | rational< T > const & | rhs | ) |

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().

201 {
202 numerator_ = numerator() * rhs.denominator() - rhs.numerator() * denominator();
203 denominator_ *= rhs.denominator();
204 reduce();
205 return *this;
206 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

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().

210 {
211 numerator_ = numerator() - rhs * denominator();
212 reduce();
213 return *this;
214 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

rational< T > & rational< T >::operator/= | ( | rational< T > const & | rhs | ) |

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_.

235 {
236 if (rhs.numerator() == value_type{})
237 throw zero_denominator{"divide by zero"};
238 numerator_ *= rhs.denominator();
239 denominator_ *= rhs.numerator();
240 normalize();
241 return *this;
242 }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::normalize
void normalize()
Definition: rational.hpp:144
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::value_type
T value_type
Convenience typedef for the integral type of the numerator and denominator.
Definition: rational.hpp:30
zero_denominator
Exception class if the denominator is ever zero.
Definition: rational.hpp:17
template<class T >

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().

246 {
247 if (rhs == value_type{})
248 throw zero_denominator{"divide by zero"};
249 denominator_ *= rhs;
250 normalize();
251 return *this;
252 }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::normalize
void normalize()
Definition: rational.hpp:144
rational::value_type
T value_type
Convenience typedef for the integral type of the numerator and denominator.
Definition: rational.hpp:30
zero_denominator
Exception class if the denominator is ever zero.
Definition: rational.hpp:17
template<class T >

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_.

166 {
167 numerator_ = num;
168 denominator_ = value_type{1};
169 return *this;
170 }
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::value_type
T value_type
Convenience typedef for the integral type of the numerator and denominator.
Definition: rational.hpp:30
template<class T>
template<class U >

rational& rational< T >::operator= | ( | rational< U > const & | rhs | ) |

Assignment of a rational with a different size.

template<class T>
template<class U >

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().

175 {
176 numerator_ = scale<U>(rhs.numerator());
177 denominator_ = scale<U>(rhs.denominator());
178 reduce();
179 return *this;
180 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::reduce
void reduce()
Reduce the numerator and denominator by their GCD.
Definition: rational.hpp:155
template<class T >

void 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().

156 {
157 value_type div{gcd(numerator(), denominator())};
158 if (div == value_type{})
159 throw zero_denominator{"zero denominator"};
160 numerator_ /= div;
161 denominator_ /= div;
162 }
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational::denominator_
value_type denominator_
Definition: rational.hpp:109
gcd
T gcd(T n, T m)
Definition: gcd.hpp:8
rational::numerator_
value_type numerator_
Definition: rational.hpp:108
rational::value_type
T value_type
Convenience typedef for the integral type of the numerator and denominator.
Definition: rational.hpp:30
zero_denominator
Exception class if the denominator is ever zero.
Definition: rational.hpp:17
template<class T>
template<class U >

value_type rational< T >::scale | ( | U | value | ) |
| private

Scale an integer of type `U` to the value_type. If `U` has more digits than `value_type` shift `value` to the right.

template<class T>
template<class U >

T rational< T >::scale | ( | U | value | ) |

Definition at line 135 of file rational.hpp.

136 {
137 if (std::numeric_limits<T>::digits >= std::numeric_limits<U>::digits)
138 return T{value};
139 else
140 return T{value >> (std::numeric_limits<U>::digits - std::numeric_limits<T>::digits)};
141 }

## Member Data Documentation

template<class T>

value_type rational< T >::denominator_
| 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().

template<class T>

constexpr value_type rational< T >::double_divisor = power10<value_type>(std::numeric_limits<value_type>::digits10 - 1)
| static private

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().

template<class T>

value_type rational< T >::numerator_
| 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().

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

- rational.hpp