TSDuck v3.40-3963
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::Fraction< INT_T, type > Class Template Reference

Template representation of fractional numbers. More...

#include <tsFraction.h>

Inheritance diagram for ts::Fraction< INT_T, type >:
Collaboration diagram for ts::Fraction< INT_T, type >:

Public Types

using int_t = INT_T
 The underlying integer type.
 

Public Member Functions

 Fraction ()=default
 Default constructor, implicitly initialized to zero.
 
template<typename INT1 , typename std::enable_if< std::is_integral< INT1 >::value, int >::type = 0>
 Fraction (INT1 numerator)
 Constructor from an integer value.
 
template<typename INT1 , typename INT2 , typename std::enable_if< std::is_integral< INT1 >::value &&std::is_integral< INT2 >::value, int >::type = 0>
 Fraction (INT1 numerator, INT2 denominator)
 Constructor from integer values.
 
Fraction abs () const
 Get the absolute value.
 
int_t denominator () const
 Get the denominator part of the fraction.
 
virtual ts::UString description () const override
 Get a textual description of the values of that type.
 
bool divOverflow (const Fraction &x) const
 Check if this Fraction generates an overflow when divided by another Fraction.
 
virtual bool fromString (const UString &str, UChar separator=COMMA, UChar decimal_dot=FULL_STOP) override
 Parse a string and interpret it as a number.
 
virtual bool inRange (int64_t min, int64_t max) const override
 Check if the value of the number is within a range of integer value.
 
Fraction max (const Fraction &x) const
 Get the maximum value of two fractions.
 
Fraction min (const Fraction &x) const
 Get the minimum value of two fractions.
 
bool mulOverflow (const Fraction &x) const
 Check if this Fraction generates an overflow when multiplied by another Fraction.
 
template<typename INT2 , typename std::enable_if< std::is_integral< INT2 >::value, int >::type = 0>
bool mulOverflow (INT2 x) const
 Check if this Fraction generates an overflow when multiplied by an integer.
 
int_t numerator () const
 Get the numerator part of the fraction.
 
int_t proper ()
 Converts to a proper fraction (a fraction that is less than 1).
 
virtual double toDouble () const override
 Conversion to double value.
 
int_t toInt () const
 Conversion to integral value.
 
virtual int64_t toInt64 () const override
 Conversion to a 64-bit signed integer value.
 
virtual UString toString (size_t min_width=0, bool right_justified=true, UChar separator=COMMA, bool force_sign=false, size_t decimals=NPOS, bool force_decimals=false, UChar decimal_dot=FULL_STOP, UChar pad=SPACE) const override
 Convert the number to a string object.
 

Static Public Attributes

static const AbstractNumber *const INVALID
 Pointer to an invalid instance of AbstractNumber.
 
static const Fraction MAX
 The maximum representable value of this fraction type.
 
static const Fraction MIN
 The minimum representable value of this fraction type.
 

Static Protected Member Functions

static void Deformat (UString &str, UChar separator, UChar decimal_dot)
 Static common utility to deformat a string containing a number.
 
static void Format (UString &str, size_t min_width, bool right_justified, UChar separator, bool add_plus_sign, size_t decimals, bool force_decimals, UChar decimal_dot, UChar pad)
 Static common utility to format a string containing a number with or without decimal part.
 

Detailed Description

template<typename INT_T, typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
class ts::Fraction< INT_T, type >

Template representation of fractional numbers.

Fraction are always reduced so that the numerator and denominator have no common divisor other than 1. For signed integer types, the signed is carried by the numerator and the denominator is always positive.

All arithmetic and comparison operators are defined between fraction values and between a fraction value and an integer value, both directions.

In debug mode (when the macro DEBUG is defined), the arithmetic operators throw an exception in case of overflow. Without debug mode, the arithmetic overflow are ignored, which may lead to inconsistent values after a large number of operations (typically additions and substractions with distinct denominators).

Template Parameters
INT_TThe integer type for numerator and denominator.

Constructor & Destructor Documentation

◆ Fraction() [1/2]

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
template<typename INT1 , typename std::enable_if< std::is_integral< INT1 >::value, int >::type = 0>
ts::Fraction< INT_T, type >::Fraction ( INT1  numerator)

Constructor from an integer value.

Template Parameters
INT1Some other integer type (signed or unsigned).
Parameters
[in]numeratorInitial numerator value.
Exceptions
std::overflow_errorWhen numerator is out of range.

◆ Fraction() [2/2]

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
template<typename INT1 , typename INT2 , typename std::enable_if< std::is_integral< INT1 >::value &&std::is_integral< INT2 >::value, int >::type = 0>
ts::Fraction< INT_T, type >::Fraction ( INT1  numerator,
INT2  denominator 
)

Constructor from integer values.

Template Parameters
INT1Some other integer type (signed or unsigned).
INT2Some other integer type (signed or unsigned).
Parameters
[in]numeratorInitial numerator value.
[in]denominatorInitial denominator value.
Exceptions
std::underflow_errorWhen denominator is zero.
std::overflow_errorWhen numerator or denominator are out of range.

Member Function Documentation

◆ description()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
virtual ts::UString ts::Fraction< INT_T, type >::description ( ) const
overridevirtual

Get a textual description of the values of that type.

This is typically used in help texts.

Returns
A textual description of the values of that type.

Implements ts::AbstractNumber.

◆ inRange()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
virtual bool ts::Fraction< INT_T, type >::inRange ( int64_t  min,
int64_t  max 
) const
overridevirtual

Check if the value of the number is within a range of integer value.

Parameters
[in]minMinimum integer value (inclusive).
[in]maxMaximum integer value (inclusive).
Returns
True if this number is within the range min to max, inclusive.

Implements ts::AbstractNumber.

◆ toInt64()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
virtual int64_t ts::Fraction< INT_T, type >::toInt64 ( ) const
overridevirtual

Conversion to a 64-bit signed integer value.

Returns
The value in integral units. Underflow or overflow rounding is applied when necessary.

Implements ts::AbstractNumber.

◆ toDouble()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
virtual double ts::Fraction< INT_T, type >::toDouble ( ) const
overridevirtual

Conversion to double value.

Returns
The value as a double.

Implements ts::AbstractNumber.

◆ fromString()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
virtual bool ts::Fraction< INT_T, type >::fromString ( const UString str,
UChar  separator = COMMA,
UChar  decimal_dot = FULL_STOP 
)
overridevirtual

Parse a string and interpret it as a number.

The content of this object is updated from the parsed string.

Parameters
[in]strA string to parse, representing this object.
[in]separatorSeparator character for groups of thousands, a comma by default.
[in]decimal_dotThe decimal separator, a dot by default.
Returns
True if the str is valid, false otherwise. In case of parsing error, the content of this object is undefined.

Implements ts::AbstractNumber.

◆ toString()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
virtual UString ts::Fraction< INT_T, type >::toString ( size_t  min_width = 0,
bool  right_justified = true,
UChar  separator = COMMA,
bool  force_sign = false,
size_t  decimals = NPOS,
bool  force_decimals = false,
UChar  decimal_dot = FULL_STOP,
UChar  pad = SPACE 
) const
overridevirtual

Convert the number to a string object.

Parameters
[in]min_widthMinimum width of the returned string. Padded with pad characters if larger than the number of characters in the formatted number.
[in]right_justifiedIf true (the default), return a right-justified string. When false, return a left-justified string. Ignored if min_width is lower than the number of characters in the formatted number.
[in]separatorSeparator character for groups of thousands, a comma by default. CHAR_NULL means no separator.
[in]force_signIf true, force a '+' sign for positive values.
[in]decimalsPrecision to use after the decimal point. NPOS means default.
[in]force_decimalsIf true, with positive precision, force a decimal dot and the number of decimal digits of the precision. By default, skip non significant decimal digits.
[in]decimal_dotThe decimal separator, a dot by default.
[in]padThe padding character to adjust the width.
Returns
The formatted string.

Implements ts::AbstractNumber.

◆ numerator()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
int_t ts::Fraction< INT_T, type >::numerator ( ) const
inline

Get the numerator part of the fraction.

Returns
The numerator part.

◆ denominator()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
int_t ts::Fraction< INT_T, type >::denominator ( ) const
inline

Get the denominator part of the fraction.

Returns
The denominator part.

◆ toInt()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
int_t ts::Fraction< INT_T, type >::toInt ( ) const
inline

Conversion to integral value.

Returns
The value in integral units. Underflow or overflow rounding is applied when necessary.

◆ abs()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
Fraction ts::Fraction< INT_T, type >::abs ( ) const
inline

Get the absolute value.

Returns
The absolute value of this fraction number.

◆ proper()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
int_t ts::Fraction< INT_T, type >::proper ( )

Converts to a proper fraction (a fraction that is less than 1).

The previous integer part is returned. This means that:

using Frac = Fraction<...>;
Frac f(..);
Frac old(f);
Frac::int_t i = f.proper();
// then old == i + f, with f.abs() < 1
Template representation of fractional numbers.
Definition tsFraction.h:39
Returns
The previous integer part of the fraction.

◆ max()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
Fraction ts::Fraction< INT_T, type >::max ( const Fraction< INT_T, type > &  x) const
inline

Get the maximum value of two fractions.

Parameters
[in]xAnother fraction.
Returns
The maximum value of this fraction and x.

◆ min()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
Fraction ts::Fraction< INT_T, type >::min ( const Fraction< INT_T, type > &  x) const
inline

Get the minimum value of two fractions.

Parameters
[in]xAnother fraction.
Returns
The minimum value of this fraction and x.

◆ mulOverflow() [1/2]

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
template<typename INT2 , typename std::enable_if< std::is_integral< INT2 >::value, int >::type = 0>
bool ts::Fraction< INT_T, type >::mulOverflow ( INT2  x) const

Check if this Fraction generates an overflow when multiplied by an integer.

Template Parameters
INT2Another integer type.
Parameters
[in]xAn integer of type INT2.
Returns
True if this Fraction generates an overflow when multiplied by x.

◆ mulOverflow() [2/2]

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
bool ts::Fraction< INT_T, type >::mulOverflow ( const Fraction< INT_T, type > &  x) const

Check if this Fraction generates an overflow when multiplied by another Fraction.

Parameters
[in]xAnother Fraction.
Returns
True if this Fraction generates an overflow when multiplied by x.

◆ divOverflow()

template<typename INT_T , typename std::enable_if< std::is_integral< INT_T >::value, int >::type = 0>
bool ts::Fraction< INT_T, type >::divOverflow ( const Fraction< INT_T, type > &  x) const

Check if this Fraction generates an overflow when divided by another Fraction.

Parameters
[in]xAnother Fraction.
Returns
True if this Fraction generates an overflow when divided by x.

◆ Format()

static void ts::AbstractNumber::Format ( UString str,
size_t  min_width,
bool  right_justified,
UChar  separator,
bool  add_plus_sign,
size_t  decimals,
bool  force_decimals,
UChar  decimal_dot,
UChar  pad 
)
staticprotectedinherited

Static common utility to format a string containing a number with or without decimal part.

Parameters
[in,out]strString to format.
[in]min_widthMinimum width of the string.
[in]right_justifiedIf true (the default), return a right-justified string.
[in]separatorSeparator character for groups of thousands.
[in]add_plus_signIf true, force a '+' sign before the number.
[in]decimalsPrecision to use after the decimal point. NPOS not allowed.
[in]force_decimalsIf true, force a decimal dot and the number of decimal digits. For integer types, use force_decimals == true and decimals == 0.
[in]decimal_dotThe decimal separator.
[in]padThe padding character to adjust the width.

◆ Deformat()

static void ts::AbstractNumber::Deformat ( UString str,
UChar  separator,
UChar  decimal_dot 
)
staticprotectedinherited

Static common utility to deformat a string containing a number.

Parameters
[in,out]strString to deformat.
[in]separatorSeparator character for groups of thousands.
[in]decimal_dotThe decimal separator.

Member Data Documentation

◆ INVALID

const AbstractNumber* const ts::AbstractNumber::INVALID
staticinherited

Pointer to an invalid instance of AbstractNumber.

Its toString() method always return an empty string.


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