Example #1
0
 public static EInteger DivRem(
     EInteger dividend,
     EInteger divisor,
     out EInteger remainder)
 {
     if (dividend == null)
     {
         throw new ArgumentNullException("dividend");
     }
     if (divisor == null)
     {
         throw new ArgumentNullException("divisor");
     }
     EInteger[] result = dividend.DivRem(divisor);
     remainder = result[1];
     return(result[0]);
 }
Example #2
0
   public static EInteger ReduceTrailingZeros(
       EInteger bigmant,
       FastInteger exponentMutable,
       int radix,
       FastInteger digits,
       FastInteger precision,
       FastInteger idealExp)
   {
 #if DEBUG
       if (precision != null && digits == null)
       {
           throw new ArgumentException("doesn't satisfy precision==null || digits!=null");
       }
 #endif
       if (bigmant.IsZero)
       {
           exponentMutable.SetInt(0);
           return(bigmant);
       }
       var bigradix    = (EInteger)radix;
       var bitToTest   = 0;
       var bitsToShift = new FastInteger(0);
       while (!bigmant.IsZero)
       {
           if (precision != null && digits.CompareTo(precision) == 0)
           {
               break;
           }
           if (idealExp != null && exponentMutable.CompareTo(idealExp) == 0)
           {
               break;
           }
           if (radix == 2)
           {
               if (bitToTest < Int32.MaxValue)
               {
                   if (bigmant.GetSignedBit(bitToTest))
                   {
                       break;
                   }
                   ++bitToTest;
                   bitsToShift.Increment();
               }
               else
               {
                   if (!bigmant.IsEven)
                   {
                       break;
                   }
                   bigmant >>= 1;
               }
           }
           else
           {
               EInteger bigrem;
               EInteger bigquo;
               {
                   EInteger[] divrem = bigmant.DivRem(bigradix);
                   bigquo = divrem[0];
                   bigrem = divrem[1];
               }
               if (!bigrem.IsZero)
               {
                   break;
               }
               bigmant = bigquo;
           }
           exponentMutable.Increment();
           if (digits != null)
           {
               digits.Decrement();
           }
       }
       if (radix == 2 && !bitsToShift.IsValueZero)
       {
           while (bitsToShift.CompareToInt(1000000) > 0)
           {
               bigmant >>= 1000000;
               bitsToShift.SubtractInt(1000000);
           }
           int tmpshift = bitsToShift.AsInt32();
           bigmant >>= tmpshift;
       }
       return(bigmant);
   }
Example #3
0
 public static void TestMultiplyDivideOne(
  EInteger bigintA,
  EInteger bigintB) {
   // Test that A*B/A = B and A*B/B = A
   try {
     EInteger bigintRem;
     EInteger bigintE;
     EInteger bigintD;
     EInteger bigintC = bigintA.Multiply (bigintB);
     TestCommon.CompareTestEqualAndConsistent (
       bigintC,
       bigintB.Multiply (bigintA));
     if (!bigintB.IsZero) {
       {
         EInteger [] divrem = bigintC.DivRem (bigintB);
         bigintD = divrem [0];
         bigintRem = divrem [1];
       }
       TestCommon.CompareTestEqualAndConsistent (bigintD, bigintA);
       TestCommon.CompareTestEqual (EInteger.Zero, bigintRem);
       bigintE = bigintC.Divide (bigintB);
       // Testing that DivRem and division method return
       // the same value
       TestCommon.CompareTestEqualAndConsistent (bigintD, bigintE);
       bigintE = bigintC.Remainder (bigintB);
       TestCommon.CompareTestEqualAndConsistent (bigintRem, bigintE);
       if (bigintE.Sign > 0 && !bigintC.Mod (bigintB).Equals (bigintE)) {
         TestCommon.CompareTestEqualAndConsistent (
           bigintE,
           bigintC.Mod (bigintB));
       }
     }
     if (!bigintA.IsZero) {
       EInteger [] divrem = bigintC.DivRem (bigintA);
       bigintD = divrem [0];
       bigintRem = divrem [1];
       TestCommon.CompareTestEqualAndConsistent (bigintD, bigintB);
       TestCommon.CompareTestEqual (EInteger.Zero, bigintRem);
     }
     if (!bigintB.IsZero) {
       EInteger [] divrem = bigintA.DivRem (bigintB);
       bigintC = divrem [0];
       bigintRem = divrem [1];
       bigintD = bigintB.Multiply (bigintC);
       bigintD += (EInteger)bigintRem;
       TestCommon.CompareTestEqualAndConsistent (bigintA, bigintD);
     }
     // -----------------------------------
     // EDecimal
     // -----------------------------------
     EDecimal edecA = EDecimal.FromEInteger (bigintA);
     EDecimal edecB = EDecimal.FromEInteger (bigintB);
     EDecimal edecC = edecA.Multiply (edecB);
     EDecimal edecRem;
     EDecimal edecE;
     EDecimal edecD;
     TestCommon.CompareTestEqualAndConsistent (
       edecC,
       edecB.Multiply (edecA));
     if (!edecB.IsZero) {
       EDecimal [] divrem = edecC.DivRemNaturalScale (edecB);
       edecD = divrem [0].Plus (null);
       edecRem = divrem [1];
       TestCommon.CompareTestEqualAndConsistent (edecD, edecA);
       TestCommon.CompareTestEqual (EDecimal.Zero, edecRem);
       edecE = edecC.DivideToExponent (edecB, 0, ERounding.Down);
       // Testing that DivRemNaturalScale and division method return
       // the same value
       TestCommon.CompareTestEqualAndConsistent (edecD, edecE);
       edecE = edecC.RemainderNaturalScale (edecB, null);
       TestCommon.CompareTestEqualAndConsistent (edecRem, edecE);
     }
     if (!edecA.IsZero) {
       EDecimal [] divrem = edecC.DivRemNaturalScale (edecA);
       edecD = divrem [0].Plus (null);
       edecRem = divrem [1];
       TestCommon.CompareTestEqualAndConsistent (edecD, edecB);
       TestCommon.CompareTestEqual (EDecimal.Zero, edecRem);
     }
     if (!edecB.IsZero) {
       EDecimal [] divrem = edecA.DivRemNaturalScale (edecB);
       edecC = divrem [0].Plus (null);
       edecRem = divrem [1];
       edecD = edecB.Multiply (edecC);
       edecD = edecD.Add (edecRem);
       TestCommon.CompareTestEqualAndConsistent (edecA, edecD);
     }
     // -----------------------------------
     // EFloat
     // -----------------------------------
     EFloat efloatA = EFloat.FromEInteger (bigintA);
     EFloat efloatB = EFloat.FromEInteger (bigintB);
     EFloat efloatC = efloatA.Multiply (efloatB);
     EFloat efloatRem;
     EFloat efloatE;
     EFloat efloatD;
     TestCommon.CompareTestEqualAndConsistent (
       efloatC,
       efloatB.Multiply (efloatA));
     if (!efloatB.IsZero) {
       EFloat [] divrem = efloatC.DivRemNaturalScale (efloatB);
       efloatD = divrem [0].Plus (null);
       efloatRem = divrem [1];
       TestCommon.CompareTestEqualAndConsistent (efloatD, efloatA);
       TestCommon.CompareTestEqual (EFloat.Zero, efloatRem);
       efloatE = efloatC.DivideToExponent (efloatB, 0, ERounding.Down);
       // Testing that DivRemNaturalScale and division method return
       // the same value
       TestCommon.CompareTestEqualAndConsistent (efloatD, efloatE);
       efloatE = efloatC.RemainderNaturalScale (efloatB, null);
       TestCommon.CompareTestEqualAndConsistent (efloatRem, efloatE);
     }
     if (!efloatA.IsZero) {
       EFloat [] divrem = efloatC.DivRemNaturalScale (efloatA);
       efloatD = divrem [0].Plus (null);
       efloatRem = divrem [1];
       TestCommon.CompareTestEqualAndConsistent (efloatD, efloatB);
       TestCommon.CompareTestEqual (EFloat.Zero, efloatRem);
     }
     if (!efloatB.IsZero) {
       EFloat [] divrem = efloatA.DivRemNaturalScale (efloatB);
       efloatC = divrem [0].Plus (null);
       efloatRem = divrem [1];
       efloatD = efloatB.Multiply (efloatC);
       efloatD = efloatD.Add (efloatRem);
       TestCommon.CompareTestEqualAndConsistent (efloatA, efloatD);
     }
   } catch (Exception ex) {
 string testLine =
       "TestMultiplyDivideOne (\nEInteger.FromRadixString (\"" +
       bigintA.ToRadixString (16) + "\",16),\nEInteger.FromRadixString (\"" +
             bigintB.ToRadixString (16) + "\",16));";
     Console.WriteLine(testLine);
     throw new InvalidOperationException(ex.Message + "\n"+testLine,ex);
   }
 }
Example #4
0
   public static EInteger ReduceTrailingZeros(
       EInteger bigmant,
       FastInteger exponentMutable,
       int radix,
       FastInteger digits,
       FastInteger precision,
       FastInteger idealExp)
   {
 #if DEBUG
       if (precision != null && digits == null)
       {
           throw new ArgumentException("doesn't satisfy precision==null ||" +
                                       "\u0020digits!=null");
       }
       if (!(bigmant.Sign >= 0))
       {
           throw new ArgumentException("doesn't satisfy bigmant.Sign >= 0");
       }
 #endif
       if (bigmant.IsZero)
       {
           exponentMutable.SetInt(0);
           return(bigmant);
       }
       if (radix == 2)
       {
           if (!bigmant.IsEven)
           {
               return(bigmant);
           }
           long lowbit = bigmant.GetLowBitAsInt64();
           if (lowbit != Int64.MaxValue)
           {
               if (precision != null && digits.CompareTo(precision) >= 0)
               {
                   // Limit by digits minus precision
                   EInteger tmp = digits.ToEInteger().Subtract(precision.ToEInteger());
                   if (tmp.CompareTo(EInteger.FromInt64(lowbit)) < 0)
                   {
                       lowbit = tmp.ToInt64Checked();
                   }
               }
               if (idealExp != null && exponentMutable.CompareTo(idealExp) <= 0)
               {
                   // Limit by idealExp minus exponentMutable
                   EInteger tmp =
                       idealExp.ToEInteger().Subtract(exponentMutable.ToEInteger());
                   if (tmp.CompareTo(EInteger.FromInt64(lowbit)) < 0)
                   {
                       lowbit = tmp.ToInt64Checked();
                   }
               }
               bigmant = (lowbit <= Int32.MaxValue) ?
                         bigmant.ShiftRight((int)lowbit) :
                         bigmant.ShiftRight(EInteger.FromInt64(lowbit));
               if (digits != null)
               {
                   digits.SubtractInt64(lowbit);
               }
               if (exponentMutable != null)
               {
                   exponentMutable.AddInt64(lowbit);
               }
               return(bigmant);
           }
       }
       var bigradix    = (EInteger)radix;
       var bitsToShift = new FastInteger(0);
       while (!bigmant.IsZero)
       {
           if (precision != null && digits.CompareTo(precision) == 0)
           {
               break;
           }
           if (idealExp != null && exponentMutable.CompareTo(idealExp) == 0)
           {
               break;
           }
           EInteger   bigrem;
           EInteger   bigquo;
           EInteger[] divrem = bigmant.DivRem(bigradix);
           bigquo = divrem[0];
           bigrem = divrem[1];
           if (!bigrem.IsZero)
           {
               break;
           }
           bigmant = bigquo;
           exponentMutable.Increment();
           if (digits != null)
           {
               digits.Decrement();
           }
       }
       return(bigmant);
   }
Example #5
0
    public static EInteger ReduceTrailingZeros(
      EInteger bigmant,
      FastInteger exponentMutable,
      int radix,
      FastInteger digits,
      FastInteger precision,
      FastInteger idealExp) {
      #if DEBUG
      if (precision != null && digits == null) {
throw new ArgumentException("doesn't satisfy precision==null || digits!=null");
      }
      #endif
      if (bigmant.IsZero) {
        exponentMutable.SetInt(0);
        return bigmant;
      }
      var bigradix = (EInteger)radix;
      var bitToTest = 0;
      var bitsToShift = new FastInteger(0);
      while (!bigmant.IsZero) {
        if (precision != null && digits.CompareTo(precision) == 0) {
          break;
        }
        if (idealExp != null && exponentMutable.CompareTo(idealExp) == 0) {
          break;
        }
        if (radix == 2) {
          if (bitToTest < Int32.MaxValue) {
            if (bigmant.GetSignedBit(bitToTest)) {
              break;
            }
            ++bitToTest;
            bitsToShift.Increment();
          } else {
            if (!bigmant.IsEven) {
              break;
            }
            bigmant >>= 1;
          }
        } else {
          EInteger bigrem;
          EInteger bigquo;
{
EInteger[] divrem = bigmant.DivRem(bigradix);
bigquo = divrem[0];
bigrem = divrem[1]; }
          if (!bigrem.IsZero) {
            break;
          }
          bigmant = bigquo;
        }
        exponentMutable.Increment();
        if (digits != null) {
          digits.Decrement();
        }
      }
      if (radix == 2 && !bitsToShift.IsValueZero) {
        while (bitsToShift.CompareToInt(1000000) > 0) {
          bigmant >>= 1000000;
          bitsToShift.SubtractInt(1000000);
        }
        int tmpshift = bitsToShift.AsInt32();
        bigmant >>= tmpshift;
      }
      return bigmant;
    }
Example #6
0
   public static EInteger DivRem(
 EInteger dividend,
 EInteger divisor,
 out EInteger remainder) {
     if (dividend == null) {
       throw new ArgumentNullException("dividend");
     }
     if (divisor == null) {
       throw new ArgumentNullException("divisor");
     }
     EInteger[] result = dividend.DivRem(divisor);
     remainder = result[1];
     return result[0];
   }