23 #ifndef __GNC_NUMERIC_HPP__ 24 #define __GNC_NUMERIC_HPP__ 30 #include "gnc-rational-rounding.hpp" 79 throw std::invalid_argument(
"Attempt to construct a GncNumeric with a 0 denominator.");
99 throw std::invalid_argument(
"Attempt to construct a GncNumeric with a 0 denominator.");
139 GncNumeric(
const std::string& str,
bool autoround=
false);
148 operator gnc_numeric() const noexcept;
152 operator
double() const noexcept;
157 int64_t
num() const noexcept {
return m_num; }
161 int64_t
denom() const noexcept {
return m_den; }
191 template <RoundType RT>
194 auto params = prepare_conversion(new_denom);
199 return GncNumeric(round(params.num, params.den,
200 params.rem,
RT2T<RT>()), new_denom);
214 template <RoundType RT>
217 auto new_denom(sigfigs_denom(figs));
218 auto params = prepare_conversion(new_denom);
223 return GncNumeric(round(params.num, params.den,
224 params.rem,
RT2T<RT>()), new_denom);
265 int cmp(int64_t b) {
return cmp(
GncNumeric(b, 1)); }
275 int64_t sigfigs_denom(
unsigned figs)
const noexcept;
279 round_param prepare_conversion(int64_t new_denom)
const;
342 std::ostream& operator<<(std::ostream&,
GncNumeric);
347 template <
typename charT,
typename traits>
348 std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& s,
GncNumeric n)
350 std::basic_ostringstream<charT, traits> ss;
351 std::locale loc = s.getloc();
353 auto dec_pt =
static_cast<charT
>(
'.');
356 dec_pt = std::use_facet<std::numpunct<charT>>(loc).decimal_point();
358 catch(
const std::bad_cast& err) {}
364 else if (n.is_decimal())
365 ss << n.num() / n.denom() << dec_pt
366 << (n.num() > 0 ? n.num() : -n.num()) % n.denom();
368 ss << n.num() <<
"/" << n.denom();
386 template <
typename charT,
typename traits>
387 std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& s,
GncNumeric& n)
389 std::basic_string<charT, traits> instr;
400 inline int cmp(
GncNumeric a, int64_t b) {
return a.cmp(b); }
409 inline bool operator<(
GncNumeric a, int64_t b) {
return cmp(a, b) < 0; }
410 inline bool operator<(int64_t a,
GncNumeric b) {
return cmp(a, b) < 0; }
412 inline bool operator>(
GncNumeric a, int64_t b) {
return cmp(a, b) > 0; }
413 inline bool operator>(int64_t a,
GncNumeric b) {
return cmp(a, b) > 0; }
415 inline bool operator==(
GncNumeric a, int64_t b) {
return cmp(a, b) == 0; }
416 inline bool operator==(int64_t a,
GncNumeric b) {
return cmp(a, b) == 0; }
418 inline bool operator<=(
GncNumeric a, int64_t b) {
return cmp(a, b) <= 0; }
419 inline bool operator<=(int64_t a,
GncNumeric b) {
return cmp(a, b) <= 0; }
421 inline bool operator>=(
GncNumeric a, int64_t b) {
return cmp(a, b) >= 0; }
422 inline bool operator>=(int64_t a,
GncNumeric b) {
return cmp(a, b) >= 0; }
424 inline bool operator!=(
GncNumeric a, int64_t b) {
return cmp(a, b) != 0; }
425 inline bool operator!=(int64_t a,
GncNumeric b) {
return cmp(a, b) != 0; }
432 int64_t powten(
unsigned int digits);
434 #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.