使用堆栈机使用Brouncker公式对PI进行逼近

我正在研究如何转换此公式以获得PI(基于Brouncker's)到堆栈计算机的近似值。

The formula I'm working with is 4/(1 + 1^2/(2 + 3^2/(2 + 5^2/(2 + 7^2/(2 + 9^2/2))))) which is roughly 2.97. How can I translate this to stack machine code?

这是我到目前为止的内容,但这是错误的:

DIV
PUSH 4
DIV
ADD
PUSH 1
POW
PUSH 1
PUSH 2
DIV
ADD
POW 
PUSH 3
PUSH 2
PUSH 2
DIV
ADD
POW
PUSH 5
PUSH 2
PUSH 2
DIV
ADD
POW
PUSH 7
PUSH 2
PUSH 2
ADD
DIV
POW
PUSH 9
PUSH 2
PUSH 2
PUSH 2

(从下至上阅读说明)

它结束时将4除以4.0909,但应将4除以1.344。 那里有一些数学语法是错误的,但是我尝试了却没有弄清楚我哪里错了...

如果需要,这是我为阅读说明而构建的类,它在C#中,但是应该被广泛理解:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Máquinas
{
    /**
    * @author toyomitsu
    *
    * @date - 2020.05.17 
    */
    public class StackMachine
    {
        private Stack<string> _Stack;
        private string[] _Instructions;

        public StackMachine(string[] Instructions)
        {
            Array.Reverse(Instructions);

            _Stack = new Stack<string>();
            _Instructions = Instructions;
        }

        public void PerformInstructions()
        {
            Console.WriteLine();
            Console.WriteLine("*** Stack machine ***");

            foreach (string Instruction in _Instructions)
            {
                string[] InstData = Instruction.Split(' ');

                switch (InstData[0])
                {
                    case "PUSH":
                        Push(InstData[1]);
                        break;

                    case "POP":
                        Pop();
                        break;

                    case "ADD":
                        Add();
                        break;

                    case "SUB":
                        Sub();
                        break;

                    case "MOD":
                        Mod();
                        break;

                    case "MUL":
                        Mul();
                        break;

                    case "DIV":
                        Div();
                        break;

                    case "POW":
                        Pow();
                        break;

                    case "SQR":
                        Sqr();
                        break;

                    case "EXP":
                        Exp();
                        break;

                    default:
                        continue;
                }

                List<string> Registry = _Stack.ToList<string>();

                Console.WriteLine();
                Console.WriteLine(string.Format("** Instruction: {0} **", Instruction));
                for (int i = 0; i < Registry.Count; i++)
                {
                    Console.WriteLine(string.Format("Registry {0}: {1}", i + 1, Registry[i]));
                }
                Console.WriteLine("**********************");
            }
        }

        #region Functions
        private void Push(string Data)
        {
            _Stack.Push(Data);
        }

        private string Pop()
        {
            return _Stack.Pop();
        }

        private void Add()
        {
            string Data1 = Pop();
            string Data2 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
            double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

            double Result = Num1 + Num2;
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Sub()
        {
            string Data1 = Pop();
            string Data2 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
            double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

            double Result = Num1 - Num2;
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Mul()
        {
            string Data1 = Pop();
            string Data2 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
            double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

            double Result = Num1 * Num2;
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Mod()
        {
            string Data1 = Pop();
            string Data2 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
            double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

            double Result = Num1 % Num2;
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Div()
        {
            string Data1 = Pop();
            string Data2 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
            double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

            double Result = Num1 / Num2;
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Pow()
        {
            string Data1 = Pop();
            string Data2 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
            double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

            double Result = Math.Pow(Num1, Num2);
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Sqr()
        {
            string Data1 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);

            double Result = Math.Sqrt(Num1);
            Push(Result.ToString(CultureInfo.InvariantCulture));
        }

        private void Exp()
        {
            string Data1 = Pop();

            double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);

            double Result = Math.Exp(Num1);
            Push(Result.ToString(CultureInfo.InvariantCulture));

        }
        #endregion
    }
}
评论