фЕБЪ (febb) wrote,
фЕБЪ
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");
            }
        }

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments