Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/FloatExpressions.cs
- Timestamp:
- 04/18/17 01:15:25 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/FloatExpressions.cs
r14834 r14875 10 10 /// </summary> 11 11 [PushExpression(StackTypes.Float, "FLOAT.+")] 12 public class FloatAddExpression : PushResultExpression<double> { 13 public override bool Eval(IInternalPushInterpreter interpreter) { 14 return Eval(interpreter.FloatStack, 2, 15 values => { 16 var result = values[0] + values[1]; 17 18 if (double.IsPositiveInfinity(result)) return double.MaxValue; 19 if (double.IsNegativeInfinity(result)) return double.MinValue; 20 21 return result; 22 }); 12 public class FloatAddExpression : StatelessExpression { 13 public override bool Eval(IInternalPushInterpreter interpreter) { 14 if (interpreter.FloatStack.Count < 2) 15 return false; 16 17 var first = interpreter.FloatStack.Pop(); 18 var second = interpreter.FloatStack.Top; 19 var result = second + first; 20 21 if (double.IsPositiveInfinity(result)) result = double.MaxValue; 22 if (double.IsNegativeInfinity(result)) result = double.MinValue; 23 24 interpreter.FloatStack.SetTop(result); 25 return true; 23 26 } 24 27 } … … 28 31 /// </summary> 29 32 [PushExpression(StackTypes.Float, "FLOAT.-")] 30 public class FloatSubtractExpression : PushResultExpression<double> { 31 public override bool Eval(IInternalPushInterpreter interpreter) { 32 return Eval(interpreter.FloatStack, 2, 33 values => { 34 var result = values[0] - values[1]; 35 36 if (double.IsPositiveInfinity(result)) return double.MaxValue; 37 if (double.IsNegativeInfinity(result)) return double.MinValue; 38 39 return result; 40 }); 33 public class FloatSubtractExpression : StatelessExpression { 34 public override bool Eval(IInternalPushInterpreter interpreter) { 35 if (interpreter.FloatStack.Count < 2) 36 return false; 37 38 var first = interpreter.FloatStack.Pop(); 39 var second = interpreter.FloatStack.Top; 40 var result = second - first; 41 42 if (double.IsPositiveInfinity(result)) result = double.MaxValue; 43 if (double.IsNegativeInfinity(result)) result = double.MinValue; 44 45 interpreter.FloatStack.SetTop(result); 46 return true; 41 47 } 42 48 } … … 46 52 /// </summary> 47 53 [PushExpression(StackTypes.Float, "FLOAT.*")] 48 public class FloatMultiplyExpression : PushResultExpression<double> { 49 public override bool Eval(IInternalPushInterpreter interpreter) { 50 return Eval(interpreter.FloatStack, 2, 51 values => { 52 var result = values[0] * values[1]; 53 54 if (double.IsPositiveInfinity(result)) return double.MaxValue; 55 if (double.IsNegativeInfinity(result)) return double.MinValue; 56 57 return result; 58 }); 54 public class FloatMultiplyExpression : StatelessExpression { 55 public override bool Eval(IInternalPushInterpreter interpreter) { 56 if (interpreter.FloatStack.Count < 2) 57 return false; 58 59 var first = interpreter.FloatStack.Pop(); 60 var second = interpreter.FloatStack.Top; 61 var result = second * first; 62 63 if (double.IsPositiveInfinity(result)) result = double.MaxValue; 64 if (double.IsNegativeInfinity(result)) result = double.MinValue; 65 66 interpreter.FloatStack.SetTop(result); 67 return true; 59 68 } 60 69 } … … 65 74 /// </summary> 66 75 [PushExpression(StackTypes.Float, "FLOAT./")] 67 public class FloatDivideExpression : PushResultExpression<double> { 68 public override bool Eval(IInternalPushInterpreter interpreter) { 69 return Eval(interpreter.FloatStack, 2, values => { 70 var result = values[0] / values[1]; 71 72 if (double.IsPositiveInfinity(result)) return double.MaxValue; 73 if (double.IsNegativeInfinity(result)) return double.MinValue; 74 75 return result; 76 }, 0); 76 public class FloatDivideExpression : StatelessExpression { 77 public override bool Eval(IInternalPushInterpreter interpreter) { 78 if (interpreter.FloatStack.Count < 2 || 79 interpreter.FloatStack.Top.IsAlmost(0)) 80 return false; 81 82 var first = interpreter.FloatStack.Pop(); 83 var second = interpreter.FloatStack.Top; 84 var result = second / first; 85 86 if (double.IsPositiveInfinity(result)) result = double.MaxValue; 87 if (double.IsNegativeInfinity(result)) result = double.MinValue; 88 89 interpreter.FloatStack.SetTop(result); 90 return true; 77 91 } 78 92 } … … 86 100 /// </summary> 87 101 [PushExpression(StackTypes.Float, "FLOAT.%")] 88 public class FloatModuloExpression : PushResultExpression<double> { 89 public override bool Eval(IInternalPushInterpreter interpreter) { 90 return Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0); 102 public class FloatModuloExpression : StatelessExpression { 103 public override bool Eval(IInternalPushInterpreter interpreter) { 104 if (interpreter.FloatStack.Count < 2 || 105 interpreter.FloatStack.Top.IsAlmost(0)) 106 return false; 107 108 var first = interpreter.FloatStack.Pop(); 109 var second = interpreter.FloatStack.Top; 110 var result = second % first; 111 112 interpreter.FloatStack.SetTop(result); 113 return true; ; 91 114 } 92 115 } … … 96 119 /// </summary> 97 120 [PushExpression(StackTypes.Float, "FLOAT.MIN")] 98 public class FloatMinExpression : PushResultExpression<double> { 99 public override bool Eval(IInternalPushInterpreter interpreter) { 100 return Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1])); 121 public class FloatMinExpression : StatelessExpression { 122 public override bool Eval(IInternalPushInterpreter interpreter) { 123 if (interpreter.FloatStack.Count < 2) 124 return false; 125 126 var first = interpreter.FloatStack.Pop(); 127 var second = interpreter.FloatStack.Top; 128 var result = Math.Min(second, first); 129 130 interpreter.FloatStack.SetTop(result); 131 return true; 101 132 } 102 133 } … … 106 137 /// </summary> 107 138 [PushExpression(StackTypes.Float, "FLOAT.MAX")] 108 public class FloatMaxExpression : PushResultExpression<double> { 109 public override bool Eval(IInternalPushInterpreter interpreter) { 110 return Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1])); 139 public class FloatMaxExpression : StatelessExpression { 140 public override bool Eval(IInternalPushInterpreter interpreter) { 141 if (interpreter.FloatStack.Count < 2) 142 return false; 143 144 var first = interpreter.FloatStack.Pop(); 145 var second = interpreter.FloatStack.Top; 146 var result = Math.Max(second, first); 147 148 interpreter.FloatStack.SetTop(result); 149 return true; 111 150 } 112 151 } … … 116 155 /// </summary> 117 156 [PushExpression(StackTypes.Float, "FLOAT.<", StackTypes.Boolean)] 118 public class FloatSmallerThanExpression : PushResultExpression<double> { 119 public override bool Eval(IInternalPushInterpreter interpreter) { 120 return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1]); 157 public class FloatSmallerThanExpression : StatelessExpression { 158 public override bool Eval(IInternalPushInterpreter interpreter) { 159 if (interpreter.FloatStack.Count < 2) 160 return false; 161 162 var first = interpreter.FloatStack.Top; 163 var second = interpreter.FloatStack[1]; 164 interpreter.FloatStack.Remove(2); 165 166 var result = second < first; 167 interpreter.BooleanStack.Push(result); 168 return true; 121 169 } 122 170 } … … 126 174 /// </summary> 127 175 [PushExpression(StackTypes.Float, "FLOAT.<=", StackTypes.Boolean)] 128 public class FloatSmallerThanOrEqualExpression : PushResultExpression<double> { 129 public override bool Eval(IInternalPushInterpreter interpreter) { 130 return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1] || values[0].IsAlmost(values[1])); 176 public class FloatSmallerThanOrEqualExpression : StatelessExpression { 177 public override bool Eval(IInternalPushInterpreter interpreter) { 178 if (interpreter.FloatStack.Count < 2) 179 return false; 180 181 var first = interpreter.FloatStack.Top; 182 var second = interpreter.FloatStack[1]; 183 interpreter.FloatStack.Remove(2); 184 185 var result = second < first || second.IsAlmost(first); 186 interpreter.BooleanStack.Push(result); 187 return true; 131 188 } 132 189 } … … 136 193 /// </summary> 137 194 [PushExpression(StackTypes.Float, "FLOAT.>", StackTypes.Boolean)] 138 public class FloatGreaterThanExpression : PushResultExpression<double> { 139 public override bool Eval(IInternalPushInterpreter interpreter) { 140 return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1]); 195 public class FloatGreaterThanExpression : StatelessExpression { 196 public override bool Eval(IInternalPushInterpreter interpreter) { 197 if (interpreter.FloatStack.Count < 2) 198 return false; 199 200 var first = interpreter.FloatStack.Top; 201 var second = interpreter.FloatStack[1]; 202 interpreter.FloatStack.Remove(2); 203 204 var result = second > first; 205 interpreter.BooleanStack.Push(result); 206 return true; 141 207 } 142 208 } … … 146 212 /// </summary> 147 213 [PushExpression(StackTypes.Float, "FLOAT.>=", StackTypes.Boolean)] 148 public class FloatGreaterThanOrEqualExpression : PushResultExpression<double> { 149 public override bool Eval(IInternalPushInterpreter interpreter) { 150 return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1] || values[0].IsAlmost(values[1])); 214 public class FloatGreaterThanOrEqualExpression : StatelessExpression { 215 public override bool Eval(IInternalPushInterpreter interpreter) { 216 if (interpreter.FloatStack.Count < 2) 217 return false; 218 219 var first = interpreter.FloatStack.Top; 220 var second = interpreter.FloatStack[1]; 221 interpreter.FloatStack.Remove(2); 222 223 var result = second > first || second.IsAlmost(first); 224 interpreter.BooleanStack.Push(result); 225 return true; 151 226 } 152 227 } … … 156 231 /// </summary> 157 232 [PushExpression(StackTypes.Float, "FLOAT.SIN")] 158 public class FloatSineExpression : PushResultExpression<double> { 159 public override bool Eval(IInternalPushInterpreter interpreter) { 160 return Eval(interpreter.FloatStack, 1, values => Math.Sin(values[0])); 233 public class FloatSineExpression : StatelessExpression { 234 public override bool Eval(IInternalPushInterpreter interpreter) { 235 if (interpreter.FloatStack.IsEmpty) 236 return false; 237 238 var result = Math.Sin(interpreter.FloatStack.Top); 239 interpreter.FloatStack.SetTop(result); 240 return true; 161 241 } 162 242 } … … 166 246 /// </summary> 167 247 [PushExpression(StackTypes.Float, "FLOAT.COS")] 168 public class FloatCosineExpression : PushResultExpression<double> { 169 public override bool Eval(IInternalPushInterpreter interpreter) { 170 return Eval(interpreter.FloatStack, 1, values => Math.Cos(values[0])); 248 public class FloatCosineExpression : StatelessExpression { 249 public override bool Eval(IInternalPushInterpreter interpreter) { 250 if (interpreter.FloatStack.IsEmpty) 251 return false; 252 253 var result = Math.Cos(interpreter.FloatStack.Top); 254 interpreter.FloatStack.SetTop(result); 255 return true; 171 256 } 172 257 }
Note: See TracChangeset
for help on using the changeset viewer.