예제 #1
0
 public UnaryExpr(UnaryOperationType type, Element value, TextRange range)
     : base(range)
 {
     Contract.Requires <ArgumentNullException>(value != null);
     this.ExprType = type;
     this.Value    = value;
 }
예제 #2
0
        public static Operator Parse(string oper, int num)
        {
            int indexOfEquals = oper.LastIndexOf("=");

            string assigned = oper.Substring(0, indexOfEquals);

            if (IsUnacceptableIdentifier(assigned))
            {
                throw new FormatException("Ошибка в задании цели присваивания (неверный идентификатор)");
            }

            int indexOfSign = oper.Length - 1;
            UnaryOperationType operationType = GetUnaryOperationType(oper.ElementAt(indexOfSign));

            if (operationType == UnaryOperationType.NotSpecified)
            {
                throw new FormatException("Несуществующая операция");
            }

            string operand = oper.Substring(indexOfEquals + 1, (indexOfSign - indexOfEquals - 1));

            if (IsUnacceptableIdentifier(assigned))
            {
                throw new FormatException("Ошибка в задании операнда (неверный идентификатор)");
            }

            UnaryOperator operation = new UnaryOperator(assigned, operand, operationType, num);

            return(operation);
        }
예제 #3
0
        private static void RandomTestTrigonometryOperation(UnaryOperationType op)
        {
            Func <float, float> func = op switch
            {
                UnaryOperationType.Sine => MathF.Sin,
                UnaryOperationType.Cosine => MathF.Cos,
                UnaryOperationType.Tangent => MathF.Tan,
                _ => throw new ArgumentException(),
            };

            PCG rand = new PCG(0, 0);

            // small values
            for (int i = 0; i < RandomTestCount; ++i)
            {
                float x = rand.FloatInclusive(-1.0f, 1.0f);
                TestTrigonometryOperationApproximate(x, func(x), op);
            }

            // medium values
            for (int i = 0; i < RandomTestCount; ++i)
            {
                float x = rand.FloatInclusive(-100.0f, 100.0f);
                TestTrigonometryOperationApproximate(x, func(x), op);
            }
        }
예제 #4
0
 public UnaryOperationSyntax(UnaryOperationType type, T operand)
 {
     Type    = type;
     Operand = operand == null
         ? throw new ArgumentNullException(nameof(operand))
         : operand;
 }
예제 #5
0
        private static void TestUnaryOperationFloatExact(float x, float expected, UnaryOperationType op)
        {
            UnaryOperation func   = unaryOperations[(int)op];
            sfloat         result = func((sfloat)x);
            bool           isOk   = result.Equals((sfloat)expected);

            Debug.Assert(isOk);
        }
예제 #6
0
        /// <summary>
        /// Creates a new UnOpItem with the given state.
        /// </summary>
        /// <param name="target">The target expression.</param>
        /// <param name="type">The type of operation.</param>
        /// <exception cref="System.ArgumentNullException">If target is null.</exception>
        public UnOpItem(IParseExp target, UnaryOperationType type)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            this.target = target;
            this.OperationType = type;
        }
예제 #7
0
 public UnaryOperator(string assigned, string operand, UnaryOperationType operationType, int lineNumber)
     : this()
 {
     Assigned        = assigned;
     Operand         = operand;
     OperationType   = operationType;
     base.LineNumber = lineNumber;
 }
예제 #8
0
 private SyntaxToken EvalUnary(ConstantSyntaxToken value, UnaryOperationType type)
 {
     switch (type)
     {
         case UnaryOperationType.Factorial: return SyntaxToken.Constant(MathEx.Factorial(value.Value));
         default: throw new NotSupportedException();
     }
 }
예제 #9
0
        /// <summary>
        /// Creates a new UnOpItem with the given state.
        /// </summary>
        /// <param name="target">The target expression.</param>
        /// <param name="type">The type of operation.</param>
        /// <exception cref="System.ArgumentNullException">If target is null.</exception>
        public UnOpItem(IParseExp target, UnaryOperationType type)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            this.target        = target;
            this.OperationType = type;
        }
예제 #10
0
            private DynamicMetaObjectBinder SearchBinder(UnaryOperationType op)
            {
                switch (op)
                {
                case UnaryOperationType.Not:
                    return(Factory.NotBinder);

                case UnaryOperationType.Negate:
                    return(Factory.NegateBinder);

                default:
                    return(null);
                }
            }
예제 #11
0
        private static void RandomTestUnaryOperation(UnaryOperationType op, float allowedErrorMultiplier = 1.0f)
        {
            Func <float, float> func = op switch
            {
                UnaryOperationType.Round => MathF.Round,
                UnaryOperationType.Floor => MathF.Floor,
                UnaryOperationType.Ceiling => MathF.Ceiling,
                UnaryOperationType.Sine => MathF.Sin,
                UnaryOperationType.Cosine => MathF.Cos,
                UnaryOperationType.Tangent => MathF.Tan,
                UnaryOperationType.SquareRoot => MathF.Sqrt,
                UnaryOperationType.Exponential => MathF.Exp,
                UnaryOperationType.LogarithmNatural => MathF.Log,
                UnaryOperationType.LogarithmBase2 => MathF.Log2,
                UnaryOperationType.ArcSine => MathF.Asin,
                UnaryOperationType.ArcCosine => MathF.Acos,
                UnaryOperationType.ArcTangent => MathF.Atan,
                _ => throw new ArgumentException(),
            };

            PCG rand = new PCG(0, 0);

            // very small values
            for (int i = 0; i < RandomTestCount; ++i)
            {
                float x = rand.FloatInclusive(-1e-40f, 1e-40f);
                TestUnaryOperationFloatApproximate(x, func(x), op, allowedErrorMultiplier);
            }

            // small values
            for (int i = 0; i < RandomTestCount; ++i)
            {
                float x = rand.FloatInclusive(-1.0f, 1.0f);
                TestUnaryOperationFloatApproximate(x, func(x), op, allowedErrorMultiplier);
            }

            // large values
            for (int i = 0; i < RandomTestCount; ++i)
            {
                float x = rand.FloatInclusive(-100000.0f, 100000.0f);
                TestUnaryOperationFloatApproximate(x, func(x), op, allowedErrorMultiplier);
            }

            // huge values
            for (int i = 0; i < RandomTestCount; ++i)
            {
                float x = rand.FloatInclusive(-1000000000.0f, 1000000000.0f);
                TestUnaryOperationFloatApproximate(x, func(x), op, allowedErrorMultiplier);
            }
        }
예제 #12
0
 public UnaryGeneralInstruction(
     VirtualRegister input,
     VirtualRegister result,
     UnaryOperationType type)
     : base(
         new List <VirtualRegister> {
     input
 },
         new List <VirtualRegister> {
     result
 },
         new List <Tuple <VirtualRegister, VirtualRegister> >
 {
     new Tuple <VirtualRegister, VirtualRegister>(input, result)
 })
 {
     this.input  = input;
     this.result = result;
     this.type   = type;
 }
예제 #13
0
 public UnaryOperationNode(AstNode operand, UnaryOperationType type)
 {
     Operand = operand;
     Type    = type;
 }
예제 #14
0
        private static void TestUnaryOperationFloatApproximate(float x, float expected, UnaryOperationType op, float allowedErrorMultiplier = 1.0f)
        {
            UnaryOperation func   = unaryOperations[(int)op];
            sfloat         result = func((sfloat)x);

            if (float.IsNaN(expected) && result.IsNaN())
            {
                // special case, NaN-s cannot be compared
                return;
            }

            if (float.IsInfinity(expected) && result.IsInfinity() && MathF.Sign(expected) == result.Sign())
            {
                // both are the same infinities
                return;
            }

            float allowedError = MathF.Max(1e-6f * allowedErrorMultiplier * MathF.Pow(2.0f, MathF.Log2(MathF.Abs(expected) + 1.0f)), 1e-6f);

            float difference = MathF.Abs((float)result - expected);
            bool  isOk       = difference <= allowedError;

            if (!isOk && (op == UnaryOperationType.Round || op == UnaryOperationType.Floor || op == UnaryOperationType.Ceiling))
            {
                // Because of the loss of precision that can result from representing decimal values
                // as floating-point numbers or performing arithmetic operations on floating-point values,
                // in some cases the Round method may not appear to round midpoint values to the nearest even integer.
                // https://docs.microsoft.com/en-us/dotnet/api/system.math.round

                if (MathF.Abs(x % 1.0f) - 0.5f < 0.01f)
                {
                    // x is near a midpoint, it's possible that rounding happened in a different direction
                    isOk = MathF.Abs((float)result - expected) <= 1.0f;
                }
            }

            Debug.Assert(isOk);
        }
예제 #15
0
 public UnaryOperation(IExpression expression, UnaryOperationType type)
 {
     Expression = expression;
     Type       = type;
 }
예제 #16
0
        private static void TestTrigonometryOperationApproximate(float x, float expected, UnaryOperationType op)
        {
            UnaryOperation func   = unaryOperations[(int)op];
            sfloat         result = func((sfloat)x);

            if (float.IsNaN(expected) && result.IsNaN())
            {
                // special case, NaN-s cannot be compared
                return;
            }

            if (float.IsInfinity(expected) && result.IsInfinity() && MathF.Sign(expected) == result.Sign())
            {
                // both are the same infinities
                return;
            }

            float allowedError = MathF.Max(0.005f * MathF.Pow(2.0f, MathF.Log2(MathF.Abs(expected) + 1.0f)), 1e-6f);

            float difference = MathF.Abs((float)result - expected);
            bool  isOk       = difference <= allowedError;

            Debug.Assert(isOk);
        }
예제 #17
0
 public static UnarySyntaxToken Unary(SyntaxToken value, UnaryOperationType type)
 {
     return(new UnarySyntaxToken(value, type));
 }
예제 #18
0
 public UnaryOperation(UnaryOperationType type)
 {
     Type = type;
     switch (type)
     {
         case UnaryOperationType.cos:
             Priority = 4;
             break;
         case UnaryOperationType.sin:
             Priority = 4;
             break;
         case UnaryOperationType.log:
             Priority = 4;
             break;
     }
 }
예제 #19
0
 private DynamicMetaObjectBinder SearchBinder(UnaryOperationType op)
 {
     switch (op) {
     case UnaryOperationType.Not:
         return Factory.NotBinder;
     case UnaryOperationType.Negate:
         return Factory.NegateBinder;
     default:
         return null;
     }
 }
예제 #20
0
 internal UnarySyntaxToken(SyntaxToken value, UnaryOperationType type)
 {
     this.Value = value;
     this.Type  = type;
 }
예제 #21
0
 /// <summary>
 /// Creates a new UnOpItem with the given state.
 /// </summary>
 /// <param name="target">The target expression.</param>
 /// <param name="type">The type of operation.</param>
 public UnOpItem(IParseExp target, UnaryOperationType type)
 {
     Target        = target;
     OperationType = type;
 }
예제 #22
0
 internal UnaryPredicate(object expression, UnaryOperationType operationType)
 {
     Expression    = Argument.NotNull(expression, "expression");
     OperationType = operationType;
 }
예제 #23
0
 public UnaryOperation(UnaryOperationType type, Variable first, Variable result)
 {
     Type   = type;
     First  = first;
     Result = result;
 }
예제 #24
0
 public UnaryGeneralTemplate(UnaryOperationType operationType)
     : base(new UnaryOperation(null, operationType), 1)
 {
     this.operationType = operationType;
 }
예제 #25
0
파일: Nodes.cs 프로젝트: nobikik9/kju
 public UnaryOperation(Node operand, UnaryOperationType type)
 {
     this.Operand = operand;
     this.Type    = type;
 }
예제 #26
0
 public static Boolean IsPostfixUnary(this UnaryOperationType type)
 {
     return(type >= UnaryOperationType.PostIncrement);
 }
예제 #27
0
파일: Ast.cs 프로젝트: irxground/kurogane
 public UnaryExpr(UnaryOperationType type, Element value, TextRange range)
     : base(range)
 {
     Contract.Requires<ArgumentNullException>(value != null);
     this.ExprType = type;
     this.Value = value;
 }