///<summary> ///</summary> ///<param name="number"></param> ///<returns></returns> public BigNumber Multiply(BigNumber number) { if (numberState == NumberState.None) { if (number.numberState == 0) return new BigNumber(NumberState.None, bigDecimal.Multiply(number.bigDecimal)); return new BigNumber(number.numberState, null); } return new BigNumber(numberState, null); }
///<summary> ///</summary> ///<param name="number"></param> ///<returns></returns> public BigNumber Subtract(BigNumber number) { if (numberState == NumberState.None) { if (number.numberState == NumberState.None) return new BigNumber(NumberState.None, bigDecimal.Subtract(number.bigDecimal)); return new BigNumber(number.InverseState, null); } return new BigNumber(numberState, null); }
public BigNumber Modulus(BigNumber number) { if (numberState == 0) { if (number.numberState == 0) { BigDecimal div_by = number.bigDecimal; if (div_by.CompareTo(BdZero) != 0) { BigDecimal remainder = bigDecimal.Remainder(div_by); return new BigNumber(NumberState.None, remainder); } } } return new BigNumber(NumberState.NotANumber, null); }
///<summary> ///</summary> ///<param name="number"></param> ///<returns></returns> public BigNumber Divide(BigNumber number) { if (numberState == 0) { if (number.numberState == 0) { BigDecimal div_by = number.bigDecimal; if (div_by.CompareTo (BdZero) != 0) { return new BigNumber(NumberState.None, bigDecimal.Divide(div_by, 10, RoundingMode.HalfUp)); } } } // Return NaN if we can't divide return new BigNumber(NumberState.NotANumber, null); }
/** * Compares this BigNumber with the given BigNumber. Returns 0 if the values * are equal, >0 if this is greater than the given value, and < 0 if this * is less than the given value. */ public int CompareTo(BigNumber number) { if (this == number) { return 0; } // If this is a non-infinity number if (numberState == 0) { // If both values can be represented by a long value if (CanBeLong && number.CanBeLong) { // Perform a long comparison check, if (long_representation > number.long_representation) return 1; if (long_representation < number.long_representation) return -1; return 0; } // And the compared number is non-infinity then use the BigDecimal // compareTo method. if (number.numberState == 0) return bigDecimal.CompareTo(number.bigDecimal); // Comparing a regular number with a NaN number. // If positive infinity or if NaN if (number.numberState == NumberState.PositiveInfinity || number.numberState == NumberState.NotANumber) { return -1; } // If negative infinity if (number.numberState == NumberState.NegativeInfinity) return 1; throw new ApplicationException("Unknown number state."); } // This number is a NaN number. // Are we comparing with a regular number? if (number.numberState == NumberState.None) { // Yes, negative infinity if (numberState == NumberState.NegativeInfinity) return -1; // positive infinity or NaN if (numberState == NumberState.PositiveInfinity || numberState == NumberState.NotANumber) { return 1; } throw new ApplicationException("Unknown number state."); } // Comparing NaN number with a NaN number. // This compares -Inf less than Inf and NaN and NaN greater than // Inf and -Inf. -Inf < Inf < NaN return (numberState - number.numberState); }
// ---- Mathematical functions ---- ///<summary> ///</summary> ///<param name="number"></param> ///<returns></returns> public BigNumber BitWiseOr(BigNumber number) { if (numberState == NumberState.None && Scale == 0 && number.numberState == 0 && number.Scale == 0) { BigInteger bi1 = bigDecimal.ToBigInteger(); BigInteger bi2 = number.bigDecimal.ToBigInteger(); return new BigNumber(NumberState.None, new BigDecimal(bi1.Or(bi2))); } return null; }
///<summary> ///</summary> ///<param name="number"></param> ///<returns></returns> public BigNumber Add(BigNumber number) { if (numberState == NumberState.None) { if (number.numberState == NumberState.None) return new BigNumber(NumberState.None, bigDecimal.Add(number.bigDecimal)); return new BigNumber(number.numberState, null); } return new BigNumber(numberState, null); }