jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Public Types | Public Member Functions | Public Attributes | List of all members
jau::fraction< Int_type, > Class Template Reference

Fraction template type using integral values, evaluated at runtime. More...

#include <fraction_type.hpp>

Inheritance diagram for jau::fraction< Int_type, >:
Collaboration diagram for jau::fraction< Int_type, >:

Public Types

typedef Int_type int_type
 User defined integral integer template type, used for numerator and may be signed. More...
 
typedef std::make_unsigned_t< int_typeuint_type
 unsigned variant of template int_type, used for denominator. More...
 

Public Member Functions

constexpr fraction () noexcept
 Constructs a zero fraction instance { 0, 1 }. More...
 
template<typename T , std::enable_if_t< std::is_same_v< int_type, T > &&!std::is_unsigned_v< T >, bool > = true>
constexpr fraction (const int_type &n, const T &d) noexcept
 Constructs a fraction instance with smallest numerator and denominator using gcd() More...
 
constexpr fraction (const int_type &n, const uint_type &abs_d) noexcept
 Constructs a fraction instance with smallest numerator and denominator using gcd() More...
 
template<typename Rep , typename Period >
constexpr fraction (const std::chrono::duration< Rep, Period > &dur) noexcept
 Constructs a fraction from the given std::chrono::duration and its Rep and Period with smallest numerator and denominator using gcd() More...
 
constexpr bool is_zero () const noexcept
 Returns true if numerator is zero. More...
 
constexpr fraction< int_type > & operator*= (const fraction< int_type > &rhs) noexcept
 Multiplication in place. More...
 
constexpr fraction< int_type > & operator*= (const int_type &rhs) noexcept
 Multiplication of this fraction's numerator with scalar in place. More...
 
constexpr fraction< int_type > & operator+= (const fraction< int_type > &rhs) noexcept
 Compound assignment (addition) More...
 
constexpr fraction< int_typeoperator- () const noexcept
 Unary minus. More...
 
constexpr fraction< int_type > & operator-= (const fraction< int_type > &rhs) noexcept
 Negative compound assignment (subtraction) More...
 
constexpr fraction< int_type > & operator/= (const fraction< int_type > &rhs) noexcept
 Division in place. More...
 
constexpr fraction< int_type > & operator/= (const int_type &rhs) noexcept
 Division of this fraction's numerator with scalar in place. More...
 
constexpr fraction< int_type > & reduce () noexcept
 Reduce this fraction to the lowest terms using the greatest common denominator, see gcd(), i.e. More...
 
constexpr snsize_t sign () const noexcept
 Returns the value of the sign function applied to numerator. More...
 
constexpr double to_double () const noexcept
 Returns the converted fraction to lossy double. More...
 
template<typename Rep , typename Period >
std::chrono::duration< Rep, Period > to_duration (const std::chrono::duration< Rep, Period > &dur_ref, bool *overflow_ptr=nullptr) const noexcept
 Convert this fraction into std::chrono::duration with given Rep and Period. More...
 
constexpr float to_float () const noexcept
 Returns the converted fraction to lossy float. More...
 
constexpr long double to_ldouble () const noexcept
 Returns the converted fraction to lossy long double. More...
 
constexpr int_type to_ms () const noexcept
 Convenient shortcut to to_num_of(1_ms) More...
 
constexpr int_type to_ns () const noexcept
 Convenient shortcut to to_num_of(1_ns) More...
 
constexpr int_type to_num_of (const fraction< int_type > &new_base, bool *overflow_ptr=nullptr) const noexcept
 Converts this this fraction to a numerator for the given new base fraction. More...
 
constexpr int_type to_num_of (const int_type &new_base_num, const uint_type &new_base_denom, bool *overflow_ptr=nullptr) const noexcept
 Converts this this fraction to a numerator for the given new base fraction. More...
 
std::string to_string (const bool show_double=false) const noexcept
 Returns a string representation of this fraction. More...
 
constexpr int_type to_us () const noexcept
 Convenient shortcut to to_num_of(1_us) More...
 

Public Attributes

uint_type denom
 Denominator, always positive. More...
 
int_type num
 Numerator, carries the sign. More...
 
bool overflow
 Overflow flag. More...
 

Detailed Description

template<typename Int_type, std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
class jau::fraction< Int_type, >

Fraction template type using integral values, evaluated at runtime.

All operations reduce its fraction to the lowest terms using the greatest common divisor (gcd()) following Euclid's algorithm from Euclid's Elements ~300 BC, see reduce().

fraction provides similar properties like C++11's std::ratio, but is evaluated at runtime time without constexpr constraints using a common integral template type. std::ratio is evaluated at compile time and must use constexpr literal values.

fraction provides similar properties like C++11's std::chrono::duration, but is flexible with its denominator and always reduce() its fraction to the lowest terms. std::chrono::duration uses a fixed std::ratio denominator and hence is inflexible.

Further, fraction can be converted to std::chrono::duration, matching the selected duration's period, see to_duration_count() and to_duration(). However, it is recommended to use fraction_timespec, see below.

The following properties are exposed:

See usable fixed typedef's

fraction_timespec covers high precision and an almost infinite range of time similar to struct timespec_t.

Counting nanoseconds in int64_t only lasts until 2262-04-12, since INT64_MAX is 9'223'372'036'854'775'807 for 9'223'372'036 seconds or 292 years.

Hence one may use fraction_i64 for durations up to 292 years and fraction_timespec for almost infinite range of time-points or durations beyond 292 years.

Constants are provided in namespace jau::fractions_i64, from fractions_i64::pico to fractions_i64::tera, including fractions_i64::seconds to fractions_i64::years, etc.

Literal operators are provided in namespace jau::fractions_i64_literals, e.g. for 3_s, 100_ns ... literals.

Template Parameters
int_type

Definition at line 109 of file fraction_type.hpp.

Member Typedef Documentation

◆ int_type

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
typedef Int_type jau::fraction< Int_type, >::int_type

User defined integral integer template type, used for numerator and may be signed.

Definition at line 112 of file fraction_type.hpp.

◆ uint_type

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
typedef std::make_unsigned_t<int_type> jau::fraction< Int_type, >::uint_type

unsigned variant of template int_type, used for denominator.

Definition at line 115 of file fraction_type.hpp.

Constructor & Destructor Documentation

◆ fraction() [1/4]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr jau::fraction< Int_type, >::fraction ( )
inlineconstexprnoexcept

Constructs a zero fraction instance { 0, 1 }.

Definition at line 137 of file fraction_type.hpp.

◆ fraction() [2/4]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
template<typename T , std::enable_if_t< std::is_same_v< int_type, T > &&!std::is_unsigned_v< T >, bool > = true>
constexpr jau::fraction< Int_type, >::fraction ( const int_type n,
const T &  d 
)
inlineconstexprnoexcept

Constructs a fraction instance with smallest numerator and denominator using gcd()

Note: sign is always stored in fraction's numerator, i.e. the denominator is always positive.

Parameters
nthe given numerator
dthe given denominator

Definition at line 152 of file fraction_type.hpp.

◆ fraction() [3/4]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr jau::fraction< Int_type, >::fraction ( const int_type n,
const uint_type abs_d 
)
inlineconstexprnoexcept

Constructs a fraction instance with smallest numerator and denominator using gcd()

Note: sign is always stored in fraction's numerator, i.e. the denominator is always positive and hence unsigned.

Parameters
nthe given numerator
dthe given denominator

Definition at line 173 of file fraction_type.hpp.

◆ fraction() [4/4]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
template<typename Rep , typename Period >
constexpr jau::fraction< Int_type, >::fraction ( const std::chrono::duration< Rep, Period > &  dur)
inlineconstexprnoexcept

Constructs a fraction from the given std::chrono::duration and its Rep and Period with smallest numerator and denominator using gcd()

Note: sign is always stored in fraction's numerator, i.e. the denominator is always positive and hence unsigned.

Template Parameters
RepRep of given std::chrono::duration
PeriodPeriod of given std::chrono::duration
Parameters
durstd::chrono::duration reference to convert into a fraction

Definition at line 325 of file fraction_type.hpp.

Member Function Documentation

◆ reduce()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::reduce ( )
inlineconstexprnoexcept

Reduce this fraction to the lowest terms using the greatest common denominator, see gcd(), i.e.

normalization.

Might need to be called after manual modifications on numerator or denominator.

Not required after applying any provided operation as they normalize the fraction.

Definition at line 212 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_num_of() [1/2]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr int_type jau::fraction< Int_type, >::to_num_of ( const fraction< int_type > &  new_base,
bool *  overflow_ptr = nullptr 
) const
inlineconstexprnoexcept

Converts this this fraction to a numerator for the given new base fraction.

If overflow_ptr is not nullptr, true is stored if an overflow occurred otherwise false.

Parameters
new_basethe new base fraction for conversion
overflow_ptroptional pointer to overflow result, defaults to nullptr
Returns
numerator representing this fraction on the new base, or std::numeric_limits<int_type>::max() if an overflow occurred.

Definition at line 230 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_num_of() [2/2]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr int_type jau::fraction< Int_type, >::to_num_of ( const int_type new_base_num,
const uint_type new_base_denom,
bool *  overflow_ptr = nullptr 
) const
inlineconstexprnoexcept

Converts this this fraction to a numerator for the given new base fraction.

If overflow_ptr is not nullptr, true is stored if an overflow occurred otherwise false.

Parameters
new_base_numthe new base numerator for conversion
new_base_denomthe new base denominator for conversion
new_basethe new base fraction for conversion
overflow_ptroptional pointer to overflow result, defaults to nullptr
Returns
numerator representing this fraction on the new base, or std::numeric_limits<int_type>::max() if an overflow occurred.

Definition at line 264 of file fraction_type.hpp.

◆ to_ms()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr int_type jau::fraction< Int_type, >::to_ms ( ) const
inlineconstexprnoexcept

Convenient shortcut to to_num_of(1_ms)

Returns
time in milliseconds
See also
to_num_of()

Definition at line 289 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_us()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr int_type jau::fraction< Int_type, >::to_us ( ) const
inlineconstexprnoexcept

Convenient shortcut to to_num_of(1_us)

Returns
time in microseconds
See also
to_num_of()

Definition at line 296 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_ns()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr int_type jau::fraction< Int_type, >::to_ns ( ) const
inlineconstexprnoexcept

Convenient shortcut to to_num_of(1_ns)

Returns
time in nanoseconds
See also
to_num_of()

Definition at line 303 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_float()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr float jau::fraction< Int_type, >::to_float ( ) const
inlineconstexprnoexcept

Returns the converted fraction to lossy float.

Definition at line 306 of file fraction_type.hpp.

◆ to_double()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr double jau::fraction< Int_type, >::to_double ( ) const
inlineconstexprnoexcept

Returns the converted fraction to lossy double.

Definition at line 309 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_ldouble()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr long double jau::fraction< Int_type, >::to_ldouble ( ) const
inlineconstexprnoexcept

Returns the converted fraction to lossy long double.

Definition at line 312 of file fraction_type.hpp.

◆ to_duration()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
template<typename Rep , typename Period >
std::chrono::duration< Rep, Period > jau::fraction< Int_type, >::to_duration ( const std::chrono::duration< Rep, Period > &  dur_ref,
bool *  overflow_ptr = nullptr 
) const
inlinenoexcept

Convert this fraction into std::chrono::duration with given Rep and Period.

If overflow_ptr is not nullptr, true is stored if an overflow occurred otherwise false.

Template Parameters
Repstd::chrono::duration numerator type
Periodstd::chrono::duration denominator type, i.e. a std::ratio
Parameters
dur_refstd::chrono::duration reference to please automated template type deduction and ease usage
overflow_ptroptional pointer to overflow result, defaults to nullptr
Returns
fraction converted into given std::chrono::duration Rep and Period, or using (Rep)std::numeric_limits<Rep>::max() if an overflow occurred

Definition at line 351 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ to_string()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
std::string jau::fraction< Int_type, >::to_string ( const bool  show_double = false) const
inlinenoexcept

Returns a string representation of this fraction.

If the overflow flag is set, O! will be appended.

Parameters
show_doubletrue to show the double value, otherwise false (default)
Returns

Definition at line 376 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ is_zero()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr bool jau::fraction< Int_type, >::is_zero ( ) const
inlineconstexprnoexcept

Returns true if numerator is zero.

Definition at line 399 of file fraction_type.hpp.

◆ sign()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr snsize_t jau::fraction< Int_type, >::sign ( ) const
inlineconstexprnoexcept

Returns the value of the sign function applied to numerator.

-1 for numerator < 0
 0 for numerator = 0
 1 for numerator > 0
Returns
function result

Definition at line 412 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ operator-()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > jau::fraction< Int_type, >::operator- ( ) const
inlineconstexprnoexcept

Unary minus.

Returns
new instance with negated value, reduced

Definition at line 421 of file fraction_type.hpp.

◆ operator*=() [1/2]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::operator*= ( const int_type rhs)
inlineconstexprnoexcept

Multiplication of this fraction's numerator with scalar in place.

Operation may set the overflow flag if occurring.

Parameters
rhsthe scalar
Returns
reference to this instance, reduced

Definition at line 435 of file fraction_type.hpp.

◆ operator/=() [1/2]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::operator/= ( const int_type rhs)
inlineconstexprnoexcept

Division of this fraction's numerator with scalar in place.

Parameters
rhsthe scalar
Returns
reference to this instance, reduced

Definition at line 450 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ operator+=()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::operator+= ( const fraction< int_type > &  rhs)
inlineconstexprnoexcept

Compound assignment (addition)

Operation may set the overflow flag if occurring.

Parameters
rhsthe other fraction
Returns
reference to this instance, reduced

Definition at line 462 of file fraction_type.hpp.

◆ operator-=()

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::operator-= ( const fraction< int_type > &  rhs)
inlineconstexprnoexcept

Negative compound assignment (subtraction)

Operation may set the overflow flag if occurring.

Parameters
rhsthe other fraction
Returns
reference to this instance, reduced

Definition at line 487 of file fraction_type.hpp.

◆ operator*=() [2/2]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::operator*= ( const fraction< int_type > &  rhs)
inlineconstexprnoexcept

Multiplication in place.

Operation may set the overflow flag if occurring.

Parameters
rhsthe other fraction
Returns
reference to this instance, reduced

Definition at line 523 of file fraction_type.hpp.

◆ operator/=() [2/2]

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
constexpr fraction< int_type > & jau::fraction< Int_type, >::operator/= ( const fraction< int_type > &  rhs)
inlineconstexprnoexcept

Division in place.

Parameters
rhsthe other fraction
Returns
reference to this instance, reduced

Definition at line 543 of file fraction_type.hpp.

Member Data Documentation

◆ num

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
int_type jau::fraction< Int_type, >::num

Numerator, carries the sign.

Definition at line 118 of file fraction_type.hpp.

◆ denom

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
uint_type jau::fraction< Int_type, >::denom

Denominator, always positive.

Definition at line 120 of file fraction_type.hpp.

◆ overflow

template<typename Int_type , std::enable_if_t< std::is_integral_v< Int_type >, bool > = true>
bool jau::fraction< Int_type, >::overflow

Overflow flag.

If set, last arithmetic operation produced an overflow. Must be cleared manually.

Definition at line 122 of file fraction_type.hpp.


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