public void Subtract(INumeric numeric) { if (numeric == null) return; //if (!this.HasCompatibleNumberSystem(numeric)) // throw new InvalidOperationException("incompatible number system"); var numeric1 = this.GetInnermostNumeric().Clone() as Numeric; var numeric2 = numeric.GetInnermostNumeric().Clone() as Numeric; numeric2.SwitchSign(); //apply whatever decorations this cake has to the clones //we do this so that node hooks can be applied, for instance var num2 = this.CloneDecorationCake(numeric2) as INumeric; var num1 = this.CloneDecorationCake(numeric1) as INumeric; var val = Add(num1, num2); this.SetValue(val); }
/// <summary> /// /// </summary> /// <param name="number1"></param> /// <param name="number2"></param> /// <param name="setValueIndicated">this is true if the return value is a clone and not the /// same instance as number1</param> /// <returns></returns> private static INumeric Add(INumeric number1, INumeric number2) { if (number1 == null) { return number2; } if (number2 == null) return number1; // Debug.WriteLine("adding {0} {1}", number1.SymbolsText, //number2.SymbolsText); INumeric rv = null; //determine the longer number bool? num1IsLonger = Numeric.AbsoluteValueCompare(number1.GetInnermostNumeric(), number2.GetInnermostNumeric()); //note: below we clone the 2nd arg if it is being modified as part of the operation //we don't clone the 1st arg as it is if (number1.IsPositive && number2.IsPositive) { rv = Increment(number1, number2); } else if (number1.IsPositive && number2.IsPositive == false) { switch (num1IsLonger) { case null: rv = Decrement(number1, number2); break; case true: rv = Decrement(number1, number2); break; case false: rv = Decrement(number2, number1); rv.GetInnermostNumeric().IsPositive = false; break; } } else if (number1.IsPositive == false && number2.IsPositive) { switch (num1IsLonger) { case null: rv = Decrement(number1, number2); break; case true: rv = Decrement(number1, number2); break; case false: rv = Decrement(number2, number1); rv.GetInnermostNumeric().IsPositive = true; break; } } else if (number1.IsPositive == false && number2.IsPositive == false) { rv = Increment(number1, number2); rv.GetInnermostNumeric().IsPositive = false; } return rv; }
public PrecisionNumericDecoration(object decorated, INumeric decimalPlaces, string cakeName = null) : base(decorated, cakeName) { if (decimalPlaces == null) throw new ArgumentNullException("decimalPlaces"); this.DecimalPlaces = decimalPlaces.GetInnermostNumeric(); }