mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-13 15:45:54 +00:00
Improve BookStep precision:
A computation like: `amount_remaining = amount_wanted - amount_got`, can leave `amount_remaining == 0` without `amount_wanted == amount_got`. This happens if the amounts differ by less than the smallest representable value. Fix BookStep to handle this case.
This commit is contained in:
@@ -372,14 +372,24 @@ BookStep<TIn, TOut>::revImp (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch(remainingOut.signum())
|
||||||
if (remainingOut < beast::zero)
|
{
|
||||||
|
case -1:
|
||||||
{
|
{
|
||||||
// something went very wrong
|
// something went very wrong
|
||||||
|
JLOG (j_.error ())
|
||||||
|
<< "BookStep remainingOut < 0 " << to_string (remainingOut);
|
||||||
assert (0);
|
assert (0);
|
||||||
cache_.emplace (beast::zero, beast::zero);
|
cache_.emplace (beast::zero, beast::zero);
|
||||||
return {beast::zero, beast::zero};
|
return {beast::zero, beast::zero};
|
||||||
}
|
}
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
// due to normalization, remainingOut can be zero without
|
||||||
|
// result.out == out. Force result.out == out for this case
|
||||||
|
result.out = out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cache_.emplace (result.in, result.out);
|
cache_.emplace (result.in, result.out);
|
||||||
return {result.in, result.out};
|
return {result.in, result.out};
|
||||||
@@ -460,13 +470,24 @@ BookStep<TIn, TOut>::fwdImp (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remainingIn < beast::zero)
|
switch(remainingIn.signum())
|
||||||
|
{
|
||||||
|
case -1:
|
||||||
{
|
{
|
||||||
// something went very wrong
|
// something went very wrong
|
||||||
|
JLOG (j_.error ())
|
||||||
|
<< "BookStep remainingIn < 0 " << to_string (remainingIn);
|
||||||
assert (0);
|
assert (0);
|
||||||
cache_.emplace (beast::zero, beast::zero);
|
cache_.emplace (beast::zero, beast::zero);
|
||||||
return {beast::zero, beast::zero};
|
return {beast::zero, beast::zero};
|
||||||
}
|
}
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
// due to normalization, remainingIn can be zero without
|
||||||
|
// result.in == in. Force result.in == in for this case
|
||||||
|
result.in = in;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cache_.emplace (result.in, result.out);
|
cache_.emplace (result.in, result.out);
|
||||||
return {result.in, result.out};
|
return {result.in, result.out};
|
||||||
|
|||||||
Reference in New Issue
Block a user