21 static std::optional<PriceScale>
from_ratio(int64_t num, int64_t den)
noexcept {
22 if (num <= 0 || den <= 0) {
25 const int64_t g = std::gcd(num, den);
26 return PriceScale(num / g, den / g);
30 static PriceScale
identity() noexcept {
return PriceScale(1, 1); }
35 if (wire == WIRE_NULL_MAX || wire == WIRE_NULL_MIN || wire % den_ != 0) {
38 const int64_t q = wire / den_;
39 if (q > MAX_VALUE / num_ || q < -MAX_VALUE / num_) {
48 if (price ==
NULL_PRICE || price % num_ != 0) {
51 const int64_t q = price / num_;
52 if (q > MAX_VALUE / den_ || q < -MAX_VALUE / den_) {
59 [[nodiscard]] int64_t
numerator() const noexcept {
return num_; }
60 [[nodiscard]] int64_t
denominator() const noexcept {
return den_; }
63 PriceScale(int64_t num, int64_t den) noexcept : num_(num), den_(den) {}
65 static constexpr int64_t WIRE_NULL_MAX = std::numeric_limits<int64_t>::max();
66 static constexpr int64_t WIRE_NULL_MIN = std::numeric_limits<int64_t>::min();
68 static constexpr int64_t MAX_VALUE = std::numeric_limits<int64_t>::max() - 1;
int64_t numerator() const noexcept
int64_t denominator() const noexcept
bool to_price(int64_t wire, price_t &out) const noexcept
static std::optional< PriceScale > from_ratio(int64_t num, int64_t den) noexcept
static PriceScale identity() noexcept
bool to_wire(price_t price, int64_t &out) const noexcept
constexpr price_t NULL_PRICE