我试图通过使用连分数来满足您的要求。通过将深度限制为三个,我得到了一个合理的近似值。
我未能在合理的时间内提出迭代(或递归)方法。不过,我已经清理了一点。(我知道 3 个字母的变量名不好,但我想不出好名字:-/)
该代码为您提供了它可以找到的指定容差内的最佳有理近似值。得到的分数被减少并且是具有相同或更低分母的所有分数中的最佳近似值。
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
double value = rand.NextDouble();
var fraction = getFraction(value);
var numerator = fraction.Key;
var denominator = fraction.Value;
System.Console.WriteLine(string.Format("Value {0:0.0000} approximated by {1}/{2} = {3:0.0000}", value, numerator, denominator, (double)numerator / denominator));
}
/*
Output:
Value 0,4691 approximated by 8/17 = 0,4706
Value 0,0740 approximated by 1/14 = 0,0714
Value 0,7690 approximated by 3/4 = 0,7500
Value 0,7450 approximated by 3/4 = 0,7500
Value 0,3748 approximated by 3/8 = 0,3750
Value 0,7324 approximated by 3/4 = 0,7500
Value 0,5975 approximated by 3/5 = 0,6000
Value 0,7544 approximated by 3/4 = 0,7500
Value 0,7212 approximated by 5/7 = 0,7143
Value 0,0469 approximated by 1/21 = 0,0476
Value 0,2755 approximated by 2/7 = 0,2857
Value 0,8763 approximated by 7/8 = 0,8750
Value 0,8255 approximated by 5/6 = 0,8333
Value 0,6170 approximated by 3/5 = 0,6000
Value 0,3692 approximated by 3/8 = 0,3750
Value 0,8057 approximated by 4/5 = 0,8000
Value 0,3928 approximated by 2/5 = 0,4000
Value 0,0235 approximated by 1/43 = 0,0233
Value 0,8528 approximated by 6/7 = 0,8571
Value 0,4536 approximated by 5/11 = 0,4545
*/
}
private KeyValuePair<int, int> getFraction(double value, double tolerance = 0.02)
{
double f0 = 1 / value;
double f1 = 1 / (f0 - Math.Truncate(f0));
int a_t = (int)Math.Truncate(f0);
int a_r = (int)Math.Round(f0);
int b_t = (int)Math.Truncate(f1);
int b_r = (int) Math.Round(f1);
int c = (int)Math.Round(1 / (f1 - Math.Truncate(f1)));
if (Math.Abs(1.0 / a_r - value) <= tolerance)
return new KeyValuePair<int, int>(1, a_r);
else if (Math.Abs(b_r / (a_t * b_r + 1.0) - value) <= tolerance)
return new KeyValuePair<int, int>(b_r, a_t * b_r + 1);
else
return new KeyValuePair<int, int>(c * b_t + 1, c * a_t * b_t + a_t + c);
}
}