GnuCash
5.6-150-g038405b370+
|
The primary numeric class for representing amounts and values. More...
#include <gnc-numeric.hpp>
Public Member Functions | |
GncNumeric () | |
Default constructor provides the zero value. | |
GncNumeric (int64_t num, int64_t denom) | |
Integer constructor. More... | |
GncNumeric (GncRational rr) | |
GncRational constructor. More... | |
GncNumeric (gnc_numeric in) | |
gnc_numeric constructor, used for interfacing old code. More... | |
GncNumeric (double d) | |
Double constructor. More... | |
GncNumeric (const std::string &str, bool autoround=false) | |
String constructor. More... | |
GncNumeric (const GncNumeric &rhs)=default | |
GncNumeric (GncNumeric &&rhs)=default | |
GncNumeric & | operator= (const GncNumeric &rhs)=default |
GncNumeric & | operator= (GncNumeric &&rhs)=default |
operator gnc_numeric () const noexcept | |
gnc_numeric conversion. More... | |
operator double () const noexcept | |
double conversion. More... | |
int64_t | num () const noexcept |
Accessor for numerator value. | |
int64_t | denom () const noexcept |
Accessor for denominator value. | |
GncNumeric | operator- () const noexcept |
GncNumeric | inv () const noexcept |
GncNumeric | abs () const noexcept |
GncNumeric | reduce () const noexcept |
Return an equivalent fraction with all common factors between the numerator and the denominator removed. More... | |
template<RoundType RT> | |
GncNumeric | convert (int64_t new_denom) const |
Convert a GncNumeric to use a new denominator. More... | |
template<RoundType RT> | |
GncNumeric | convert_sigfigs (unsigned int figs) const |
Convert with the specified sigfigs. More... | |
std::string | to_string () const noexcept |
Return a string representation of the GncNumeric. More... | |
bool | is_decimal () const noexcept |
GncNumeric | to_decimal (unsigned int max_places=17) const |
Convert the numeric to have a power-of-10 denominator if possible without rounding. More... | |
void | operator+= (GncNumeric b) |
void | operator-= (GncNumeric b) |
void | operator*= (GncNumeric b) |
void | operator/= (GncNumeric b) |
int | cmp (GncNumeric b) |
int | cmp (int64_t b) |
The primary numeric class for representing amounts and values.
Calculations are generally performed in 128-bit (by converting to GncRational) and reducing the result. If the result would overflow a 64-bit representation then the GncNumeric(GncRational&) constructor will call GncRational::round_to_numeric() to get the value to fit. It will not raise an exception, so in the unlikely event that you need an error instead of rounding, use GncRational directly.
Errors: Errors are signalled by exceptions as follows:
Rounding Policy: GncNumeric provides a convert() member function that object amount and value setters (and only those functions!) should call to set a number which is represented in the commodity's SCU. Since SCUs are seldom 18 digits the convert may result in rounding, which will be performed in the method specified by the arguments passed to convert(). Overflows may result in internal rounding as described earlier.
Definition at line 60 of file gnc-numeric.hpp.
|
inline |
Integer constructor.
Unfortunately specifying a default for denom causes ambiguity errors with the other single-argument constructors on older versions of gcc, so one must always specify both arguments.
num | The Numerator |
denom | The Denominator |
Definition at line 77 of file gnc-numeric.hpp.
GncNumeric::GncNumeric | ( | GncRational | rr | ) |
GncRational constructor.
This constructor will round rr's GncInt128s to fit into the int64_t members using RoundType::half-down.
rr | A GncRational. |
Definition at line 70 of file gnc-numeric.cpp.
|
inline |
gnc_numeric constructor, used for interfacing old code.
This function should not be used outside of gnc-numeric.cpp.
in | A gnc_numeric. |
Definition at line 97 of file gnc-numeric.hpp.
GncNumeric::GncNumeric | ( | double | d | ) |
Double constructor.
d | The double to be converted. In order to fit in an int64_t, its absolute value must be < 1e18; if its absolute value is < 1e-18 it will be represented as 0, though for practical purposes nearly all commodities will round to zero at 1e-9 or larger. |
Definition at line 86 of file gnc-numeric.cpp.
GncNumeric::GncNumeric | ( | const std::string & | str, |
bool | autoround = false |
||
) |
String constructor.
Accepts integer values in decimal and hexadecimal. Does not accept thousands separators. If the string contains a '/' it is taken to separate the numerator and denominator; if it contains either a '.' or a ',' it is taken as a decimal point and the integers on either side will be combined and a denominator will be the appropriate power of 10. If neither is present the number will be treated as an integer and m_den will be set to 1.
Whitespace around a '/' is ignored. A correctly-formatted number will be extracted from a larger string.
Numbers that cannot be represented with int64_ts will throw std::out_of_range unless a decimal point is found (see above). A 0 denominator will cause the constructor to throw std::underflow_error. An empty string or one which contains no recognizable number will result in std::invalid_argument.
Definition at line 244 of file gnc-numeric.cpp.
|
noexcept |
Definition at line 385 of file gnc-numeric.cpp.
|
inline |
Convert a GncNumeric 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 193 of file gnc-numeric.hpp.
|
inline |
Convert with the specified sigfigs.
The resulting denominator depends on the value of the GncNumeric, 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 216 of file gnc-numeric.hpp.
|
noexcept |
Definition at line 375 of file gnc-numeric.cpp.
|
noexcept |
Definition at line 449 of file gnc-numeric.cpp.
|
noexcept |
|
noexcept |
gnc_numeric conversion.
Use static_cast<gnc_numeric>(foo)
Definition at line 356 of file gnc-numeric.cpp.
|
noexcept |
Definition at line 367 of file gnc-numeric.cpp.
|
noexcept |
Return an equivalent fraction with all common factors between the numerator and the denominator removed.
Definition at line 393 of file gnc-numeric.cpp.
GncNumeric GncNumeric::to_decimal | ( | unsigned int | max_places = 17 | ) | const |
Convert the numeric to have a power-of-10 denominator if possible without rounding.
Throws a std::range_error on failure; the message will explain the details.
max_places | exponent of the largest permissible denominator. |
Definition at line 462 of file gnc-numeric.cpp.
|
noexcept |
Return a string representation of the GncNumeric.
See operator<< for details.
Definition at line 441 of file gnc-numeric.cpp.