コード例 #1
0
        public void Converting_from_decimal_and_back_again_returns_the_original_value(decimal x)
        {
            var accuracy = 1E-28m;

            Assert.True(Math.Abs(x - (decimal)(Rational)x) <= accuracy);
            Assert.True(Math.Abs(x - Rational.ToDecimal(Rational.FromDecimal(x))) <= accuracy);
        }
コード例 #2
0
        public void DecimalPlacesTest()
        {
            Assert.IsTrue(Rational <long> .FromDecimal(0.33333333m, out Rational <long> r, 8, false));
            Assert.IsTrue(new Rational <long>(1, 3) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.33333333m, out r, 9, false));
            Assert.IsTrue(new Rational <long>(33333333, 100000000) == r);
        }
コード例 #3
0
        public void DigitsForRealTest()
        {
            Assert.IsTrue(Rational <long> .FromDecimal(0.12345678m, out Rational <long> r, 28, true, 2, 9));
            Assert.IsTrue(new Rational <long>(12345678, 100000000) == r);

            Assert.IsFalse(Rational <long> .FromDecimal(0.12345678m, out r, 28, true, 2, 8));

            Assert.IsTrue(Rational <long> .FromDecimal(0.12121212121212121m, out r, 28, true, 2, 9));
            Assert.IsTrue(new Rational <long>(4, 33) == r);
        }
コード例 #4
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj is string)
            {
                if ((string)obj == Value.ToString())
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            if (obj is double || obj is decimal)
            {
                Rational <long> r;
                if (Rational <long> .FromDecimal((decimal)obj, out r) && r == Value)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            if (obj is int)
            {
                if ((int)obj == Value)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            var v = obj as ValueNode;

            if (v != null)
            {
                return(v.Value == Value);
            }
            else
            {
                return(false);
            }
        }
コード例 #5
0
        public void TrimEndZeroesTest()
        {
            Assert.IsTrue(Rational <long> .FromDecimal(0.33m, out Rational <long> r, 28, true));
            Assert.IsTrue(new Rational <long>(1, 3) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.33m, out r, 28, false));
            Assert.IsTrue(new Rational <long>(33, 100) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.33m, out r, 2, false));
            Assert.IsTrue(new Rational <long>(1, 3) == r);
        }
コード例 #6
0
        public void MinPeriodRepeatTest()
        {
            Assert.IsTrue(Rational <long> .FromDecimal(0.123412m, out Rational <long> r, 28, true, 1.5m));
            Assert.IsTrue(new Rational <long>(1234, 9999) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.123412m, out r, 28, false, 1.5m));
            Assert.IsTrue(new Rational <long>(123412, 1000000) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.123412m, out r, 6, false, 1.5m));
            Assert.IsTrue(new Rational <long>(1234, 9999) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.123412m, out r, 28, true, 1.6m));
            Assert.IsTrue(new Rational <long>(123412, 1000000) == r);
        }
コード例 #7
0
        public void ConversionTest()
        {
            Rational <long> r;

            Assert.IsTrue(Rational <long> .FromDecimal(0.0000000000000000000000000000m, out r));
            Assert.IsTrue(new Rational <long>(0, 1) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(53.0000000000000000000000000000m, out r));
            Assert.IsTrue(new Rational <long>(53, 1) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.3333333333333333333333333333m, out r));
            Assert.IsTrue(new Rational <long>(1, 3) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.5678333333333333333333333333m, out r));
            Assert.IsTrue(new Rational <long>(3407, 6000) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.8888888888888888888888888889m, out r));
            Assert.IsTrue(new Rational <long>(8, 9) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.9889889889889889889889889890m, out r));
            Assert.IsTrue(new Rational <long>(988, 999) == r);
            Assert.IsTrue(Rational <long> .FromDecimal(988m / 999, out r));
            Assert.IsTrue(new Rational <long>(988, 999) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.9899899899899899899899899900m, out r));
            Assert.IsTrue(new Rational <long>(989, 999) == r);
            Assert.IsTrue(Rational <long> .FromDecimal(989m / 999, out r));
            Assert.IsTrue(new Rational <long>(989, 999) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.9989989989989989989989989990m, out r));
            Assert.IsTrue(new Rational <long>(998, 999) == r);
            Assert.IsTrue(Rational <long> .FromDecimal(998m / 999, out r));
            Assert.IsTrue(new Rational <long>(998, 999) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(0.9999999999999999999999999999m, out r));
            Assert.IsTrue(new Rational <long>(1, 1) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(20.0m / 3, out r));
            Assert.IsTrue(new Rational <long>(20, 3) == r);

            Assert.IsTrue(Rational <long> .FromDecimal(-9.1234567856785678567856785678m, out r, 26));
            Assert.IsTrue(new Rational <long>(-228063611, 24997500) == r);
        }
コード例 #8
0
    private void CreateNewObject(Reduction r)
    {
        MathFuncNode arg1, arg2;

        var tableIndex = (ProductionIndex)r.Parent.TableIndex();

        switch (tableIndex)
        {
        case ProductionIndex.Statements:
            // <Statements> ::= <Statements> <Devider> <Statement>
            break;

        case ProductionIndex.Statements2:
            // <Statements> ::= <Statements> <Devider>
            break;

        case ProductionIndex.Statements3:
            // <Statements> ::= <Statement>
            //Funcs.Push(new MathFunc(Args.Pop()));
            break;

        case ProductionIndex.Devider_Semi:
            // <Devider> ::= ';'
            break;

        case ProductionIndex.Devider_Dot:
            // <Devider> ::= '.'
            break;

        case ProductionIndex.Statement_Eq:
            // <Statement> ::= <Expression> '=' <Expression>
            arg2 = Nodes.Pop();
            arg1 = Nodes.Pop();
            Funcs.Push(new MathFunc(arg1, arg2, null, Parameters.Select(p => p.Value)));
            Parameters.Clear();
            break;

        case ProductionIndex.Statement:
            // <Statement> ::= <Expression>
            Funcs.Push(new MathFunc(Nodes.Pop(), null, Parameters.Select(p => p.Value)));
            break;

        case ProductionIndex.Expression:
            // <Expression> ::= <FuncDef>
            break;

        case ProductionIndex.Funcdef_Id_Lparan_Rparan:
            // <FuncDef> ::= Id '(' <ExpressionList> ')'

            PushFunction(r[0].Data.ToString());
            break;

        case ProductionIndex.Funcdef_Id_Apost_Lparan_Rparan:
        // <FuncDef> ::= Id '' '(' <ExpressionList> ')'

        case ProductionIndex.Funcdef_Id_Lparan_Rparan_Apost:

            // <FuncDef> ::= Id '(' <ExpressionList> ')' ''

            PushFunction(r[0].Data.ToString());
            Nodes.Push(new FuncNode(KnownFuncType.Diff, new MathFuncNode[] { Nodes.Pop(), null }));

            break;

        case ProductionIndex.Expressionlist_Comma:
            ArgsCount[ArgsCount.Count - 1]++;
            break;

        case ProductionIndex.Expressionlist:
            ArgsCount.Add(1);
            ArgsFuncTypes.Add(null);
            break;

        case ProductionIndex.Expression2:

            // <Expression> ::= <Addition>
            if (AdditionMultiChilds)
            {
                PushOrRemoveFunc(KnownFuncType.Add);
            }

            break;

        case ProductionIndex.Addition_Addliteral:

            // <Addition> ::= <Addition> AddLiteral <Multiplication>
            if (MultiplicationMultiChilds)
            {
                PushOrRemoveFunc(KnownFuncType.Mult);
            }

            if (AdditionMultiChilds)
            {
                ArgsCount[ArgsCount.Count - 1]++;
                if (KnownFunc.BinaryNamesFuncs[r[1].Data.ToString()] == KnownFuncType.Sub)
                {
                    Nodes.Push(new FuncNode(KnownFuncType.Neg, new MathFuncNode[] { Nodes.Pop() }));
                }
            }
            else
            {
                PushBinaryFunction(r[1].Data.ToString());
            }
            break;

        case ProductionIndex.Addition_Addliteral2:

            // <Addition> ::= <Addition> AddLiteral <FuncDef>

            if (AdditionMultiChilds)
            {
                ArgsCount[ArgsCount.Count - 1]++;
                if (KnownFunc.BinaryNamesFuncs[r[1].Data.ToString()] == KnownFuncType.Sub)
                {
                    Nodes.Push(new FuncNode(KnownFuncType.Neg, new MathFuncNode[] { Nodes.Pop() }));
                }
            }
            else
            {
                PushBinaryFunction(r[1].Data.ToString());
            }
            break;

        case ProductionIndex.Addition_Addliteral3:

            // <Addition> ::= <FuncDef> AddLiteral <Multiplication>
            if (MultiplicationMultiChilds)
            {
                PushOrRemoveFunc(KnownFuncType.Mult);
            }

            if (AdditionMultiChilds)
            {
                PushFunc(KnownFuncType.Add, 2);
                if (KnownFunc.BinaryNamesFuncs[r[1].Data.ToString()] == KnownFuncType.Sub)
                {
                    Nodes.Push(new FuncNode(KnownFuncType.Neg, new MathFuncNode[] { Nodes.Pop() }));
                }
            }
            else
            {
                PushBinaryFunction(r[1].Data.ToString());
            }
            break;

        case ProductionIndex.Addition_Addliteral4:

            // <Addition> ::= <FuncDef> AddLiteral <FuncDef>

            if (AdditionMultiChilds)
            {
                PushFunc(KnownFuncType.Add, 2);
                if (KnownFunc.BinaryNamesFuncs[r[1].Data.ToString()] == KnownFuncType.Sub)
                {
                    Nodes.Push(new FuncNode(KnownFuncType.Neg, new MathFuncNode[] { Nodes.Pop() }));
                }
            }
            else
            {
                PushBinaryFunction(r[1].Data.ToString());
            }
            break;

        case ProductionIndex.Addition:

            // <Addition> ::= <Multiplication>

            if (MultiplicationMultiChilds)
            {
                PushOrRemoveFunc(KnownFuncType.Mult);
            }

            if (AdditionMultiChilds)
            {
                PushFunc(KnownFuncType.Add);
            }

            break;

        case ProductionIndex.Multiplication_Multliteral:

        // <Multiplication> ::= <Multiplication> MultLiteral <Exponentiation>
        case ProductionIndex.Multiplication_Multliteral2:
            // <Multiplication> ::= <Multiplication> MultLiteral <FuncDef>

            if (MultiplicationMultiChilds)
            {
                if (KnownFunc.BinaryNamesFuncs[r[1].Data.ToString()] == KnownFuncType.Div)
                {
                    Nodes.Push(new FuncNode(KnownFuncType.Pow, new MathFuncNode[] { Nodes.Pop(), new ValueNode(-1) }));
                }
                ArgsCount[ArgsCount.Count - 1]++;
            }
            else
            {
                PushBinaryFunction(r[1].Data.ToString());
            }
            break;

        case ProductionIndex.Multiplication:

            // <Multiplication> ::= <Exponentiation>

            if (MultiplicationMultiChilds)
            {
                PushFunc(KnownFuncType.Mult);
            }
            break;

        case ProductionIndex.Multiplication_Multliteral3:
        // <Multiplication> ::= <FuncDef> MultLiteral <Exponentiation>
        case ProductionIndex.Multiplication_Multliteral4:
            // <Multiplication> ::= <FuncDef> MultLiteral <FuncDef>

            if (MultiplicationMultiChilds)
            {
                PushFunc(KnownFuncType.Mult, 2);

                if (KnownFunc.BinaryNamesFuncs[r[1].Data.ToString()] == KnownFuncType.Div)
                {
                    Nodes.Push(new FuncNode(KnownFuncType.Pow, new MathFuncNode[] { Nodes.Pop(), new ValueNode(-1) }));
                }
            }
            else
            {
                PushBinaryFunction(r[1].Data.ToString());
            }
            break;

        case ProductionIndex.Exponentiation_Caret:
        // <Exponentiation> ::= <Exponentiation> '^' <Negation>
        case ProductionIndex.Exponentiation_Caret2:
        // <Exponentiation> ::= <Exponentiation> '^' <FuncDef>
        case ProductionIndex.Exponentiation_Caret3:
        // <Exponentiation> ::= <FuncDef> '^' <Negation>
        case ProductionIndex.Exponentiation_Caret4:
            // <Exponentiation> ::= <FuncDef> '^' <FuncDef>

            PushBinaryFunction(r[1].Data.ToString());
            break;

        case ProductionIndex.Exponentiation:
            // <Exponentiation> ::= <Negation>
            break;

        case ProductionIndex.Negation_Addliteral:
        // <Negation> ::= AddLiteral <Value>
        case ProductionIndex.Negation_Addliteral2:
            // <Negation> ::= AddLiteral <FuncDef>
            if (Nodes.Peek().Type == MathNodeType.Value)
            {
                if (r[0].Data.ToString() == "-")
                {
                    Nodes.Push(new ValueNode(-((ValueNode)Nodes.Pop()).Value));
                }
            }
            else
            {
                Nodes.Push(new FuncNode(r[0].Data.ToString(), new MathFuncNode[] { Nodes.Pop() }));
            }
            break;

        case ProductionIndex.Negation:

            // <Negation> ::= <Value>
            break;

        case ProductionIndex.Value_Id:

            // <Value> ::= Id
            var       id = r[0].Data.ToString();
            ConstNode idValue;
            if (Parameters.TryGetValue(id, out idValue))
            {
                Nodes.Push(idValue);
            }
            else
            {
                var newConst = new ConstNode(id);
                Nodes.Push(newConst);
                Parameters.Add(id, newConst);
            }
            break;

        case ProductionIndex.Value_Number1:
        // <Value> ::= 'Number1'
        case ProductionIndex.Value_Number2:
            // <Value> ::= 'Number2'

            try
            {
                var    str        = r[0].Data.ToString();
                var    dotInd     = str.IndexOf('.');
                string intPart    = dotInd == 0 ? "0" : str.Substring(0, dotInd == -1 ? str.Length : dotInd);
                string fracPart   = null;
                string periodPart = null;
                if (dotInd != -1)
                {
                    int braceInd = str.IndexOf('(', dotInd + 1);
                    if (braceInd == -1)
                    {
                        fracPart = str.Substring(dotInd + 1, str.Length - dotInd - 1);
                    }
                    else
                    {
                        fracPart   = str.Substring(dotInd + 1, braceInd - dotInd - 1);
                        periodPart = str.Substring(braceInd + 1, str.Length - braceInd - 2);
                    }
                }
                var result = Rational <long> .FromDecimal(intPart, fracPart, periodPart);

                Nodes.Push(new ValueNode(result));
            }
            catch
            {
                throw new ArgumentException();
            }
            break;

        case ProductionIndex.Value_Lparan_Rparan:

            // <Value> ::= '(' <Expression> ')'
            break;

        case ProductionIndex.Value_Pipe_Pipe:

            // <Value> ::= '|' <Expression> '|'
            Nodes.Push(new FuncNode(KnownFuncType.Abs, new MathFuncNode[] { Nodes.Pop() }));
            break;

        case ProductionIndex.Value_Lparan_Rparan_Apost:

            // <Value> ::= '(' <Expression> ')' ''
            Nodes.Push(new FuncNode(KnownFuncType.Diff, new MathFuncNode[] { Nodes.Pop(), null }));
            break;

        case ProductionIndex.Value_Pipe_Pipe_Apost:

            // <Value> ::= '|' <Expression> '|' ''
            Nodes.Push(new FuncNode(KnownFuncType.Diff, new MathFuncNode[] {
                new FuncNode(KnownFuncType.Abs, new MathFuncNode[] { Nodes.Pop() }), null
            }));
            break;

            /*case ProductionIndex.Value_Id_Apost:
             *
             *      // <Value> ::= Id ''
             *      Args.Push(new FuncNode(KnownMathFunctionType.Diff, null,
             *              new MathFunctionNode[] {
             *                      new FuncNode(r[0].Data.ToString(), null, new MathFunctionNode[] { null }), null }));
             *      break;*/
        }          //switch
    }
コード例 #9
0
 public void Converting_to_decimal_and_back_again_returns_the_original_value(Rational x)
 {
     Assert.Equal(x, (Rational)(decimal)x);
     Assert.Equal(x, Rational.FromDecimal(Rational.ToDecimal(x)));
 }