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 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); }