20 #ifndef RIPPLE_PROTOCOL_QUALITY_H_INCLUDED
21 #define RIPPLE_PROTOCOL_QUALITY_H_INCLUDED
23 #include <ripple/basics/IOUAmount.h>
24 #include <ripple/basics/XRPAmount.h>
25 #include <ripple/protocol/AmountConversions.h>
26 #include <ripple/protocol/STAmount.h>
43 template <
class In,
class Out>
52 TAmounts(In
const& in_, Out
const& out_) :
in(in_),
out(out_)
58 empty() const noexcept
60 return in <= beast::zero ||
out <= beast::zero;
64 operator+=(TAmounts
const& rhs)
72 operator-=(TAmounts
const& rhs)
83 using Amounts = TAmounts<STAmount, STAmount>;
85 template <
class In,
class Out>
87 operator==(TAmounts<In, Out>
const& lhs, TAmounts<In, Out>
const& rhs) noexcept
89 return lhs.in == rhs.in && lhs.out == rhs.out;
92 template <
class In,
class Out>
94 operator!=(TAmounts<In, Out>
const& lhs, TAmounts<In, Out>
const& rhs) noexcept
102 #define QUALITY_ONE 1'000'000'000
116 static const int minTickSize = 3;
117 static const int maxTickSize = 16;
133 explicit Quality(Amounts
const& amount);
136 template <
class In,
class Out>
137 Quality(Out
const& out, In
const& in)
171 round(
int tickSize)
const;
178 ceil_in(Amounts
const& amount, STAmount
const& limit)
const;
180 template <
class In,
class Out>
182 ceil_in(TAmounts<In, Out>
const& amount, In
const& limit)
const
184 if (amount.in <= limit)
191 auto const stRes = ceil_in(stAmt, stLim);
192 return TAmounts<In, Out>(
193 toAmount<In>(stRes.in), toAmount<Out>(stRes.out));
201 ceil_out(Amounts
const& amount, STAmount
const& limit)
const;
203 template <
class In,
class Out>
205 ceil_out(TAmounts<In, Out>
const& amount, Out
const& limit)
const
207 if (amount.out <= limit)
214 auto const stRes = ceil_out(stAmt, stLim);
215 return TAmounts<In, Out>(
216 toAmount<In>(stRes.in), toAmount<Out>(stRes.out));
224 operator<(Quality
const& lhs, Quality
const& rhs) noexcept
226 return lhs.m_value > rhs.m_value;
230 operator>(Quality
const& lhs, Quality
const& rhs) noexcept
232 return lhs.m_value < rhs.m_value;
236 operator<=(Quality
const& lhs, Quality
const& rhs) noexcept
242 operator>=(Quality
const& lhs, Quality
const& rhs) noexcept
248 operator==(Quality
const& lhs, Quality
const& rhs) noexcept
250 return lhs.m_value == rhs.m_value;
254 operator!=(Quality
const& lhs, Quality
const& rhs) noexcept
256 return !(lhs == rhs);
269 relativeDistance(Quality
const& q1, Quality
const& q2)
271 assert(q1.m_value > 0 && q2.m_value > 0);
273 if (q1.m_value == q2.m_value)
276 auto const [minV, maxV] =
std::minmax(q1.m_value, q2.m_value);
279 return rate & ~(255ull << (64 - 8));
282 return static_cast<int>(
rate >> (64 - 8)) - 100;
285 auto const minVMantissa = mantissa(minV);
286 auto const maxVMantissa = mantissa(maxV);
287 auto const expDiff = exponent(maxV) - exponent(minV);
289 double const minVD =
static_cast<double>(minVMantissa);
290 double const maxVD = expDiff ? maxVMantissa *
pow(10, expDiff)
291 : static_cast<double>(maxVMantissa);
296 return (maxVD - minVD) / minVD;