Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12091


Ignore:
Timestamp:
02/27/15 10:42:46 (9 years ago)
Author:
pfleck
Message:

#2344

  • Added a toint and todouble function.
  • Changed the casts to the a more intelligent Convert.ToDouble/Int to avoid unboxing issues e.g. when casting a boxed int to double. Note that Convert.ToInt32 has a slightly different behavior than a int-cast (rounds when converting double->int), but this does not break any compatibility since we never converted to int before.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionModification/Calculator.cs

    r12012 r12091  
    9090          }
    9191        }
    92       }
    93       catch (Exception x) {
     92      } catch (Exception x) {
    9493        throw new Exception(string.Format(
    9594          "Calculation of '{1}'{0}failed at token #{2}: {3} {0}current stack is: {0}{4}", Environment.NewLine,
     
    104103      var result = stack.Pop();
    105104      if (result is string) return new StringValue((string)result);
     105      if (result is int) return new IntValue((int)result);
    106106      if (result is double) return new DoubleValue((double)result);
    107107      if (result is bool) return new BoolValue((bool)result);
     
    139139          break;
    140140
    141         case "log": Apply(stack, x => Math.Log((double)x)); break;
    142         case "+": Apply(stack, (x, y) => (double)x + (double)y); break;
    143         case "-": Apply(stack, (x, y) => (double)x - (double)y); break;
    144         case "*": Apply(stack, (x, y) => (double)x * (double)y); break;
    145         case "/": Apply(stack, (x, y) => (double)x / (double)y); break;
    146         case "^": Apply(stack, (x, y) => Math.Pow((double)x, (double)y)); break;
    147         case "<": Apply(stack, (x, y) => (double)x < (double)y); break;
    148         case ">": Apply(stack, (x, y) => (double)x > (double)y); break;
     141        case "log": Apply(stack, x => Math.Log(Convert.ToDouble(x))); break;
     142        case "+": Apply(stack, (x, y) => Convert.ToDouble(x) + Convert.ToDouble(y)); break;
     143        case "-": Apply(stack, (x, y) => Convert.ToDouble(x) - Convert.ToDouble(y)); break;
     144        case "*": Apply(stack, (x, y) => Convert.ToDouble(x) * Convert.ToDouble(y)); break;
     145        case "/": Apply(stack, (x, y) => Convert.ToDouble(x) / Convert.ToDouble(y)); break;
     146        case "^": Apply(stack, (x, y) => Math.Pow(Convert.ToDouble(x), Convert.ToDouble(y))); break;
     147        case "<": Apply(stack, (x, y) => Convert.ToDouble(x) < Convert.ToDouble(y)); break;
     148        case ">": Apply(stack, (x, y) => Convert.ToDouble(x) > Convert.ToDouble(y)); break;
     149
     150        case "toint": Apply(stack, x => Convert.ToInt32(x)); break;
     151        case "todouble": Apply(stack, x => Convert.ToDouble(x)); break;
    149152
    150153        case "==": Apply(stack, (x, y) => Equal(x, y)); break;
    151         case "not": Apply(stack, x => !(bool)x); break;
     154        case "not": Apply(stack, x => !Convert.ToBoolean(x)); break;
    152155        case "isnull": Apply(stack, x => x == null); break;
    153         case "if": Apply(stack, (then, else_, cond) => (bool)cond ? then : else_); break;
    154 
    155         case "ismatch": Apply(stack, (s, p) => new Regex((string)p).IsMatch((string)s)); break;
    156         case "rename": Apply(stack, (s, p, r) => new Regex((string)p).Replace((string)s, (string)r)); break;
     156        case "if": Apply(stack, (then, else_, cond) => Convert.ToBoolean(cond) ? then : else_); break;
     157
     158        case "ismatch": Apply(stack, (s, p) => new Regex(Convert.ToString(p)).IsMatch(Convert.ToString(s))); break;
     159        case "rename": Apply(stack, (s, p, r) => new Regex(Convert.ToString(p)).Replace(Convert.ToString(s), Convert.ToString(r))); break;
    157160
    158161        default: stack.Push(GetVariableValue(variables, token)); break;
     
    208211      try {
    209212        stack.Push(func(a));
    210       }
    211       catch (Exception) {
     213      } catch (Exception) {
    212214        stack.Push(a);
    213215        throw;
     
    222224      try {
    223225        stack.Push(func(a, b));
    224       }
    225       catch (Exception) {
     226      } catch (Exception) {
    226227        stack.Push(b);
    227228        stack.Push(a);
     
    238239      try {
    239240        stack.Push(func(a, b, c));
    240       }
    241       catch (Exception) {
     241      } catch (Exception) {
    242242        stack.Push(a);
    243243        stack.Push(b);
Note: See TracChangeset for help on using the changeset viewer.