本文整理汇总了C#中BigInteger.Divide方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.Divide方法的具体用法?C# BigInteger.Divide怎么用?C# BigInteger.Divide使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.Divide方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecodeMod3Tight
/// <summary>
/// Converts a byte array produced by EncodeMod3Tight(int[]) back to an <c>int</c> array
/// </summary>
///
/// <param name="Data">The byte array</param>
/// <param name="N">The number of coefficients</param>
///
/// <returns>The decoded array</returns>
public static int[] DecodeMod3Tight(byte[] Data, int N)
{
BigInteger sum = new BigInteger(1, Data);
int[] coeffs = new int[N];
for (int i = 0; i < N; i++)
{
coeffs[i] = sum.Mod(THREE).ToInt32() - 1;
if (coeffs[i] > 1)
coeffs[i] -= 3;
sum = sum.Divide(THREE);
}
return coeffs;
}
示例2: TestDivide
public void TestDivide()
{
for (int i = -5; i <= 5; ++i)
{
try
{
Val(i).Divide(Zero);
Assert.Fail("expected ArithmeticException");
}
catch (ArithmeticException)
{
}
}
const int product = 1*2*3*4*5*6*7*8*9;
const int productPlus = product + 1;
BigInteger bigProduct = Val(product);
BigInteger bigProductPlus = Val(productPlus);
for (int divisor = 1; divisor < 10; ++divisor)
{
// Exact division
BigInteger expected = Val(product/divisor);
Assert.AreEqual(expected, bigProduct.Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProduct.Negate().Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProduct.Divide(Val(divisor).Negate()));
Assert.AreEqual(expected, bigProduct.Negate().Divide(Val(divisor).Negate()));
expected = Val((product + 1)/divisor);
Assert.AreEqual(expected, bigProductPlus.Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProductPlus.Negate().Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProductPlus.Divide(Val(divisor).Negate()));
Assert.AreEqual(expected, bigProductPlus.Negate().Divide(Val(divisor).Negate()));
}
for (int rep = 0; rep < 10; ++rep)
{
var a = new BigInteger(100 - rep, 0, Rnd);
var b = new BigInteger(100 + rep, 0, Rnd);
var c = new BigInteger(10 + rep, 0, Rnd);
BigInteger d = a.Multiply(b).Add(c);
BigInteger e = d.Divide(a);
Assert.AreEqual(b, e);
}
// Special tests for power of two since uses different code path internally
for (int i = 0; i < 100; ++i)
{
int shift = Rnd.Next(64);
BigInteger a = One.ShiftLeft(shift);
var b = new BigInteger(64 + Rnd.Next(64), Rnd);
BigInteger bShift = b.ShiftRight(shift);
string data = "shift=" + shift + ", b=" + b.ToString(16);
Assert.AreEqual(bShift, b.Divide(a), data);
Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
}
// Regression
{
int shift = 63;
BigInteger a = One.ShiftLeft(shift);
var b = new BigInteger(1, "2504b470dc188499".HexToBytes());
BigInteger bShift = b.ShiftRight(shift);
string data = "shift=" + shift + ", b=" + b.ToString(16);
Assert.AreEqual(bShift, b.Divide(a), data);
Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
// Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
}
}
示例3: Jacobi
/// <summary>
/// Computes the value of the Jacobi symbol (A|B).
/// </summary>
///
/// <param name="A">The integer value</param>
/// <param name="B">The integer value</param>
///
/// <returns>Returns value of the jacobi symbol (A|B)</returns>
public static int Jacobi(BigInteger A, BigInteger B)
{
BigInteger a, b, v;
long k = 1;
// test trivial cases
if (B.Equals(ZERO))
{
a = A.Abs();
return a.Equals(ONE) ? 1 : 0;
}
if (!A.TestBit(0) && !B.TestBit(0))
return 0;
a = A;
b = B;
if (b.Signum() == -1)
{ // b < 0
b = b.Negate();
if (a.Signum() == -1)
k = -1;
}
v = ZERO;
while (!b.TestBit(0))
{
v = v.Add(ONE);
b = b.Divide(TWO);
}
if (v.TestBit(0))
k = k * _jacobiTable[a.ToInt32() & 7];
if (a.Signum() < 0)
{
if (b.TestBit(1))
k = -k;
a = a.Negate();
}
// main loop
while (a.Signum() != 0)
{
v = ZERO;
while (!a.TestBit(0))
{ // a is even
v = v.Add(ONE);
a = a.Divide(TWO);
}
if (v.TestBit(0))
k = k * _jacobiTable[b.ToInt32() & 7];
if (a.CompareTo(b) < 0)
{
// swap and correct intermediate result
BigInteger x = a;
a = b;
b = x;
if (a.TestBit(1) && b.TestBit(1))
k = -k;
}
a = a.Subtract(b);
}
return b.Equals(ONE) ? (int)k : 0;
}
示例4: Divide_by_zero_throws
public void Divide_by_zero_throws()
{
BigInteger x = new BigInteger(1, new uint[] { 0xFFFFFFFF} );
BigInteger y = new BigInteger(0, new uint[0]);
x.Divide(y);
}
示例5: TestDiv
private void TestDiv(BigInteger i1, BigInteger i2)
{
BigInteger q = i1.Divide(i2);
BigInteger r = i1.Remainder(i2);
BigInteger remainder;
BigInteger quotient = i1.DivideAndRemainder(i2, out remainder);
Assert.IsTrue(q.Equals(quotient), "Divide and DivideAndRemainder do not agree");
Assert.IsTrue(r.Equals(remainder), "Remainder and DivideAndRemainder do not agree");
Assert.IsTrue(q.Sign != 0 || q.Equals(zero), "signum and equals(zero) do not agree on quotient");
Assert.IsTrue(r.Sign != 0 || r.Equals(zero), "signum and equals(zero) do not agree on remainder");
Assert.IsTrue(q.Sign == 0 || q.Sign == i1.Sign * i2.Sign, "wrong sign on quotient");
Assert.IsTrue(r.Sign == 0 || r.Sign == i1.Sign, "wrong sign on remainder");
Assert.IsTrue(r.Abs().CompareTo(i2.Abs()) < 0, "remainder out of range");
Assert.IsTrue(q.Abs().Add(one).Multiply(i2.Abs()).CompareTo(i1.Abs()) > 0, "quotient too small");
Assert.IsTrue(q.Abs().Multiply(i2.Abs()).CompareTo(i1.Abs()) <= 0, "quotient too large");
BigInteger p = q.Multiply(i2);
BigInteger a = p.Add(r);
Assert.IsTrue(a.Equals(i1), "(a/b)*b+(a%b) != a");
try {
BigInteger mod = i1.Mod(i2);
Assert.IsTrue(mod.Sign >= 0, "mod is negative");
Assert.IsTrue(mod.Abs().CompareTo(i2.Abs()) < 0, "mod out of range");
Assert.IsTrue(r.Sign < 0 || r.Equals(mod), "positive remainder == mod");
Assert.IsTrue(r.Sign >= 0 || r.Equals(mod.Subtract(i2)), "negative remainder == mod - divisor");
} catch (ArithmeticException e) {
Assert.IsTrue(i2.Sign <= 0, "mod fails on negative divisor only");
}
}