Ejemplo n.º 1
0
        public static bool TryParse(RpnValueType type, string text, Calculator?calc, [MaybeNullWhen(false)] out Value value)
        {
            // NOTE: calc can be null.
            bool result;

            switch (type)
            {
            case RpnValueType.Binary:
                BinaryValue?binaryValue;
                result = BinaryValue.TryParse(text, calc, out binaryValue);
                value  = binaryValue;
                break;

            case RpnValueType.Complex:
                ComplexValue?complexValue;
                result = ComplexValue.TryParse(text, calc, out complexValue);
                value  = complexValue;
                break;

            case RpnValueType.DateTime:
                DateTimeValue?dateTimeValue;
                result = DateTimeValue.TryParse(text, out dateTimeValue);
                value  = dateTimeValue;
                break;

            case RpnValueType.Double:
                DoubleValue?doubleValue;
                result = DoubleValue.TryParse(text, out doubleValue);
                value  = doubleValue;
                break;

            case RpnValueType.Fraction:
                FractionValue?fractionValue;
                result = FractionValue.TryParse(text, out fractionValue);
                value  = fractionValue;
                break;

            case RpnValueType.Integer:
                IntegerValue?integerValue;
                result = IntegerValue.TryParse(text, out integerValue);
                value  = integerValue;
                break;

            case RpnValueType.TimeSpan:
                TimeSpanValue?timeSpanValue;
                result = TimeSpanValue.TryParse(text, out timeSpanValue);
                value  = timeSpanValue;
                break;

            default:
                result = false;
                value  = null;
                break;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static Value Subtract(Value x, Value y, Calculator calc)
        {
            Value?result = null;

            if (HandleImplicitTypeConversion(ref x, ref y))
            {
                switch (x.ValueType)
                {
                case RpnValueType.Binary:
                    result = BinaryValue.Subtract((BinaryValue)x, (BinaryValue)y, calc);
                    break;

                case RpnValueType.Complex:
                    result = (ComplexValue)x - (ComplexValue)y;
                    break;

                case RpnValueType.DateTime:
                    result = (DateTimeValue)x - (DateTimeValue)y;
                    break;

                case RpnValueType.Double:
                    result = (DoubleValue)x - (DoubleValue)y;
                    break;

                case RpnValueType.Fraction:
                    result = (FractionValue)x - (FractionValue)y;
                    break;

                case RpnValueType.Integer:
                    result = (IntegerValue)x - (IntegerValue)y;
                    break;

                case RpnValueType.TimeSpan:
                    result = (TimeSpanValue)x - (TimeSpanValue)y;
                    break;
                }
            }
            else
            {
                // Handle special cases.
                if (x.ValueType == RpnValueType.DateTime && y.ValueType == RpnValueType.TimeSpan)
                {
                    result = (DateTimeValue)x - (TimeSpanValue)y;
                }
            }

            if (result == null)
            {
                throw InvalidOp(Resources.Value_Subtract, x, y);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static Value Multiply(Value x, Value y, Calculator calc)
        {
            Value?result = null;

            if (HandleImplicitTypeConversion(ref x, ref y))
            {
                switch (x.ValueType)
                {
                case RpnValueType.Binary:
                    result = BinaryValue.Multiply((BinaryValue)x, (BinaryValue)y, calc);
                    break;

                case RpnValueType.Complex:
                    result = (ComplexValue)x * (ComplexValue)y;
                    break;

                case RpnValueType.Double:
                    result = (DoubleValue)x * (DoubleValue)y;
                    break;

                case RpnValueType.Fraction:
                    result = (FractionValue)x * (FractionValue)y;
                    break;

                case RpnValueType.Integer:
                    result = (IntegerValue)x * (IntegerValue)y;
                    break;
                }
            }
            else
            {
                // Handle special cases.
                if (x.ValueType == RpnValueType.TimeSpan && y is NumericValue numY)
                {
                    // TimeSpan * Numeric
                    result = (TimeSpanValue)x * new DoubleValue(numY.ToDouble());
                }
                else if (x is NumericValue numX && y.ValueType == RpnValueType.TimeSpan)
                {
                    // Numeric * TimeSpan
                    result = (TimeSpanValue)y * new DoubleValue(numX.ToDouble());
                }
            }

            if (result == null)
            {
                throw InvalidOp(Resources.Value_Multiply, x, y);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static Value Abs(Value value, Calculator calc)
        {
            Value result = value;

            switch (value.ValueType)
            {
            case RpnValueType.TimeSpan:
                TimeSpanValue timeSpan = (TimeSpanValue)value;
                if (timeSpan.AsTimeSpan < TimeSpan.Zero)
                {
                    result = new TimeSpanValue(timeSpan.AsTimeSpan.Negate());
                }

                break;

            case RpnValueType.Complex:
                result = new DoubleValue(Complex.Abs(((ComplexValue)value).AsComplex));
                break;

            case RpnValueType.Double:
                result = new DoubleValue(Math.Abs(((DoubleValue)value).AsDouble));
                break;

            case RpnValueType.Fraction:
                FractionValue fraction = (FractionValue)value;
                if (fraction.Sign < 0)
                {
                    result = FractionValue.Negate(fraction);
                }

                break;

            case RpnValueType.Integer:
                result = new IntegerValue(BigInteger.Abs(((IntegerValue)value).AsInteger));
                break;

            case RpnValueType.Binary:
                BinaryValue binary = (BinaryValue)value;
                if (binary.Sign(calc) < 0)
                {
                    result = BinaryValue.Negate(binary, calc);
                }

                break;

            default:
                throw InvalidOp(Resources.Value_Abs, value);
            }

            return(result);
        }
Ejemplo n.º 5
0
        public static Value Negate(Value value, Calculator calc)
        {
            Value result;

            switch (value.ValueType)
            {
            case RpnValueType.TimeSpan:
                result = -(TimeSpanValue)value;
                break;

            case RpnValueType.Complex:
                result = -(ComplexValue)value;
                break;

            case RpnValueType.Double:
                result = -(DoubleValue)value;
                break;

            case RpnValueType.Fraction:
                result = -(FractionValue)value;
                break;

            case RpnValueType.Integer:
                result = -(IntegerValue)value;
                break;

            case RpnValueType.Binary:
                result = BinaryValue.Negate((BinaryValue)value, calc);
                break;

            default:
                throw InvalidOp(Resources.Value_Negate, value);
            }

            return(result);
        }