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); }
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); }
public static NumericValue Power(IntegerValue x, IntegerValue exponent) { NumericValue result; if (exponent.AsInteger < 0 || exponent.AsInteger > int.MaxValue) { result = FractionValue.Power( new FractionValue(x.AsInteger, BigInteger.One), new FractionValue(exponent.AsInteger, BigInteger.One)); } else { result = new IntegerValue(BigInteger.Pow(x.AsInteger, (int)exponent.AsInteger)); } return(result); }