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