私は、完全なビッド/オファー金額のみを受け入れる市場で、正確なレートに一致する通貨取引を行おうとしています。特定のレートで最大の取引を可能にしたい。これはおもちゃのプログラムであり、実際の取引ボットではないため、C#を使用しています。
分子と分母が大きくなる可能性がある場合でも(100000+)、妥当な時間で答えを返すアルゴリズムが必要です。
static bool CalcBiggestRationalFraction(float target_real, float epsilon, int numerator_max, int denominator_max, out int numerator, out int denominator)
{
// target_real is the ratio we are tryig to achieve in our output fraction (numerator / denominator)
// epsilon is the largest difference abs(target_real - (numerator / denominator)) we are willing to tolerate in the answer
// numerator_max, denominator_max are the upper bounds on the numerator and the denominator in the answer
//
// in the case where there are multiple answers, we want to return the largest one
//
// in the case where an answer is found that is within epsilon, we return true and the answer.
// in the case where an answer is not found that is within epsilon, we return false and the closest answer that we have found.
//
// ex: CalcBiggestRationalFraction(.5, .001, 4, 4, num, denom) returns (2/4) instead of (1/2).
}
私が実際に何をしようとしているのかを考える前に、似たような前の質問(http://stackoverflow.com/questions/4385580/finding-the-closest-integer-fraction-to-a-given-random-real)をしました達成するために、私は別の、しかし関連する問題を解決しようとしていることがわかりました。