Into

Modules

Documentation

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

(
  • T num = 0
  • T denom = 1
)

Create a new fraction with the given numerator and denominator.

Public member functions

abs
( )

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.

( )
(
  • T value
)
void
(
  • T value
)
void
( )
( )
(
  • T value
)
void
(
  • T value
)
void
( )
( )
( )
(
  • T value
)
void
(
  • T value
)
void
( )
(
  • T value
)
( )
void
( )
void
(
  • T value
)
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
(
  • double number
  • double accuracy = 1e-5
  • unsigned int recursions = 20
)

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

  • PiiFraction abs

    ()
    [inline]

    Return the absolute value of this fraction.

  • void invert

    ()
    [inline]

    Invert the number.

    Exchanges numerator to denominator.

  • PiiFraction inverted

    ()
    [inline]

    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, INFINITY will 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.

  • PiiFraction operator*

    ( )
    [inline]
  • PiiFraction operator*

    (
    • T value
    )
    [inline]
  • void operator*=

    (
    • T value
    )
    [inline]
  • void operator*=

    ( )
    [inline]
  • PiiFraction operator+

    ( )
    [inline]
  • PiiFraction operator+

    (
    • T value
    )
    [inline]
  • void operator+=

    (
    • T value
    )
    [inline]
  • void operator+=

    ( )
    [inline]
  • PiiFraction operator-

    ( )
    [inline]
  • PiiFraction operator-

    ()
    [inline]
  • PiiFraction operator-

    (
    • T value
    )
    [inline]
  • void operator-=

    (
    • T value
    )
    [inline]
  • void operator-=

    ( )
    [inline]
  • PiiFraction operator/

    (
    • T value
    )
    [inline]
  • PiiFraction operator/

    ( )
    [inline]
  • void operator/=

    ( )
    [inline]
  • void operator/=

    (
    • T value
    )
    [inline]
  • void reduce

    ()
    [inline]

    Reduce the fraction by dividing both components by the greatest common divisor (gcd).

  • PiiFraction reduced

    ()
    [inline]

    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
Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.