23#ifndef __GNC_RATIONAL_HPP__
24#define __GNC_RATIONAL_HPP__
27#include "gnc-int128.hpp"
28#include "gnc-rational-rounding.hpp"
69 : m_num(
num), m_den(den) {}
82 bool valid() const noexcept;
87 bool is_big() const noexcept;
89 operator gnc_numeric() const noexcept;
116 template <RoundType RT>
119 auto params = prepare_conversion(new_denom);
125 params.rem,
RT2T<RT>()), new_denom);
139 template <RoundType RT>
142 auto new_denom(sigfigs_denom(figs));
143 auto params = prepare_conversion(new_denom);
149 params.rem,
RT2T<RT>()), new_denom);
188 GncInt128 sigfigs_denom(
unsigned figs)
const noexcept;
192 round_param prepare_conversion(
GncInt128 new_denom)
const;
279inline std::ostream& operator<<(std::ostream& stream,
const GncRational& val)
noexcept
281 stream << val.num() <<
"/" << val.denom();
The primary numeric class for representing amounts and values.
Rational number class using GncInt128 for the numerator and denominator.
bool valid() const noexcept
Report if both members are valid numbers.
GncRational abs() const noexcept
Absolute value; return value is always >= 0 and of same magnitude.
GncRational inv() const noexcept
Inverts the number, equivalent of /= {1, 1}.
GncInt128 denom() const noexcept
Denominator accessor.
GncInt128 num() const noexcept
Numerator accessor.
GncRational()
Default constructor provides the zero value.
bool is_big() const noexcept
Report if either numerator or denominator are too big to fit in an int64_t.
GncRational round_to_numeric() const
Round to fit an int64_t, finding the closest possible approximation.
GncRational(GncInt128 num, GncInt128 den) noexcept
GncInt128 constructor.
int cmp(GncRational b)
Compare function.
GncRational convert(GncInt128 new_denom) const
Convert a GncRational to use a new denominator.
GncRational convert_sigfigs(unsigned int figs) const
Convert with the specified sigfigs.
GncRational reduce() const
Return an equivalent fraction with all common factors between the numerator and the denominator remov...
An exact-rational-number library for gnucash.
#define GNC_DENOM_AUTO
Values that can be passed as the 'denom' argument.