108 double tick)
noexcept {
109 const double raw = is_buy ? best + distance : best - distance;
110 if (distance == 0.0 || !(tick > 0.0) || !std::isfinite(tick))
return raw;
111 const auto ladder_index = [&](
double price) {
112 const double k = std::round(
price / tick);
113 if (k * tick ==
price)
return k;
114 const double inverse = 1.0 / tick;
115 const double n = std::round(inverse);
116 if (n > 0.0 && std::abs(inverse - n) <= 1e-6 * n && k / n ==
price)
return k;
117 return std::numeric_limits<double>::quiet_NaN();
119 const double best_index = ladder_index(best);
120 const double distance_index = ladder_index(distance);
121 if (std::isnan(best_index) || std::isnan(distance_index) || !(distance_index >= 1.0))
123 const double index = is_buy ? best_index + distance_index : best_index - distance_index;
124 const double product = index * tick;
125 const double inverse = 1.0 / tick;
126 const double n = std::round(inverse);
127 if (!std::isfinite(product) || !(n > 0.0) || std::abs(inverse - n) > 1e-6 * n)
129 const double quotient = index / n;
130 if (!std::isfinite(quotient))
return product;
133 return is_buy ? std::min(product, quotient) : std::max(product, quotient);