コード例 #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
ファイル: UnOpItem.cs プロジェクト: TheModMaker/ModMaker.Lua
        /// <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
ファイル: Evaluator.cs プロジェクト: CookieWookie/MathEval
 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
ファイル: SyntaxToken.cs プロジェクト: CookieWookie/MathEval
 public static UnarySyntaxToken Unary(SyntaxToken value, UnaryOperationType type)
 {
     return(new UnarySyntaxToken(value, type));
 }
コード例 #18
0
ファイル: Parser.cs プロジェクト: puninvv/university
 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
ファイル: UnaryGeneralTemplate.cs プロジェクト: nobikik9/kju
 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
ファイル: CodeDOMExtensions.cs プロジェクト: ChazZeromus/gscc
 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;
 }