TSDuck v3.40-4033
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::FloatingPoint< FLOAT_T, PREC > Class Template Reference

Encapsulation of a double floating-point as an AbstractNumber. More...

#include <tsFloatingPoint.h>

Inheritance diagram for ts::FloatingPoint< FLOAT_T, PREC >:
Collaboration diagram for ts::FloatingPoint< FLOAT_T, PREC >:

Public Types

using float_t = FLOAT_T
 The underlying floating-point type.
 
using int_t = int64_t
 An integer type for conversion to integral values.
 

Public Member Functions

 FloatingPoint ()=default
 Default constructor, implicitly initialized to zero.
 
template<typename NUM_T >
requires std::is_arithmetic_v<NUM_T>
 FloatingPoint (NUM_T x)
 Constructor from an integer or floating-point value.
 
FloatingPoint abs () const
 Get the absolute value.
 
virtual ts::UString description () const override
 Get a textual description of the values of that type.
 
bool divOverflow (const FloatingPoint &x) const
 Check if this FloatingPoint number generates an overflow when divided by another FloatingPoint.
 
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.
 
FloatingPoint max (const FloatingPoint &x) const
 Get the maximum value of two fractions.
 
FloatingPoint min (const FloatingPoint &x) const
 Get the minimum value of two fractions.
 
bool mulOverflow (const FloatingPoint &x) const
 Check if this FloatingPoint number generates an overflow when multiplied by another FloatingPoint.
 
template<typename INT2 >
requires std::integral<INT2>
bool mulOverflow (INT2 x) const
 Check if this FloatingPoint number generates an overflow when multiplied by an integer.
 
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 constexpr size_t DISPLAY_PRECISION = PREC
 The displayed precision of the floating-point type (number of decimal digits).
 
static constexpr float_t EQUAL_PRECISION = 100.0 * std::numeric_limits<float_t>::min()
 Precision of "equal" comparisons.
 
static const AbstractNumber *const INVALID
 Pointer to an invalid instance of AbstractNumber.
 
static const FloatingPoint MAX
 The maximum representable value of this fraction type.
 
static const FloatingPoint 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 FLOAT_T, const size_t PREC = 6>
requires std::floating_point<FLOAT_T>
class ts::FloatingPoint< FLOAT_T, PREC >

Encapsulation of a double floating-point as an AbstractNumber.

Template Parameters
FLOAT_TThe underlying floating-point type.
PRECThe number of decimal digits to display by default. This is a display attribute only, it does not alter the binary representation of floating-point values. The default is to display 6 digits.

Member Typedef Documentation

◆ int_t

template<typename FLOAT_T , const size_t PREC = 6>
using ts::FloatingPoint< FLOAT_T, PREC >::int_t = int64_t

An integer type for conversion to integral values.

Overflow may occur.

Constructor & Destructor Documentation

◆ FloatingPoint()

template<typename FLOAT_T , const size_t PREC = 6>
template<typename NUM_T >
requires std::is_arithmetic_v<NUM_T>
ts::FloatingPoint< FLOAT_T, PREC >::FloatingPoint ( NUM_T  x)
inline

Constructor from an integer or floating-point value.

Template Parameters
NUM_TSome integer or floating-point type.
Parameters
[in]xInitial value.

Member Function Documentation

◆ description()

template<typename FLOAT_T , const size_t PREC>
requires std::floating_point<FLOAT_T>
ts::UString ts::FloatingPoint< FLOAT_T, PREC >::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 FLOAT_T , const size_t PREC>
requires std::floating_point<FLOAT_T>
bool ts::FloatingPoint< FLOAT_T, PREC >::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 FLOAT_T , const size_t PREC>
requires std::floating_point<FLOAT_T>
int64_t ts::FloatingPoint< FLOAT_T, PREC >::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 FLOAT_T , const size_t PREC>
requires std::floating_point<FLOAT_T>
double ts::FloatingPoint< FLOAT_T, PREC >::toDouble ( ) const
overridevirtual

Conversion to double value.

Returns
The value as a double.

Implements ts::AbstractNumber.

◆ fromString()

template<typename FLOAT_T , const size_t PREC>
requires std::floating_point<FLOAT_T>
bool ts::FloatingPoint< FLOAT_T, PREC >::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 FLOAT_T , const size_t PREC>
requires std::floating_point<FLOAT_T>
ts::UString ts::FloatingPoint< FLOAT_T, PREC >::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.

◆ toInt()

template<typename FLOAT_T , const size_t PREC = 6>
int_t ts::FloatingPoint< FLOAT_T, PREC >::toInt ( ) const
inline

Conversion to integral value.

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

◆ abs()

template<typename FLOAT_T , const size_t PREC = 6>
FloatingPoint ts::FloatingPoint< FLOAT_T, PREC >::abs ( ) const
inline

Get the absolute value.

Returns
The absolute value of this fraction number.

◆ max()

template<typename FLOAT_T , const size_t PREC = 6>
FloatingPoint ts::FloatingPoint< FLOAT_T, PREC >::max ( const FloatingPoint< FLOAT_T, PREC > &  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 FLOAT_T , const size_t PREC = 6>
FloatingPoint ts::FloatingPoint< FLOAT_T, PREC >::min ( const FloatingPoint< FLOAT_T, PREC > &  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 FLOAT_T , const size_t PREC = 6>
template<typename INT2 >
requires std::integral<INT2>
bool ts::FloatingPoint< FLOAT_T, PREC >::mulOverflow ( INT2  x) const
inline

Check if this FloatingPoint number generates an overflow when multiplied by an integer.

The method is present for compliance with other AbstractNumber subclasses.

Template Parameters
INT2Another integer type.
Parameters
[in]xAn integer of type INT2.
Returns
Always false. A better idea?

◆ mulOverflow() [2/2]

template<typename FLOAT_T , const size_t PREC = 6>
bool ts::FloatingPoint< FLOAT_T, PREC >::mulOverflow ( const FloatingPoint< FLOAT_T, PREC > &  x) const
inline

Check if this FloatingPoint number generates an overflow when multiplied by another FloatingPoint.

The method is present for compliance with other AbstractNumber subclasses.

Parameters
[in]xAnother FloatingPoint number.
Returns
Always false. A better idea?

◆ divOverflow()

template<typename FLOAT_T , const size_t PREC = 6>
bool ts::FloatingPoint< FLOAT_T, PREC >::divOverflow ( const FloatingPoint< FLOAT_T, PREC > &  x) const
inline

Check if this FloatingPoint number generates an overflow when divided by another FloatingPoint.

The method is present for compliance with other AbstractNumber subclasses.

Parameters
[in]xAnother FloatingPoint.
Returns
Always false. There is no possible division overflow with FloatingPoint.

◆ 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

◆ DISPLAY_PRECISION

template<typename FLOAT_T , const size_t PREC = 6>
constexpr size_t ts::FloatingPoint< FLOAT_T, PREC >::DISPLAY_PRECISION = PREC
staticconstexpr

The displayed precision of the floating-point type (number of decimal digits).

This is a display attribute only, it does not alter the binary representation of floating-point values.

◆ EQUAL_PRECISION

template<typename FLOAT_T , const size_t PREC = 6>
constexpr float_t ts::FloatingPoint< FLOAT_T, PREC >::EQUAL_PRECISION = 100.0 * std::numeric_limits<float_t>::min()
staticconstexpr

Precision of "equal" comparisons.

We cannot test equality between floating point values. Because of rounding issues, it does not make sense. Instead we check if the absolute value of their difference is lower than some very small value, this precision.

◆ 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: