Use Number for IOUAmount and STAmount arithmetic

* Guarded by amendment fixUniversalNumber
* Produces slightly better accuracy in some computations.
This commit is contained in:
Howard Hinnant
2022-06-01 15:51:00 -04:00
committed by Elliot Lee
parent 48e804c40c
commit a82ad5ba76
14 changed files with 233 additions and 222 deletions

View File

@@ -339,6 +339,19 @@ STAmount::iou() const
return {mantissa, exponent};
}
STAmount&
STAmount::operator=(IOUAmount const& iou)
{
assert(mIsNative == false);
mOffset = iou.exponent();
mIsNegative = iou < beast::zero;
if (mIsNegative)
mValue = static_cast<std::uint64_t>(-iou.mantissa());
else
mValue = static_cast<std::uint64_t>(iou.mantissa());
return *this;
}
//------------------------------------------------------------------------------
//
// Operators
@@ -382,6 +395,13 @@ operator+(STAmount const& v1, STAmount const& v2)
if (v1.native())
return {v1.getFName(), getSNValue(v1) + getSNValue(v2)};
if (*stNumberSwitchover)
{
auto x = v1;
x = v1.iou() + v2.iou();
return x;
}
int ov1 = v1.exponent(), ov2 = v2.exponent();
std::int64_t vv1 = static_cast<std::int64_t>(v1.mantissa());
std::int64_t vv2 = static_cast<std::int64_t>(v2.mantissa());
@@ -733,6 +753,12 @@ STAmount::canonicalize()
mIsNative = false;
if (*stNumberSwitchover)
{
*this = iou();
return;
}
if (mValue == 0)
{
mOffset = -100;
@@ -1170,6 +1196,9 @@ multiply(STAmount const& v1, STAmount const& v2, Issue const& issue)
return STAmount(v1.getFName(), minV * maxV);
}
if (*stNumberSwitchover)
return {IOUAmount{Number{v1} * Number{v2}}, issue};
std::uint64_t value1 = v1.mantissa();
std::uint64_t value2 = v2.mantissa();
int offset1 = v1.exponent();