namespace Bts.ArithmeticExpressions; using System.Numerics; public class Difference : IArithmeticExpression { readonly IArithmeticExpression lhs; readonly IArithmeticExpression rhs; public Difference(IArithmeticExpression lhs, IArithmeticExpression rhs) { this.lhs = lhs; this.rhs = rhs; } public static Difference Create( IArithmeticExpression left, IArithmeticExpression right) where T : INumber, ISubtractionOperators => new(left, right); public double Evaluate(IDictionary env) => evaluateArg(lhs, nameof(lhs), env) - evaluateArg(rhs, nameof(rhs), env); static double evaluateArg( IArithmeticExpression expr, string name, IDictionary env) { var obj = expr.Evaluate(env); return ConvertUtils.try_convert(expr.Evaluate(env), out double dbl) ? dbl : throw new Exception( $"Invalid Difference. {name} is of type {obj?.GetType()} (expected {typeof(double)})"); } } public class Difference : IArithmeticExpression where TLeft : INumber, ISubtractionOperators where TRight : INumber where TResult : INumber { readonly IArithmeticExpression left; readonly IArithmeticExpression right; public Difference(IArithmeticExpression left, IArithmeticExpression right) { this.left = left; this.right = right; } public TResult Evaluate(IDictionary env) => left.Evaluate(env) - right.Evaluate(env); }