classPiiFraction
#include <PiiFraction.h>
A template class that represents fractions.
Description
Fractions are composed of a numerator and a denominator, the numerical value given by numerator/denominator. The template accepts any integer type.
Fractions are automatically reduced. This imposes a performance penalty, but prevents some overflows that would otherwise occur rather often.
Constructors and destructor
|
(
Create a new fraction with the given numerator and denominator. |
Public member functions
|
( )
Return the absolute value of this fraction. |
|
|
void
|
( )
Invert the number. |
|
( )
Return an invert version of the fraction. |
|
|
bool
|
( )
Return true if the denominator is different from zero. |
|
( )
Get a floating-point approximation of the fraction. |
|
|
template<class U>
|
Cast this fraction to another type. |
|
( )
Get an integer approximation of the fraction. |
|
|
|
|
|
(
|
|
|
void
|
(
|
|
void
|
|
|
|
|
|
(
|
|
|
void
|
(
|
|
void
|
|
|
|
|
|
( )
|
|
|
(
|
|
|
void
|
(
|
|
void
|
|
|
(
|
|
|
|
|
|
void
|
|
|
void
|
(
|
|
void
|
( )
Reduce the fraction by dividing both components by the greatest common divisor (gcd). |
|
( )
Create a reduced fraction by dividing both components by the gcd. |
Static public member functions
|
static PiiFraction
|
(
Create a fraction out of a floating-point number. |
Function details
-
PiiFraction
(- T num = 0
- T denom = 1
[inline]Create a new fraction with the given numerator and denominator.
The default denominator is one, which makes it possible to use a fraction just like an integer.
Parameters
- num
-
the numerator
- denom
-
the denominator
-
Return the absolute value of this fraction.
-
void invert
()[inline]Invert the number.
Exchanges numerator to denominator.
-
Return an invert version of the fraction.
Exchanges numerator to denominator.
-
bool isValid
()[inline]Return true if the denominator is different from zero.
-
operator double
()[inline]Get a floating-point approximation of the fraction.
If denominator is zero,
INFINITYwill be returned. -
template<class U>
operator PiiFraction< U >
()[inline]Cast this fraction to another type.
-
operator T
()[inline]Get an integer approximation of the fraction.
If the denominator is zero, 0 will be returned.
-
-
-
void operator*=
(- T value
[inline] -
-
-
-
void operator+=
(- T value
[inline] -
-
-
-
-
void operator-=
(- T value
[inline] -
-
-
-
-
void operator/=
(- T value
[inline] -
void reduce
()[inline]Reduce the fraction by dividing both components by the greatest common divisor (gcd).
-
Create a reduced fraction by dividing both components by the gcd.
-
static PiiFraction create
(- double number
- double accuracy = 1e-5
- unsigned int recursions = 20
[inline, static]Create a fraction out of a floating-point number.
This function uses a recursive formula that uses continued fractions to build an approximation for the floating-point number:
Parameters
- number
-
a floating point number
- accuracy
-
required accuracy for the approximation. This value does not determine the final accuracy of the estimation, but instead the accuracy of the lowest level in the continued fraction. The error will build up.
- recursions
Add a note
Not a single note added yet. Be the first, add yours.