febb: (Default)
[personal profile] febb
Цепные дроби мне очень понравились.
Я написал небольшую программку чтобы аппроксимировать с их помощью.
Оттоптался на числе Пи, как обычно...



        private void ButtonCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                textBoxOut.Text = "";
                double val = double.Parse(textBoxInput.Text);
                double v = val;
                Fractions = new FracSeq();
                FracList frac = new FracList();
                int i = 0;
                for (; ; )
                {
                    long a = (long)v;
                    v = 1.0 / (v - (double)a);
                    frac.Insert(0, a);
                    // calculate whole fraction:
                    long n1 = 1, n2 = 0;
                    foreach (long f in frac)
                    {
                        long n = n1; n1 = n2; n2 = n;   // flip
                        n1 += n2 * f;
                    }
                    Frac fr = new Frac() { a = a, n1 = n1, n2 = n2 };
                    Fractions.Add(fr);
                    double app = (double)n1 / (double)n2;
                    double diff = val - app;
                    textBoxOut.Text += $"{i})\t{a}\t: {n1} / {n2}\t= {app}\tdiff: {diff}\r\n";
                    if (diff == 0.0)
                        break;
                    ++i;
                }//for(;;)
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

febb: (Default)
febb

March 2022

S M T W T F S
  1 2 345
6 7 89 101112
13141516171819
20212223242526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Mar. 10th, 2026 04:50 am
Powered by Dreamwidth Studios