mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Implement gnc_numeric_invert via GncRational::invert.
This commit is contained in:
parent
50e3bf21db
commit
5537a7ed35
@ -487,20 +487,7 @@ gnc_numeric_invert(gnc_numeric num)
|
|||||||
{
|
{
|
||||||
if (num.num == 0)
|
if (num.num == 0)
|
||||||
return gnc_numeric_zero();
|
return gnc_numeric_zero();
|
||||||
if (num.denom > 0)
|
return static_cast<gnc_numeric>(GncNumeric(num).inv());
|
||||||
{
|
|
||||||
if (num.num < 0)
|
|
||||||
return gnc_numeric_create (-num.denom, -num.num);
|
|
||||||
return gnc_numeric_create (num.denom, num.num);
|
|
||||||
}
|
|
||||||
else /* Negative denominator means multiply instead of divide. */
|
|
||||||
{
|
|
||||||
int64_t mult = (num.num < 0 ? INT64_C(-1) : INT64_C(1));
|
|
||||||
qofint128 denom = mult128(-num.denom, mult * num.num);
|
|
||||||
if (denom.hi)
|
|
||||||
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
|
|
||||||
return gnc_numeric_create (mult, denom.lo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/* *******************************************************************
|
/* *******************************************************************
|
||||||
* double_to_gnc_numeric
|
* double_to_gnc_numeric
|
||||||
|
@ -80,9 +80,7 @@ GncRational::operator-() const noexcept
|
|||||||
GncRational&
|
GncRational&
|
||||||
GncRational::inv () noexcept
|
GncRational::inv () noexcept
|
||||||
{
|
{
|
||||||
auto tmp = m_num;
|
std::swap(m_num, m_den);
|
||||||
m_num = m_den;
|
|
||||||
m_den = tmp;
|
|
||||||
|
|
||||||
GncRational b {1, 1};
|
GncRational b {1, 1};
|
||||||
GncDenom d {*this, b, INT64_C(0), GNC_HOW_RND_NEVER };
|
GncDenom d {*this, b, INT64_C(0), GNC_HOW_RND_NEVER };
|
||||||
|
@ -44,6 +44,7 @@ public:
|
|||||||
GncRational& div(GncRational b, GncDenom& d) noexcept;
|
GncRational& div(GncRational b, GncDenom& d) noexcept;
|
||||||
GncRational& add(const GncRational& b, GncDenom& d) noexcept;
|
GncRational& add(const GncRational& b, GncDenom& d) noexcept;
|
||||||
GncRational& sub(const GncRational& b, GncDenom& d) noexcept;
|
GncRational& sub(const GncRational& b, GncDenom& d) noexcept;
|
||||||
|
|
||||||
/** Inverts the number, equivalent of /= {1, 1} */
|
/** Inverts the number, equivalent of /= {1, 1} */
|
||||||
GncRational& inv() noexcept;
|
GncRational& inv() noexcept;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user