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:
seelabs
2016-03-21 12:02:00 -04:00
parent c9f1966e08
commit f254ebb4ca

View File

@@ -372,13 +372,23 @@ BookStep<TIn, TOut>::revImp (
} }
} }
switch(remainingOut.signum())
if (remainingOut < beast::zero)
{ {
// something went very wrong case -1:
assert (0); {
cache_.emplace (beast::zero, beast::zero); // something went very wrong
return {beast::zero, beast::zero}; JLOG (j_.error ())
<< "BookStep remainingOut < 0 " << to_string (remainingOut);
assert (0);
cache_.emplace (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);
@@ -460,12 +470,23 @@ BookStep<TIn, TOut>::fwdImp (
} }
} }
if (remainingIn < beast::zero) switch(remainingIn.signum())
{ {
// something went very wrong case -1:
assert (0); {
cache_.emplace (beast::zero, beast::zero); // something went very wrong
return {beast::zero, beast::zero}; JLOG (j_.error ())
<< "BookStep remainingIn < 0 " << to_string (remainingIn);
assert (0);
cache_.emplace (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);