GnuCash
5.6-150-g038405b370+
|
Rational number class using GncInt128 for the numerator and denominator. More...
#include <gnc-rational.hpp>
Public Member Functions | |
GncRational () | |
Default constructor provides the zero value. | |
GncRational (GncInt128 num, GncInt128 den) noexcept | |
GncInt128 constructor. More... | |
GncRational (gnc_numeric n) noexcept | |
Convenience constructor from the C API's gnc_numeric. More... | |
GncRational (GncNumeric n) noexcept | |
GncNumeric constructor. More... | |
GncRational (const GncRational &rhs)=default | |
GncRational (GncRational &&rhs)=default | |
GncRational & | operator= (const GncRational &rhs)=default |
GncRational & | operator= (GncRational &&rhs)=default |
bool | valid () const noexcept |
Report if both members are valid numbers. More... | |
bool | is_big () const noexcept |
Report if either numerator or denominator are too big to fit in an int64_t. More... | |
operator gnc_numeric () const noexcept | |
Conversion operator; use static_cast<gnc_numeric>(foo). More... | |
GncRational | operator- () const noexcept |
Make a new GncRational with the opposite sign. More... | |
GncRational | reduce () const |
Return an equivalent fraction with all common factors between the numerator and the denominator removed. More... | |
GncRational | round_to_numeric () const |
Round to fit an int64_t, finding the closest possible approximation. More... | |
template<RoundType RT> | |
GncRational | convert (GncInt128 new_denom) const |
Convert a GncRational to use a new denominator. More... | |
template<RoundType RT> | |
GncRational | convert_sigfigs (unsigned int figs) const |
Convert with the specified sigfigs. More... | |
GncInt128 | num () const noexcept |
Numerator accessor. | |
GncInt128 | denom () const noexcept |
Denominator accessor. | |
void | operator+= (GncRational b) |
void | operator-= (GncRational b) |
void | operator*= (GncRational b) |
void | operator/= (GncRational b) |
GncRational | inv () const noexcept |
Inverts the number, equivalent of /= {1, 1}. | |
GncRational | abs () const noexcept |
Absolute value; return value is always >= 0 and of same magnitude. More... | |
int | cmp (GncRational b) |
Compare function. More... | |
int | cmp (GncInt128 b) |
Rational number class using GncInt128 for the numerator and denominator.
This class provides far greater overflow protection compared to GncNumeric at the expense of doubling the size, so GncNumeric is preferred for storage into objects. Furthermore the backends are not able to store GncRational numbers; storage in SQL would require using BLOBs which would preclude calculations in queries. GncRational exists primarily as a more overflow-resistant calculation facility for GncNumeric. It's available for cases where one needs an error instead of an automatically rounded value for a calculation that produces a result that won't fit into an int64 without rounding.
Errors: Errors are signalled by exceptions as follows:
Definition at line 57 of file gnc-rational.hpp.
GncInt128 constructor.
This will take any flavor of built-in integer thanks to implicit construction of the GncInt128s.
Definition at line 68 of file gnc-rational.hpp.
|
noexcept |
Convenience constructor from the C API's gnc_numeric.
Definition at line 39 of file gnc-rational.cpp.
|
noexcept |
GncNumeric constructor.
Definition at line 29 of file gnc-rational.cpp.
|
noexcept |
Absolute value; return value is always >= 0 and of same magnitude.
Definition at line 96 of file gnc-rational.cpp.
int GncRational::cmp | ( | GncRational | b | ) |
Compare function.
b | GncNumeric or integer value to compare to. |
Definition at line 132 of file gnc-rational.cpp.
|
inline |
Convert a GncRational to use a new denominator.
If rounding is necessary use the indicated template specification. For example, to use half-up rounding you'd call bar = foo.convert<RoundType::half_up>(1000). If you specify RoundType::never this will throw std::domain_error if rounding is required.
new_denom | The new denominator to convert the fraction to. |
Definition at line 117 of file gnc-rational.hpp.
|
inline |
Convert with the specified sigfigs.
The resulting denominator depends on the value of the GncRational, such that the specified significant digits are retained in the numerator and the denominator is always a power of
figs | The number of digits to use for the numerator. |
Definition at line 140 of file gnc-rational.hpp.
|
noexcept |
Report if either numerator or denominator are too big to fit in an int64_t.
Definition at line 58 of file gnc-rational.cpp.
|
noexcept |
Conversion operator; use static_cast<gnc_numeric>(foo).
Definition at line 65 of file gnc-rational.cpp.
|
noexcept |
Make a new GncRational with the opposite sign.
Definition at line 80 of file gnc-rational.cpp.
GncRational GncRational::reduce | ( | ) | const |
Return an equivalent fraction with all common factors between the numerator and the denominator removed.
Definition at line 181 of file gnc-rational.cpp.
GncRational GncRational::round_to_numeric | ( | ) | const |
Round to fit an int64_t, finding the closest possible approximation.
Throws std::overflow_error if m_den is 1 and m_num is big.
Definition at line 190 of file gnc-rational.cpp.
|
noexcept |
Report if both members are valid numbers.
Definition at line 50 of file gnc-rational.cpp.