object IConvertible.ToType(Type conversionType, IFormatProvider provider) { var scaleDivisor = BigInteger.Pow(new BigInteger(10), this._scale); var remainder = BigInteger.Remainder(this._unscaledValue, scaleDivisor); var scaledValue = BigInteger.Divide(this._unscaledValue, scaleDivisor); if (scaledValue > new BigInteger(Decimal.MaxValue)) { throw new ArgumentOutOfRangeException("value", "The value " + this._unscaledValue + " cannot fit into " + conversionType.Name + "."); } var leftOfDecimal = (decimal)scaledValue; var rightOfDecimal = ((decimal)remainder) / ((decimal)scaleDivisor); var value = leftOfDecimal + rightOfDecimal; return(Convert.ChangeType(value, conversionType)); }
/// <summary> /// Find the great common divisor for a and b /// </summary> public static BigInteger FindGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y) { // Base Case if (a == 0) { x = BigInteger.Zero; y = BigInteger.One; return(b); } // To store results of recursive call var gcd = FindGCD(BigInteger.Remainder(b, a), a, out BigInteger x1, out BigInteger y1); // Update x and y using results of recursive // call x = BigInteger.Subtract(y1, BigInteger.Multiply(BigInteger.Divide(b, a), x1)); y = new BigInteger(x1.ToByteArray()); return(gcd); }
public int CompareTo(BigDecimal other) { var unscaledValueCompare = this._unscaledValue.CompareTo(other._unscaledValue); var scaleCompare = this._scale.CompareTo(other._scale); // if both are the same value, return the value if (unscaledValueCompare == scaleCompare) { return(unscaledValueCompare); } // if the scales are both the same return unscaled value if (scaleCompare == 0) { return(unscaledValueCompare); } var scaledValue = BigInteger.Divide(this._unscaledValue, BigInteger.Pow(new BigInteger(10), this._scale)); var otherScaledValue = BigInteger.Divide(other._unscaledValue, BigInteger.Pow(new BigInteger(10), other._scale)); return(scaledValue.CompareTo(otherScaledValue)); }
// ---- SECTION: public instance methods --------------* #region Public Instance Methods // GetWholePart() and GetFractionPart() // // BigRational == Whole, Fraction // 0/2 == 0, 0/2 // 1/2 == 0, 1/2 // -1/2 == 0, -1/2 // 1/1 == 1, 0/1 // -1/1 == -1, 0/1 // -3/2 == -1, -1/2 // 3/2 == 1, 1/2 public BigInteger GetWholePart() { return BigInteger.Divide(m_numerator, m_denominator); }