Exemple #1
0
        private static bool SatisfyCalcCondition(
            Func <Expression, Expression, BinaryExpression> func,
            object x, object y, out object output)
        {
            output = null;
            if (func.Method.Name.Equals("Divide"))
            {
                return(false);
            }

            if (LogicSharp.IsNumeric(x) && LogicSharp.IsNumeric(y))
            {
                double xDoubleVal;
                double yDoubleVal;
                bool   isXDouble = LogicSharp.IsDouble(x, out xDoubleVal);
                bool   isYDouble = LogicSharp.IsDouble(y, out yDoubleVal);
                if (isXDouble || isYDouble)
                {
                    var xExpr = Expression.Constant(xDoubleVal);
                    var yExpr = Expression.Constant(yDoubleVal);
                    var rExpr = func(xExpr, yExpr);
                    output = Expression.Lambda <Func <double> >(rExpr).Compile().Invoke();
                    int iResult;
                    if (LogicSharp.IsInt(output, out iResult))
                    {
                        output = iResult;
                        return(true);
                    }
                    return(true);
                }
            }
            return(false);
        }
Exemple #2
0
        public static object Calculate(Func <Expression, Expression, BinaryExpression> func,
                                       object x, object y)
        {
            double xDoubleVal;
            double yDoubleVal;
            bool   isXDouble = LogicSharp.IsDouble(x, out xDoubleVal);
            bool   isYDouble = LogicSharp.IsDouble(y, out yDoubleVal);

            if (isXDouble || isYDouble)
            {
                var xExpr  = Expression.Constant(xDoubleVal);
                var yExpr  = Expression.Constant(yDoubleVal);
                var rExpr  = func(xExpr, yExpr);
                var result = Expression.Lambda <Func <double> >(rExpr).Compile().Invoke();

                int iResult;
                if (LogicSharp.IsInt(result, out iResult))
                {
                    return(iResult);
                }
                else
                {
                    return(result);
                }
            }
            else
            {
                return(null);
            }
        }
Exemple #3
0
        public override string ToString()
        {
            double dNum;
            bool   isNum = LogicSharp.IsDouble(Rhs, out dNum);

            if (isNum)
            {
                int  iNum;
                bool isInteger = LogicSharp.IsInt(Rhs, out iNum);
                if (isInteger)
                {
                    return(base.ToString());
                }

                double roundD = Math.Round(dNum, 4);
                return(string.Format("{0}={1}", Lhs, roundD));
            }
            return(base.ToString());
        }