/// <summary> /// Performs a binary arithmetic operation and returns the result. /// </summary> /// <param name="type">The type of operation to perform.</param> /// <param name="other">The other value to use.</param> /// <returns>The result of the operation.</returns> /// <exception cref="System.InvalidOperationException"> /// If the operation cannot be performed with the given values. /// </exception> /// <exception cref="System.InvalidArgumentException"> /// If the argument is an invalid value. /// </exception> ILuaValue ILuaValue.Arithmetic(BinaryOperationType type, ILuaValue other) { // Attempt to use a meta-method. var ret = LuaValueBase.AttempMetamethod(type, this, other); if (ret != null) { return(ret); } // Do some default operations. ret = DefaultArithmetic(type, other); if (ret != null) { return(ret); } // If the other is not a visitor, throw. if (!(other is ILuaValueVisitor)) { throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Function)); } else { return(((ILuaValueVisitor)other).Arithmetic(type, this)); } }
/// <summary> /// Defines basic arithmetic for derived classes. This returns a /// non-null value when default, or null if it is a visitor. This /// throws if not a visitor. /// </summary> /// <param name="type">The type of operation to perform.</param> /// <param name="other">The other value to use.</param> /// <returns>The result of the operation.</returns> /// <exception cref="System.InvalidOperationException"> /// If the operation cannot be performed with the given values. /// </exception> /// <exception cref="System.InvalidArgumentException"> /// If the argument is an invalid value. /// </exception> protected ILuaValue ArithmeticBase(BinaryOperationType type, ILuaValue other) { // Attempt to use a meta-method. var ret = AttempMetamethod(type, this, other); if (ret != null) { return(ret); } // Do some default operations. ret = DefaultArithmetic(type, other); if (ret != null) { return(ret); } // If the other is not a visitor, throw. if (!(other is ILuaValueVisitor)) { throw new InvalidOperationException(Errors.CannotArithmetic(this.ValueType)); } else { return(null); } }
public ISinglePredicateState Compare(object value, BinaryOperationType operationType) { Argument.NotNull(value, "value"); var predicate = new BinaryPredicate(_expression, value, operationType); return(InitialPredicateState(predicate, _negate)); }
public static void TestMultiplication() { const BinaryOperationType op = BinaryOperationType.Multiplication; TestBinaryOperationFloatExact(0.0f, 0.0f, 0.0f, op); TestBinaryOperationFloatExact(1.0f, 0.0f, 0.0f, op); TestBinaryOperationFloatExact(0.0f, 1.0f, 0.0f, op); TestBinaryOperationFloatExact(-0.0f, 0.0f, 0.0f, op); TestBinaryOperationFloatExact(-0.0f, 0.0f, -0.0f, op); TestBinaryOperationFloatExact(0.0f, 0.0f, -0.0f, op); TestBinaryOperationFloatExact(1.0f, -1.0f, -1.0f, op); TestBinaryOperationFloatExact(-1.0f, -1.0f, 1.0f, op); TestBinaryOperationFloatApproximate(123.456f, 456.789f, 56393.34f, op); TestBinaryOperationFloatApproximate(1e-40f, 1e-42f, 0.0f, op); TestBinaryOperationFloatExact(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, op); TestBinaryOperationFloatExact(float.PositiveInfinity, float.NegativeInfinity, float.NegativeInfinity, op); TestBinaryOperationFloatExact(float.NegativeInfinity, float.NegativeInfinity, float.PositiveInfinity, op); TestBinaryOperationFloatExact(float.NaN, float.PositiveInfinity, float.NaN, op); TestBinaryOperationFloatExact(0.0f, float.PositiveInfinity, float.NaN, op); TestBinaryOperationFloatExact(float.NaN, float.NaN, float.NaN, op); TestBinaryOperationFloatExact(0.0f, float.NaN, float.NaN, op); TestBinaryOperationFloatExact(-999999.0f, float.NaN, float.NaN, op); RandomTestBinaryOperation(op); }
public static void TestDivision() { const BinaryOperationType op = BinaryOperationType.Division; TestBinaryOperationFloatExact(0.0f, 0.0f, float.NaN, op); TestBinaryOperationFloatExact(1.0f, 0.0f, float.PositiveInfinity, op); TestBinaryOperationFloatExact(0.0f, 1.0f, 0.0f, op); TestBinaryOperationFloatExact(-0.0f, 0.0f, float.NaN, op); TestBinaryOperationFloatExact(-0.0f, 0.0f, float.NaN, op); TestBinaryOperationFloatExact(0.0f, 0.0f, float.NaN, op); TestBinaryOperationFloatExact(1.0f, -1.0f, -1.0f, op); TestBinaryOperationFloatExact(-1.0f, -1.0f, 1.0f, op); TestBinaryOperationFloatApproximate(123.456f, 456.789f, 0.2702692f, op); TestBinaryOperationFloatApproximate(1e-40f, 1e-42f, 99.94678f, op); TestBinaryOperationFloatExact(float.PositiveInfinity, float.PositiveInfinity, float.NaN, op); TestBinaryOperationFloatExact(float.PositiveInfinity, float.NegativeInfinity, float.NaN, op); TestBinaryOperationFloatExact(float.NegativeInfinity, float.NegativeInfinity, float.NaN, op); TestBinaryOperationFloatExact(float.NaN, float.PositiveInfinity, float.NaN, op); TestBinaryOperationFloatExact(0.0f, float.PositiveInfinity, 0.0f, op); TestBinaryOperationFloatExact(float.PositiveInfinity, 0.0f, float.PositiveInfinity, op); TestBinaryOperationFloatExact(float.NaN, float.NaN, float.NaN, op); TestBinaryOperationFloatExact(0.0f, float.NaN, float.NaN, op); TestBinaryOperationFloatExact(-999999.0f, float.NaN, float.NaN, op); RandomTestBinaryOperation(op); }
public static void TestSubtraction() { const BinaryOperationType op = BinaryOperationType.Subtraction; TestBinaryOperationFloatExact(0.0f, 0.0f, 0.0f, op); TestBinaryOperationFloatExact(1.0f, 0.0f, 1.0f, op); TestBinaryOperationFloatExact(0.0f, 1.0f, -1.0f, op); TestBinaryOperationFloatExact(-0.0f, 0.0f, 0.0f, op); TestBinaryOperationFloatExact(-0.0f, 0.0f, -0.0f, op); TestBinaryOperationFloatExact(0.0f, 0.0f, -0.0f, op); TestBinaryOperationFloatExact(1.0f, -1.0f, 2.0f, op); TestBinaryOperationFloatExact(-1.0f, -1.0f, 0.0f, op); TestBinaryOperationFloatApproximate(123.456f, 456.789f, -333.333f, op); TestBinaryOperationFloatExact(float.PositiveInfinity, float.PositiveInfinity, float.NaN, op); TestBinaryOperationFloatExact(float.PositiveInfinity, float.NegativeInfinity, float.PositiveInfinity, op); TestBinaryOperationFloatExact(float.NaN, float.NaN, float.NaN, op); TestBinaryOperationFloatExact(0.0f, float.NaN, float.NaN, op); TestBinaryOperationFloatExact(-999999.0f, float.NaN, float.NaN, op); RandomTestBinaryOperation(op); }
/// <summary> /// Performs some default arithmetic like comparisons and returns the result. /// Returns null if there is no default. /// </summary> /// <param name="type">The type of operation to perform.</param> /// <param name="other">The other value to use.</param> /// <returns>The result of the operation.</returns> private ILuaValue DefaultArithmetic(BinaryOperationType type, ILuaValue other) { switch (type) { case BinaryOperationType.Concat: return(new LuaString(this.ToString() + other.ToString())); case BinaryOperationType.Gt: return(LuaBoolean.Create(CompareTo(other) > 0)); case BinaryOperationType.Lt: return(LuaBoolean.Create(CompareTo(other) < 0)); case BinaryOperationType.Gte: return(LuaBoolean.Create(CompareTo(other) >= 0)); case BinaryOperationType.Lte: return(LuaBoolean.Create(CompareTo(other) <= 0)); case BinaryOperationType.Equals: return(LuaBoolean.Create(Equals(other))); case BinaryOperationType.NotEquals: return(LuaBoolean.Create(!Equals(other))); case BinaryOperationType.And: return(other); case BinaryOperationType.Or: return(this); default: return(null); } }
public IPredicateDisjunctionState Compare(object value, BinaryOperationType operationType) { Argument.NotNull(value, "value"); var predicate = new BinaryPredicate(Expression, value, operationType); return(PredicateDisjunctionState(predicate, _negate)); }
public BinaryOperation(BinaryOperationType type, Variable first, Variable second, Variable result) { Type = type; First = first; Second = second; Result = result; }
public BinaryOperator(string assigned, string left, string right, BinaryOperationType operationType, int lineNumber) : this() { Assigned = assigned; LeftOperand = left; RightOperand = right; OperationType = operationType; base.LineNumber = lineNumber; }
public BinaryExpr(Element left, BinaryOperationType type, Element right, TextRange range) : base(range) { Contract.Requires <ArgumentNullException>(left != null); Contract.Requires <ArgumentNullException>(right != null); this.Left = left; this.Right = right; this.ExprType = type; }
private static void RandomTestBinaryOperation(BinaryOperationType op) { Func <float, float, float> func = op switch { BinaryOperationType.Addition => (float a, float b) => a + b, BinaryOperationType.Subtraction => (float a, float b) => a - b, BinaryOperationType.Multiplication => (float a, float b) => a * b, BinaryOperationType.Division => (float a, float b) => a / b, BinaryOperationType.Modulus => (float a, float b) => a % b, BinaryOperationType.Power => MathF.Pow, BinaryOperationType.ArcTangent2 => MathF.Atan2, _ => throw new ArgumentException(), }; PCG rand = new PCG(0, 0); // very small values for (int i = 0; i < RandomTestCount; ++i) { float a = rand.FloatInclusive(-1e-10f, 1e-10f); float b = rand.FloatInclusive(-1e-10f, 1e-10f); TestBinaryOperationFloatApproximate(a, b, func(a, b), op); } // small values for (int i = 0; i < RandomTestCount; ++i) { float a = rand.FloatInclusive(-1.0f, 1.0f); float b = rand.FloatInclusive(-1.0f, 1.0f); TestBinaryOperationFloatApproximate(a, b, func(a, b), op); } // large values for (int i = 0; i < RandomTestCount; ++i) { float a = rand.FloatInclusive(-100000.0f, 100000.0f); float b = rand.FloatInclusive(-100000.0f, 100000.0f); TestBinaryOperationFloatApproximate(a, b, func(a, b), op); } // huge values for (int i = 0; i < RandomTestCount; ++i) { float a = rand.FloatInclusive(-1000000000.0f, 1000000000.0f); float b = rand.FloatInclusive(-1000000000.0f, 1000000000.0f); TestBinaryOperationFloatApproximate(a, b, func(a, b), op); } // gigantic values for (int i = 0; i < RandomTestCount; ++i) { float a = rand.FloatInclusive(-1e38f, 1e38f); float b = rand.FloatInclusive(-1e38f, 1e38f); TestBinaryOperationFloatApproximate(a, b, func(a, b), op); } }
public override ILuaValue Arithmetic(BinaryOperationType type, LuaNumber self) { // Cannot use DefaultArithmetic since self and this are swapped. switch (type) { case BinaryOperationType.Add: return(new LuaNumber(self.Value + Value)); case BinaryOperationType.Subtract: return(new LuaNumber(self.Value - Value)); case BinaryOperationType.Multiply: return(new LuaNumber(self.Value * Value)); case BinaryOperationType.Divide: return(new LuaNumber(self.Value / Value)); case BinaryOperationType.Power: return(new LuaNumber(Math.Pow(self.Value, Value))); case BinaryOperationType.Modulo: return(new LuaNumber(self.Value - Math.Floor(self.Value / Value) * Value)); case BinaryOperationType.Concat: return(new LuaString(self.ToString() + ToString())); case BinaryOperationType.Gt: return(LuaBoolean.Create(self.CompareTo(this) > 0)); case BinaryOperationType.Lt: return(LuaBoolean.Create(self.CompareTo(this) < 0)); case BinaryOperationType.Gte: return(LuaBoolean.Create(self.CompareTo(this) >= 0)); case BinaryOperationType.Lte: return(LuaBoolean.Create(self.CompareTo(this) <= 0)); case BinaryOperationType.Equals: return(LuaBoolean.Create(self.Equals(this))); case BinaryOperationType.NotEquals: return(LuaBoolean.Create(!self.Equals(this))); case BinaryOperationType.And: return(!self.IsTrue ? self : this); case BinaryOperationType.Or: return(self.IsTrue ? self : this); default: throw new ArgumentException(Resources.BadBinOp); } }
/// <summary> /// Performs a binary arithmetic operation and returns the result. /// </summary> /// <param name="type">The type of operation to perform.</param> /// <param name="self">The first value to use.</param> /// <returns>The result of the operation.</returns> /// <exception cref="System.InvalidOperationException"> /// If the operation cannot be performed with the given values. /// </exception> /// <exception cref="System.InvalidArgumentException"> /// If the argument is an invalid value. /// </exception> public virtual ILuaValue Arithmetic(BinaryOperationType type, LuaValues.LuaTable self) { var ret = AttempMetamethod(type, self, this); if (ret != null) { return(ret); } throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Table)); }
/// <summary> /// Performs a binary arithmetic operation and returns the result. /// </summary> /// <param name="type">The type of operation to perform.</param> /// <param name="self">The first value to use.</param> /// <returns>The result of the operation.</returns> /// <exception cref="System.InvalidOperationException"> /// If the operation cannot be performed with the given values. /// </exception> /// <exception cref="System.InvalidArgumentException"> /// If the argument is an invalid value. /// </exception> ILuaValue ILuaValueVisitor.Arithmetic(BinaryOperationType type, LuaValues.LuaThread self) { var ret = LuaValueBase.AttempMetamethod(type, self, this); if (ret != null) { return(ret); } throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Thread)); }
/// <summary> /// Requests function for given operation type. /// </summary> /// <param name="operation">Operation type.</param> /// <returns>Operation function.</returns> public static Func<bool[], bool[], bool[]> Request(BinaryOperationType operation) { switch (operation) { case BinaryOperationType.And: return new BinaryOperation().And; case BinaryOperationType.Or: return new BinaryOperation().Or; default: return new BinaryOperation().Or; } }
/// <summary> /// Returns whether the given operation is right associative. /// </summary> protected static bool _isRightAssociative(BinaryOperationType type) { switch (type) { case BinaryOperationType.Concat: case BinaryOperationType.Power: return(true); default: return(false); } }
/// <summary> /// Performs a binary arithmetic operation and returns the result. /// </summary> /// <param name="type">The type of operation to perform.</param> /// <param name="self">The first value to use.</param> /// <returns>The result of the operation.</returns> /// <exception cref="System.InvalidOperationException"> /// If the operation cannot be performed with the given values. /// </exception> /// <exception cref="System.InvalidArgumentException"> /// If the argument is an invalid value. /// </exception> public override ILuaValue Arithmetic(BinaryOperationType type, LuaString self) { var t = self.ToNumber(); if (t != null) { return(Arithmetic(type, t)); } else { throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.String)); } }
private DynamicMetaObjectBinder SearchBinder(BinaryOperationType op) { switch (op) { // arithmetic case BinaryOperationType.Add: return(Factory.AddBinder); case BinaryOperationType.Subtract: return(Factory.SubBinder); case BinaryOperationType.Multiply: return(Factory.MultBinder); case BinaryOperationType.Divide: return(Factory.DivideBinder); case BinaryOperationType.Modulo: return(Factory.ModBinder); // compare case BinaryOperationType.LessThan: return(Factory.LessThanBinder); case BinaryOperationType.LessThanOrEqual: return(Factory.LessThanOrEqualBinder); case BinaryOperationType.GreaterThan: return(Factory.GreaterThanBinder); case BinaryOperationType.GreaterThanOrEqual: return(Factory.GreaterThanOrEqualBinder); // equality case BinaryOperationType.Equal: return(Factory.EqualBinder); case BinaryOperationType.NotEqual: return(Factory.NotEqualBinder); // bool case BinaryOperationType.And: return(Factory.AndBinder); case BinaryOperationType.Or: return(Factory.OrBinder); default: return(null); } }
/// <summary> /// Requests function for given operation type. /// </summary> /// <param name="operation">Operation type.</param> /// <returns>Operation function.</returns> public static Func <bool[], bool[], bool[]> Request(BinaryOperationType operation) { switch (operation) { case BinaryOperationType.And: return(new BinaryOperation().And); case BinaryOperationType.Or: return(new BinaryOperation().Or); default: return(new BinaryOperation().Or); } }
/// <summary> /// Creates a new instance with the given state. /// </summary> /// <param name="lhs">The left-hand-side of the epxression.</param> /// <param name="rhs">The right-hand-side of the epxression.</param> /// <param name="type">The type of the expression.</param> /// <exception cref="System.ArgumentNullException">If lhs is null.</exception> public BinOpItem(IParseExp lhs, BinaryOperationType type, IParseExp rhs) { if (lhs == null) { throw new ArgumentNullException(nameof(lhs)); } if (rhs == null) { throw new ArgumentNullException(nameof(rhs)); } this.lhs = lhs; this.rhs = rhs; this.OperationType = type; }
public BinaryOperationSyntax( BinaryOperationType type, T left_operand, U right_operand) { if (left_operand is ILiteral && right_operand is ILiteral) { throw new InvalidOperationException(); } Type = type; LeftOperand = left_operand == null ? throw new ArgumentNullException(nameof(left_operand)) : left_operand; RightOperand = right_operand == null ? throw new ArgumentNullException(nameof(right_operand)) : right_operand; }
/// <summary> /// Gets the .NET name of a given operation. /// </summary> /// <param name="type">The type of operation.</param> /// <returns>The name of the operation (e.g. op_Addition).</returns> static string _getOperationName(BinaryOperationType type) { switch (type) { case BinaryOperationType.Add: return("op_Addition"); case BinaryOperationType.Subtract: return("op_Subtraction"); case BinaryOperationType.Multiply: return("op_Multiply"); case BinaryOperationType.Divide: return("op_Division"); case BinaryOperationType.Power: return("op_Power"); case BinaryOperationType.Modulo: return("op_Modulo"); case BinaryOperationType.Gt: return("op_GreaterThan"); case BinaryOperationType.Lt: return("op_LessThan"); case BinaryOperationType.Gte: return("op_GreaterThanOrEqual"); case BinaryOperationType.Lte: return("op_LessThanOrEqual"); case BinaryOperationType.Equals: return("op_Equality"); case BinaryOperationType.NotEquals: return("op_Inequality"); default: return(""); } }
public static string GetString(this BinaryOperationType type) { switch (type) { case BinaryOperationType.Add: return("+"); case BinaryOperationType.Subtract: return("-"); case BinaryOperationType.Multiply: return("*"); case BinaryOperationType.Divide: return("/"); case BinaryOperationType.Modulus: return("%"); case BinaryOperationType.Power: return("^"); default: throw new ArgumentOutOfRangeException(); } }
static int FindBinaryOpeationSign(string line, out BinaryOperationType type) { if (line.Contains('+')) { type = BinaryOperationType.Add; return(line.LastIndexOf('+')); } else if (line.Contains('-')) { type = BinaryOperationType.Sub; return(line.LastIndexOf('-')); } else if (line.Contains('*')) { type = BinaryOperationType.Multi; return(line.LastIndexOf('*')); } else { type = BinaryOperationType.NotSpecified; return(-1); } }
/// <summary> /// Gets the precedence of the given operation. A smaller number means that the operation /// should be applied before ones with larger numbers. /// </summary> /// <param name="type">The type to get for.</param> /// <returns>The precedence of the given operation.</returns> protected static int _getPrecedence(BinaryOperationType type) { switch (type) { case BinaryOperationType.Or: return(1); case BinaryOperationType.And: return(2); case BinaryOperationType.Gt: case BinaryOperationType.Lt: case BinaryOperationType.Gte: case BinaryOperationType.Lte: case BinaryOperationType.Equals: case BinaryOperationType.NotEquals: return(3); case BinaryOperationType.Concat: return(8); case BinaryOperationType.Add: case BinaryOperationType.Subtract: return(9); case BinaryOperationType.Multiply: case BinaryOperationType.Divide: case BinaryOperationType.Modulo: return(10); case BinaryOperationType.Power: return(12); default: return(-1); } }
public static Boolean IsBitWise(this BinaryOperationType type) { return(type >= BinaryOperationType.BitAND && type <= BinaryOperationType.BitShiftLeft); }
/// <summary> /// Gets the precedence of the given operation. A smaller number /// means that the operation should be applied before ones with /// larger numbers. /// </summary> /// <param name="type">The type to get for.</param> /// <returns>The precedence of the given operation.</returns> protected static int GetPrecedence(BinaryOperationType type) { switch (type) { case BinaryOperationType.Multiply: case BinaryOperationType.Divide: case BinaryOperationType.Modulo: return 3; case BinaryOperationType.Add: case BinaryOperationType.Subtract: return 4; case BinaryOperationType.Concat: return 5; case BinaryOperationType.Gt: case BinaryOperationType.Lt: case BinaryOperationType.Gte: case BinaryOperationType.Lte: case BinaryOperationType.Equals: case BinaryOperationType.NotEquals: return 6; case BinaryOperationType.And: return 7; case BinaryOperationType.Or: return 8; default: return -1; } }
public static Boolean IsBooleanWise(this BinaryOperationType type) { return(type <= BinaryOperationType.BoolGreaterEquals); }
public override ILuaValue Arithmetic(BinaryOperationType type, ILuaValue other) { return(_arithmeticBase(type, other) ?? ((ILuaValueVisitor)other).Arithmetic(type, this)); }
public override ILuaValue Arithmetic <T>(BinaryOperationType type, LuaUserData <T> self) { return(self.ArithmeticFrom(type, this)); }
public BinaryExpr(Element left, BinaryOperationType type, Element right, TextRange range) : base(range) { Contract.Requires<ArgumentNullException>(left != null); Contract.Requires<ArgumentNullException>(right != null); this.Left = left; this.Right = right; this.ExprType = type; }
/// <summary> /// Creates a new instance with the given state. /// </summary> /// <param name="lhs">The left-hand-side of the epxression.</param> /// <param name="rhs">The right-hand-side of the epxression.</param> /// <param name="type">The type of the expression.</param> /// <exception cref="System.ArgumentNullException">If lhs is null.</exception> public BinOpItem(IParseExp lhs, BinaryOperationType type, IParseExp rhs) { if (lhs == null) throw new ArgumentNullException("lhs"); if (rhs == null) throw new ArgumentNullException("rhs"); this.lhs = lhs; this.rhs = rhs; this.OperationType = type; }
private DynamicMetaObjectBinder SearchBinder(BinaryOperationType op) { switch (op) { // arithmetic case BinaryOperationType.Add: return Factory.AddBinder; case BinaryOperationType.Subtract: return Factory.SubBinder; case BinaryOperationType.Multiply: return Factory.MultBinder; case BinaryOperationType.Divide: return Factory.DivideBinder; case BinaryOperationType.Modulo: return Factory.ModBinder; // compare case BinaryOperationType.LessThan: return Factory.LessThanBinder; case BinaryOperationType.LessThanOrEqual: return Factory.LessThanOrEqualBinder; case BinaryOperationType.GreaterThan: return Factory.GreaterThanBinder; case BinaryOperationType.GreaterThanOrEqual: return Factory.GreaterThanOrEqualBinder; // equality case BinaryOperationType.Equal: return Factory.EqualBinder; case BinaryOperationType.NotEqual: return Factory.NotEqualBinder; // bool case BinaryOperationType.And: return Factory.AndBinder; case BinaryOperationType.Or: return Factory.OrBinder; default: return null; } }
internal BinaryPredicate(object left, object right, BinaryOperationType operationType) { Left = Argument.NotNull(left, "left"); Right = Argument.NotNull(right, "right"); OperationType = operationType; }
public BinaryOperation(BinaryOperationType type) { Type = type; switch (type) { case BinaryOperationType.power: Priority = 5; break; case BinaryOperationType.multiply: Priority = 3; break; case BinaryOperationType.divide: Priority = 3; break; case BinaryOperationType.plus: Priority = 2; break; case BinaryOperationType.minus: Priority = 2; break; } }