Skip to content

Commit

Permalink
[FOLD] Address reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardHinnant committed Sep 27, 2022
1 parent 7e10665 commit be8e0c4
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 153 deletions.
29 changes: 15 additions & 14 deletions src/ripple/basics/impl/IOUAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ IOUAmount::minPositiveAmount()
void
IOUAmount::normalize()
{
if (mantissa_ == 0)
{
*this = beast::zero;
return;
}

if (*stNumberSwitchover)
{
Number v{mantissa_, exponent_};
Expand All @@ -56,11 +62,6 @@ IOUAmount::normalize()
*this = beast::zero;
return;
}
if (mantissa_ == 0)
{
*this = beast::zero;
return;
}

bool const negative = (mantissa_ < 0);

Expand Down Expand Up @@ -107,21 +108,21 @@ IOUAmount::IOUAmount(Number const& other)
IOUAmount&
IOUAmount::operator+=(IOUAmount const& other)
{
if (other == beast::zero)
return *this;

if (*this == beast::zero)
{
*this = other;
return *this;
}

if (*stNumberSwitchover)
{
*this = IOUAmount{Number{*this} + Number{other}};
}
else
{
if (other == beast::zero)
return *this;

if (*this == beast::zero)
{
*this = other;
return *this;
}

auto m = other.mantissa_;
auto e = other.exponent_;

Expand Down
51 changes: 27 additions & 24 deletions src/ripple/basics/impl/Number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <type_traits>
#include <utility>

#ifdef _MSVC_LANG
#ifdef BOOST_COMP_MSVC
#include <boost/multiprecision/cpp_int.hpp>
using uint128_t = boost::multiprecision::uint128_t;
#else // !defined(_MSVC_LANG)
Expand Down Expand Up @@ -130,34 +130,37 @@ int
Number::Guard::round() noexcept
{
auto mode = Number::getround();
switch (mode)

if (mode == towards_zero)
return -1;

if (mode == downward)
{
// round to nearest if mode is not one of the predefined values
default:
case to_nearest:
if (digits_ > 0x5000'0000'0000'0000)
return 1;
if (digits_ < 0x5000'0000'0000'0000)
return -1;
if (xbit_)
return 1;
return 0;
case towards_zero:
return -1;
case downward:
if (sbit_)
{
if (digits_ > 0 || xbit_)
return 1;
}
return -1;
case upward:
if (sbit_)
return -1;
if (sbit_)
{
if (digits_ > 0 || xbit_)
return 1;
}
return -1;
}

if (mode == upward)
{
if (sbit_)
return -1;
if (digits_ > 0 || xbit_)
return 1;
return -1;
}

// assume round to nearest if mode is not one of the predefined values
if (digits_ > 0x5000'0000'0000'0000)
return 1;
if (digits_ < 0x5000'0000'0000'0000)
return -1;
if (xbit_)
return 1;
return 0;
}

// Number
Expand Down
62 changes: 39 additions & 23 deletions src/test/app/Offer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,37 +2087,53 @@ class Offer_test : public beast::unit_test::suite

using namespace jtx;

Env env{*this, features};
for (auto NumberSwitchOver : {false, true})
{
Env env{*this, features};
if (NumberSwitchOver)
env.enableFeature(fixUniversalNumber);
else
env.disableFeature(fixUniversalNumber);

auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const USD = gw["USD"];
auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const USD = gw["USD"];

env.fund(XRP(10000), gw, alice, bob);
env.fund(XRP(10000), gw, alice, bob);

env(rate(gw, 1.005));
env(rate(gw, 1.005));

env(trust(alice, USD(1000)));
env(trust(bob, USD(1000)));
env(trust(gw, alice["USD"](50)));
env(trust(alice, USD(1000)));
env(trust(bob, USD(1000)));
env(trust(gw, alice["USD"](50)));

env(pay(gw, bob, bob["USD"](1)));
env(pay(alice, gw, USD(50)));
env(pay(gw, bob, bob["USD"](1)));
env(pay(alice, gw, USD(50)));

env(trust(gw, alice["USD"](0)));
env(trust(gw, alice["USD"](0)));

env(offer(alice, USD(50), XRP(150000)));
env(offer(bob, XRP(100), USD(0.1)));
env(offer(alice, USD(50), XRP(150000)));
env(offer(bob, XRP(100), USD(0.1)));

auto jrr = ledgerEntryState(env, alice, gw, "USD");
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"49.96666666666667");
jrr = ledgerEntryState(env, bob, gw, "USD");
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.9665000000333333");
auto jrr = ledgerEntryState(env, alice, gw, "USD");
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"49.96666666666667");
jrr = ledgerEntryState(env, bob, gw, "USD");
if (NumberSwitchOver)
{
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.9665000000333333");
}
else
{
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.966500000033334");
}
}
}

void
Expand Down
Loading

0 comments on commit be8e0c4

Please sign in to comment.