Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        public MathFuncNode VisitNumber([NotNull] NumberContext context)
        {
            string intPart = context.intPart != null ? context.intPart.Text : "0";
            string fracPart;
            string periodPart;

            if (context.fracTail() != null)
            {
                var fracTail = context.fracTail();
                fracPart   = fracTail.fracPart != null ? fracTail.fracPart.Text : "0";
                periodPart = fracTail.periodPart != null ? fracTail.periodPart.Text : "0";
            }
            else
            {
                fracPart   = "0";
                periodPart = "0";
            }

            var rational = Rational <long> .FromDecimal(intPart, fracPart, periodPart);

            return(new ValueNode(rational));
        }
Ejemplo n.º 3
0
        public ValueNode SimplifyValues(KnownFuncType?funcType, IList <ValueNode> args)
        {
            Rational <long> result;
            double          temp = 0.0;

            switch (funcType)
            {
            case KnownFuncType.Add:
                result = args[0].Value;
                for (int i = 1; i < args.Count; i++)
                {
                    result += args[i].Value;
                }
                return(new ValueNode(result));

            case KnownFuncType.Sub:
                result = args[0].Value;
                for (int i = 1; i < args.Count; i++)
                {
                    result -= args[i].Value;
                }
                return(new ValueNode(result));

            case KnownFuncType.Mult:
                result = args[0].Value;
                for (int i = 1; i < args.Count; i++)
                {
                    result *= args[i].Value;
                }
                return(new ValueNode(result));

            case KnownFuncType.Div:
                result = args[0].Value;
                for (int i = 1; i < args.Count; i++)
                {
                    result /= args[i].Value;
                }
                return(new ValueNode(result));

            case KnownFuncType.Pow:
                if (args[1].Value.ToDouble() == 0.5)
                {
                    temp = Math.Sqrt(args[0].Value.ToDouble());
                }
                else
                {
                    temp = Math.Pow(args[0].Value.ToDouble(), args[1].Value.ToDouble());
                }
                break;

            case KnownFuncType.Neg:
                return(new ValueNode(-args[0].Value));

            case KnownFuncType.Sgn:
                return(new ValueNode(new Rational <long>((long)Math.Sign(args[0].DoubleValue), 1, false)));

            case KnownFuncType.Trunc:
                return(new ValueNode(new Rational <long>((long)Math.Truncate(args[0].DoubleValue), 1, false)));

            case KnownFuncType.Round:
                return(new ValueNode(new Rational <long>((long)Math.Round(args[0].DoubleValue), 1, false)));

            case KnownFuncType.Diff:
                return(new ValueNode(0));

            case KnownFuncType.Sqrt:
                temp = Math.Sqrt(args[0].DoubleValue);
                break;

            case KnownFuncType.Sin:
                temp = Math.Sin(args[0].DoubleValue);
                break;

            case KnownFuncType.Cos:
                temp = Math.Cos(args[0].DoubleValue);
                break;

            case KnownFuncType.Tan:
                temp = Math.Tan(args[0].DoubleValue);
                break;

            case KnownFuncType.Cot:
                temp = 1 / Math.Tan(args[0].DoubleValue);
                break;

            case KnownFuncType.Arcsin:
                temp = Math.Asin(args[0].DoubleValue);
                break;

            case KnownFuncType.Arccos:
                temp = Math.Acos(args[0].DoubleValue);
                break;

            case KnownFuncType.Arctan:
                temp = Math.Atan(args[0].DoubleValue);
                break;

            case KnownFuncType.Arccot:
                temp = Math.PI / 2 - Math.Atan(args[0].DoubleValue);
                break;

            case KnownFuncType.Sinh:
                temp = Math.Sinh(args[0].DoubleValue);
                break;

            case KnownFuncType.Cosh:
                temp = Math.Cosh(args[0].DoubleValue);
                break;

            case KnownFuncType.Arcsinh:
                temp = Math.Log(args[0].DoubleValue + Math.Sqrt(args[0].DoubleValue * args[0].DoubleValue + 1));
                break;

            case KnownFuncType.Arcosh:
                temp = Math.Log(args[0].DoubleValue + Math.Sqrt(args[0].DoubleValue * args[0].DoubleValue - 1));
                break;

            case KnownFuncType.Exp:
                temp = Math.Exp(args[0].DoubleValue);
                break;

            case KnownFuncType.Ln:
                temp = Math.Log(args[0].DoubleValue);
                break;

            case KnownFuncType.Log10:
                temp = Math.Log10(args[0].DoubleValue);
                break;

            case KnownFuncType.Log:
                temp = Math.Log(args[0].DoubleValue, args[1].DoubleValue);
                break;

            case KnownFuncType.Abs:
                temp = Math.Abs(args[0].DoubleValue);
                break;

            default:
                return(null);
            }

            try
            {
                if (Rational <long> .FromDecimal((decimal)temp, out result, 14, false, 4, 8))
                {
                    return(new ValueNode(result));
                }
            }
            catch
            {
            }

            return(null);
        }