Min function

Topics: Help
Apr 23, 2014 at 8:13 AM
Hi all,
I've found this strange behaviour of the evaluation engine:

    public Sub doTest()
        Dim exp As Expression = New Expression("Min(6, 0.33 + 1.8)")
        Debug.Print(exp.Evaluate)
        Dim exp2 As Expression = New Expression("Min(0.33 + 1.8 , 6)")
        Debug.Print(exp2.Evaluate)
    End Sub
the result is:
Information: 0 : Expression added to cache: Min(6, 0.33 + 1.8)
2
Information: 0 : Expression added to cache: Min(0.33 + 1.8 , 6)
2,13

Any idea?
May 12, 2014 at 2:31 PM
Edited May 13, 2014 at 1:01 PM
Hi,

You need to perform the "cast" individually for each parameter.

To fix the problem, change the "Min" routine in "Numbers.cs" as follows:
            switch (typeCodeA)
            {
                case TypeCode.Byte:
                    switch (typeCodeB)
                    {
                        case TypeCode.Byte:
                            return Math.Min((Byte) a, (Byte) b);
                        case TypeCode.SByte:
                            return Math.Min((Byte) a, (SByte) b);
                        case TypeCode.Int16:
                            return Math.Min((Byte) a, (Int16) b);
                        case TypeCode.UInt16:
                            return Math.Min((Byte) a, (UInt16) b);
                        case TypeCode.Int32:
                            return Math.Min((Byte) a, (Int32) b);
                        case TypeCode.UInt32:
                            return Math.Min((Byte) a, (UInt32) b);
                        case TypeCode.Int64:
                            return Math.Min((Byte) a, (Int64) b);
                        case TypeCode.UInt64:
                            return Math.Min((Byte) a, (UInt64) b);
                        case TypeCode.Single:
                            return Math.Min((Byte) a, (Single) b);
                        case TypeCode.Double:
                            return Math.Min((Byte) a, (Double) b);
                        case TypeCode.Decimal:
                            return Math.Min((Byte) a, (Decimal) b);
                    }
                    break;
                case TypeCode.SByte:
                    switch (typeCodeB)
                    {
                        case TypeCode.Byte:
                            return Math.Min((SByte) a, (Byte) b);
                        case TypeCode.SByte:
                            return Math.Min((SByte) a, (SByte) b);
                        case TypeCode.Int16:
                            return Math.Min((SByte) a, (Int16) b);
                        case TypeCode.UInt16:
                            return Math.Min((SByte) a, (UInt16) b);
                        case TypeCode.Int32:
                            return Math.Min((SByte) a, (Int32) b);
                        case TypeCode.UInt32:
                            return Math.Min((SByte) a, (UInt32) b);
                        case TypeCode.Int64:
                            return Math.Min((SByte) a, (Int64) b);
                        case TypeCode.UInt64:
                            throw new InvalidOperationException(
                                "Operator '%' can't be applied to operands of types 'sbyte' and 'ulong'");
                        case TypeCode.Single:
                            return Math.Min((SByte) a, (Single) b);
                        case TypeCode.Double:
                            return Math.Min((SByte) a, (Double) b);
                        case TypeCode.Decimal:
                            return Math.Min((SByte) a, (Decimal) b);
                    }
                    break;

                ...

            }
            return null;