我想知道在python中检查数字x是否合理(两个整数n,m存在使得x = n/m)的好方法.
在Mathematica中,这是由函数完成的Rationalize[6.75]:27/4
我认为这个问题对于给定的准确性有一个答案.是否有获得这两个整数的通用算法?
我试图在仅接受整体买入/卖出金额的市场上进行与准确汇率相匹配的货币交易.我想以特定的速度进行最大规模的交易.这是一个玩具程序,而不是真正的交易机器人,所以我使用的是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 …Run Code Online (Sandbox Code Playgroud)