示例#1
0
 public bool Equals(NumberDecimal other)
 {
     return(Equals(other, null) &&
            Numerator.Equals(other.Significand) &&
            Denominator.Equals(1) &&
            Exponent.Equals(other.Exponent));
 }
示例#2
0
        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));
        }
示例#3
0
        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);
        }
示例#4
0
 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));
     }
 }
示例#5
0
 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));
     }
 }
示例#6
0
        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);
        }
示例#7
0
 public IValue Divide(NumberDecimal number) => this / number;
示例#8
0
 public NumberDecimal Multiply(NumberDecimal number) => this * number;
示例#9
0
 public NumberDecimal Substract(NumberDecimal number) => this - number;
示例#10
0
 public NumberDecimal Add(NumberDecimal number) => this + number;
示例#11
0
 public bool Equals(NumberDecimal other)
 {
     return(!Equals(other, null) &&
            this.Significand.Equals(other.Significand) &&
            this.Exponent.Equals(other.Exponent));
 }