public bool Equals(NumberDecimal other) { return(Equals(other, null) && Numerator.Equals(other.Significand) && Denominator.Equals(1) && Exponent.Equals(other.Exponent)); }
public static NumberRational operator +(NumberRational a, NumberRational b) { if (a == null || b == null) { return(null); } var ad = a.Denominator; var an = new NumberDecimal(a.Numerator, a.Exponent); var bd = b.Denominator; var bn = new NumberDecimal(b.Numerator, b.Exponent); var d = an.Multiply(bd).Add(bn.Multiply(ad)); return(new NumberRational(d.Significand, ad * bd, d.Exponent)); }
public (BigInteger, NumberDecimal, bool) DivideDecimal(NumberDecimal number) { var(a, b, e) = (NormalizeExponent(this, number)); if (!a.HasValue) { return(int.MaxValue, new NumberDecimal(-1, 0), false); //a is too large. } if (!b.HasValue) { return(0, this, true); //b is too large } var div = BigInteger.DivRem(a.Value, b.Value, out BigInteger remainder); return(div, new NumberDecimal(remainder, e), true); }
public ConversionResult <BigInteger> GetBigInteger() { if (this.Denominator == 1) { return(new NumberDecimal(this.Numerator, this.Exponent).GetBigInteger()); } else { var asBI = new NumberDecimal(this.Numerator, this.Exponent).GetBigInteger(); if (!asBI.WithinRange) { return(new ConversionResult <BigInteger>(0, false, false)); } var result = asBI.Value / this.Denominator; return(new ConversionResult <BigInteger>(result, false, true)); } }
public static IValue Power(NumberDecimal x, int exponent) { if (exponent > 0) { return(new NumberDecimal( BigInteger.Pow(x.Significand, exponent), x.Exponent * exponent)); } else if (exponent == 0) { return(One); } else { return(new NumberRational(1, BigInteger.Pow(x.Significand, -exponent), x.Exponent * exponent)); } }
public static (BigInteger?a, BigInteger?b, BigInteger exponent) NormalizeExponent(NumberDecimal a, NumberDecimal b) { var exp = BigInteger.Min(a.Exponent, b.Exponent); return(a.ShiftExponent(exp).GetNullable(), b.ShiftExponent(exp).GetNullable(), exp); }
public IValue Divide(NumberDecimal number) => this / number;
public NumberDecimal Multiply(NumberDecimal number) => this * number;
public NumberDecimal Substract(NumberDecimal number) => this - number;
public NumberDecimal Add(NumberDecimal number) => this + number;
public bool Equals(NumberDecimal other) { return(!Equals(other, null) && this.Significand.Equals(other.Significand) && this.Exponent.Equals(other.Exponent)); }