Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
|
Fraction template type using integral values, evaluated at runtime. More...
#include <fraction_type.hpp>
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_type > | uint_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_type > | operator- () 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... | |
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.
int_type | |
Definition at line 109 of file fraction_type.hpp.
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.
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.
|
inlineconstexprnoexcept |
Constructs a zero fraction instance { 0, 1 }.
Definition at line 137 of file fraction_type.hpp.
|
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.
n | the given numerator |
d | the given denominator |
Definition at line 152 of file fraction_type.hpp.
|
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.
n | the given numerator |
d | the given denominator |
Definition at line 173 of file fraction_type.hpp.
|
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.
Rep | Rep of given std::chrono::duration |
Period | Period of given std::chrono::duration |
dur | std::chrono::duration reference to convert into a fraction |
Definition at line 325 of file fraction_type.hpp.
|
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.
|
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.
new_base | the new base fraction for conversion |
overflow_ptr | optional pointer to overflow result, defaults to nullptr |
Definition at line 230 of file fraction_type.hpp.
|
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.
new_base_num | the new base numerator for conversion |
new_base_denom | the new base denominator for conversion |
new_base | the new base fraction for conversion |
overflow_ptr | optional pointer to overflow result, defaults to nullptr |
Definition at line 264 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Convenient shortcut to to_num_of(1_ms)
Definition at line 289 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Convenient shortcut to to_num_of(1_us)
Definition at line 296 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Convenient shortcut to to_num_of(1_ns)
Definition at line 303 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Returns the converted fraction to lossy float.
Definition at line 306 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Returns the converted fraction to lossy double.
Definition at line 309 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Returns the converted fraction to lossy long double.
Definition at line 312 of file fraction_type.hpp.
|
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.
Rep | std::chrono::duration numerator type |
Period | std::chrono::duration denominator type, i.e. a std::ratio |
dur_ref | std::chrono::duration reference to please automated template type deduction and ease usage |
overflow_ptr | optional pointer to overflow result, defaults to nullptr |
Definition at line 351 of file fraction_type.hpp.
|
inlinenoexcept |
Returns a string representation of this fraction.
If the overflow flag is set, O!
will be appended.
show_double | true to show the double value, otherwise false (default) |
Definition at line 376 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Returns true if numerator is zero.
Definition at line 399 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Returns the value of the sign function applied to numerator.
-1 for numerator < 0 0 for numerator = 0 1 for numerator > 0
Definition at line 412 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Unary minus.
Definition at line 421 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Multiplication of this fraction's numerator with scalar in place.
Operation may set the overflow flag if occurring.
rhs | the scalar |
Definition at line 435 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Division of this fraction's numerator with scalar in place.
rhs | the scalar |
Definition at line 450 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Compound assignment (addition)
Operation may set the overflow flag if occurring.
rhs | the other fraction |
Definition at line 462 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Negative compound assignment (subtraction)
Operation may set the overflow flag if occurring.
rhs | the other fraction |
Definition at line 487 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Multiplication in place.
Operation may set the overflow flag if occurring.
rhs | the other fraction |
Definition at line 523 of file fraction_type.hpp.
|
inlineconstexprnoexcept |
Division in place.
rhs | the other fraction |
Definition at line 543 of file fraction_type.hpp.
int_type jau::fraction< Int_type, >::num |
Numerator, carries the sign.
Definition at line 118 of file fraction_type.hpp.
uint_type jau::fraction< Int_type, >::denom |
Denominator, always positive.
Definition at line 120 of file fraction_type.hpp.
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.