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;
343 std::ostream& operator<<(std::ostream&,
GncNumeric);
348 template <
typename charT,
typename traits>
349 std::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) {}
365 else if (n.is_decimal())
366 ss << n.num() / n.denom() << dec_pt
367 << (n.num() > 0 ? n.num() : -n.num()) % n.denom();
369 ss << n.num() <<
"/" << n.denom();
387 template <
typename charT,
typename traits>
388 std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& s,
GncNumeric& n)
390 std::basic_string<charT, traits> instr;
401 inline int cmp(
GncNumeric a, int64_t b) {
return a.cmp(b); }
410 inline bool operator<(
GncNumeric a, int64_t b) {
return cmp(a, b) < 0; }
411 inline bool operator<(int64_t a,
GncNumeric b) {
return cmp(a, b) < 0; }
413 inline bool operator>(
GncNumeric a, int64_t b) {
return cmp(a, b) > 0; }
414 inline bool operator>(int64_t a,
GncNumeric b) {
return cmp(a, b) > 0; }
416 inline bool operator==(
GncNumeric a, int64_t b) {
return cmp(a, b) == 0; }
417 inline bool operator==(int64_t a,
GncNumeric b) {
return cmp(a, b) == 0; }
419 inline bool operator<=(
GncNumeric a, int64_t b) {
return cmp(a, b) <= 0; }
420 inline bool operator<=(int64_t a,
GncNumeric b) {
return cmp(a, b) <= 0; }
422 inline bool operator>=(
GncNumeric a, int64_t b) {
return cmp(a, b) >= 0; }
423 inline bool operator>=(int64_t a,
GncNumeric b) {
return cmp(a, b) >= 0; }
425 inline bool operator!=(
GncNumeric a, int64_t b) {
return cmp(a, b) != 0; }
426 inline bool operator!=(int64_t a,
GncNumeric b) {
return cmp(a, b) != 0; }
433 int64_t powten(
unsigned int digits);
435 #endif // __GNC_NUMERIC_HPP__ GncNumeric operator-() const noexcept
GncNumeric(int64_t num, int64_t denom)
Integer constructor.
The primary numeric class for representing amounts and values.
std::string to_string() const noexcept
Return a string representation of the GncNumeric.
GncNumeric abs() const noexcept
GncNumeric reduce() const noexcept
Return an equivalent fraction with all common factors between the numerator and the denominator remov...
GncNumeric to_decimal(unsigned int max_places=17) const
Convert the numeric to have a power-of-10 denominator if possible without rounding.
Rational number class using GncInt128 for the numerator and denominator.
GncNumeric(gnc_numeric in)
gnc_numeric constructor, used for interfacing old code.
GncNumeric()
Default constructor provides the zero value.
GncNumeric convert(int64_t new_denom) const
Convert a GncNumeric to use a new denominator.
GncNumeric convert_sigfigs(unsigned int figs) const
Convert with the specified sigfigs.
int64_t num() const noexcept
Accessor for numerator value.
GncNumeric inv() const noexcept
#define GNC_DENOM_AUTO
Values that can be passed as the 'denom' argument.
bool is_decimal() const noexcept
int64_t denom() const noexcept
Accessor for denominator value.