23#ifndef __GNC_NUMERIC_HPP__
24#define __GNC_NUMERIC_HPP__
31#include "gnc-rational-rounding.hpp"
80 throw std::invalid_argument(
"Attempt to construct a GncNumeric with a 0 denominator.");
100 throw std::invalid_argument(
"Attempt to construct a GncNumeric with a 0 denominator.");
140 GncNumeric(
const std::string& str,
bool autoround=
false);
149 operator gnc_numeric() const noexcept;
153 operator
double() const noexcept;
158 int64_t
num() const noexcept {
return m_num; }
162 int64_t
denom() const noexcept {
return m_den; }
192 template <RoundType RT>
195 auto params = prepare_conversion(new_denom);
200 return GncNumeric(round(params.num, params.den,
201 params.rem,
RT2T<RT>()), new_denom);
215 template <RoundType RT>
218 auto new_denom(sigfigs_denom(figs));
219 auto params = prepare_conversion(new_denom);
224 return GncNumeric(round(params.num, params.den,
225 params.rem,
RT2T<RT>()), new_denom);
266 int cmp(int64_t b) {
return cmp(
GncNumeric(b, 1)); }
276 int64_t sigfigs_denom(
unsigned figs)
const noexcept;
280 round_param prepare_conversion(int64_t new_denom)
const;
343std::ostream& operator<<(std::ostream&,
GncNumeric);
348template <
typename charT,
typename traits>
349std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& s,
GncNumeric n)
351 std::basic_ostringstream<charT, traits> ss;
352 std::locale loc = s.getloc();
354 auto dec_pt =
static_cast<charT
>(
'.');
357 dec_pt = std::use_facet<std::numpunct<charT>>(loc).decimal_point();
359 catch(
const std::bad_cast& err) {}
387template <
typename charT,
typename traits>
388std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& s,
GncNumeric& n)
390 std::basic_string<charT, traits> instr;
401inline int cmp(
GncNumeric a, int64_t b) {
return a.cmp(b); }
410inline bool operator<(
GncNumeric a, int64_t b) {
return cmp(a, b) < 0; }
411inline bool operator<(int64_t a,
GncNumeric b) {
return cmp(a, b) < 0; }
413inline bool operator>(
GncNumeric a, int64_t b) {
return cmp(a, b) > 0; }
414inline bool operator>(int64_t a,
GncNumeric b) {
return cmp(a, b) > 0; }
416inline bool operator==(
GncNumeric a, int64_t b) {
return cmp(a, b) == 0; }
417inline bool operator==(int64_t a,
GncNumeric b) {
return cmp(a, b) == 0; }
419inline bool operator<=(
GncNumeric a, int64_t b) {
return cmp(a, b) <= 0; }
420inline bool operator<=(int64_t a,
GncNumeric b) {
return cmp(a, b) <= 0; }
422inline bool operator>=(
GncNumeric a, int64_t b) {
return cmp(a, b) >= 0; }
423inline bool operator>=(int64_t a,
GncNumeric b) {
return cmp(a, b) >= 0; }
425inline bool operator!=(
GncNumeric a, int64_t b) {
return cmp(a, b) != 0; }
426inline bool operator!=(int64_t a,
GncNumeric b) {
return cmp(a, b) != 0; }
433int64_t powten(
unsigned int digits);
The primary numeric class for representing amounts and values.
GncNumeric to_decimal(unsigned int max_places=17) const
Convert the numeric to have a power-of-10 denominator if possible without rounding.
GncNumeric reduce() const noexcept
Return an equivalent fraction with all common factors between the numerator and the denominator remov...
GncNumeric operator-() const noexcept
bool is_decimal() const noexcept
GncNumeric inv() const noexcept
GncNumeric(gnc_numeric in)
gnc_numeric constructor, used for interfacing old code.
int64_t denom() const noexcept
Accessor for denominator value.
GncNumeric abs() const noexcept
GncNumeric()
Default constructor provides the zero value.
GncNumeric convert_sigfigs(unsigned int figs) const
Convert with the specified sigfigs.
GncNumeric(int64_t num, int64_t denom)
Integer constructor.
int64_t num() const noexcept
Accessor for numerator value.
std::string to_string() const noexcept
Return a string representation of the GncNumeric.
GncNumeric convert(int64_t new_denom) const
Convert a GncNumeric to use a new denominator.
Rational number class using GncInt128 for the numerator and denominator.
#define GNC_DENOM_AUTO
Values that can be passed as the 'denom' argument.