本文整理汇总了C#中BigInteger.ToInt64方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.ToInt64方法的具体用法?C# BigInteger.ToInt64怎么用?C# BigInteger.ToInt64使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.ToInt64方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BigInteger2Double
/** @see BigInteger#ToDouble() */
public static double BigInteger2Double(BigInteger val)
{
// val.bitLength() < 64
if ((val.numberLength < 2)
|| ((val.numberLength == 2) && (val.Digits[1] > 0))) {
return val.ToInt64();
}
// val.bitLength() >= 33 * 32 > 1024
if (val.numberLength > 32) {
return ((val.Sign > 0) ? Double.PositiveInfinity
: Double.NegativeInfinity);
}
int bitLen = val.Abs().BitLength;
long exponent = bitLen - 1;
int delta = bitLen - 54;
// We need 54 top bits from this, the 53th bit is always 1 in lVal.
long lVal = val.Abs().ShiftRight(delta).ToInt64();
/*
* Take 53 bits from lVal to mantissa. The least significant bit is
* needed for rounding.
*/
long mantissa = lVal & 0x1FFFFFFFFFFFFFL;
if (exponent == 1023) {
if (mantissa == 0X1FFFFFFFFFFFFFL) {
return ((val.Sign > 0) ? Double.PositiveInfinity
: Double.NegativeInfinity);
}
if (mantissa == 0x1FFFFFFFFFFFFEL) {
return ((val.Sign > 0) ? Double.MaxValue : -Double.MaxValue);
}
}
// Round the mantissa
if (((mantissa & 1) == 1)
&& (((mantissa & 2) == 2) || BitLevel.NonZeroDroppedBits(delta,
val.Digits))) {
mantissa += 2;
}
mantissa >>= 1; // drop the rounding bit
// long resSign = (val.sign < 0) ? 0x8000000000000000L : 0;
long resSign = (val.Sign < 0) ? Int64.MinValue : 0;
exponent = ((1023 + exponent) << 52) & 0x7FF0000000000000L;
long result = resSign | exponent | mantissa;
return BitConverter.Int64BitsToDouble(result);
}
示例2: GcdBinary
/// <summary>
/// Return the greatest common divisor of X and Y
/// </summary>
///
/// <param name="X">Operand 1, must be greater than zero</param>
/// <param name="Y">Operand 2, must be greater than zero</param>
///
/// <returns>Returns <c>GCD(X, Y)</c></returns>
internal static BigInteger GcdBinary(BigInteger X, BigInteger Y)
{
// Divide both number the maximal possible times by 2 without rounding * gcd(2*a, 2*b) = 2 * gcd(a,b)
int lsb1 = X.LowestSetBit;
int lsb2 = Y.LowestSetBit;
int pow2Count = System.Math.Min(lsb1, lsb2);
BitLevel.InplaceShiftRight(X, lsb1);
BitLevel.InplaceShiftRight(Y, lsb2);
BigInteger swap;
// I want op2 > op1
if (X.CompareTo(Y) == BigInteger.GREATER)
{
swap = X;
X = Y;
Y = swap;
}
do
{ // INV: op2 >= op1 && both are odd unless op1 = 0
// Optimization for small operands (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
if ((Y._numberLength == 1) || ((Y._numberLength == 2) && (Y._digits[1] > 0)))
{
Y = BigInteger.ValueOf(Division.GcdBinary(X.ToInt64(), Y.ToInt64()));
break;
}
// Implements one step of the Euclidean algorithm
// To reduce one operand if it's much smaller than the other one
if (Y._numberLength > X._numberLength * 1.2)
{
Y = Y.Remainder(X);
if (Y.Signum() != 0)
BitLevel.InplaceShiftRight(Y, Y.LowestSetBit);
}
else
{
// Use Knuth's algorithm of successive subtract and shifting
do
{
Elementary.InplaceSubtract(Y, X); // both are odd
BitLevel.InplaceShiftRight(Y, Y.LowestSetBit); // op2 is even
} while (Y.CompareTo(X) >= BigInteger.EQUALS);
}
// now op1 >= op2
swap = Y;
Y = X;
X = swap;
} while (X._sign != 0);
return Y.ShiftLeft(pow2Count);
}
示例3: NextLong
/// <summary>
/// Get a pseudo random 64bit integer
/// </summary>
///
/// <returns>Random Int64</returns>
public long NextLong()
{
// Xi+1 = (X pow 2) mod N
_X = _X.Multiply(_X).Mod(_N);
return _X.ToInt64();
}
示例4: GcdBinary
/**
* @param m a positive modulus
* Return the greatest common divisor of op1 and op2,
*
* @param op1
* must be greater than zero
* @param op2
* must be greater than zero
* @see BigInteger#gcd(BigInteger)
* @return {@code GCD(op1, op2)}
*/
public static BigInteger GcdBinary(BigInteger op1, BigInteger op2)
{
// PRE: (op1 > 0) and (op2 > 0)
/*
* Divide both number the maximal possible times by 2 without rounding
* gcd(2*a, 2*b) = 2 * gcd(a,b)
*/
int lsb1 = op1.LowestSetBit;
int lsb2 = op2.LowestSetBit;
int pow2Count = System.Math.Min(lsb1, lsb2);
BitLevel.InplaceShiftRight(op1, lsb1);
BitLevel.InplaceShiftRight(op2, lsb2);
BigInteger swap;
// I want op2 > op1
if (op1.CompareTo(op2) == BigInteger.GREATER) {
swap = op1;
op1 = op2;
op2 = swap;
}
do {
// INV: op2 >= op1 && both are odd unless op1 = 0
// Optimization for small operands
// (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
if ((op2.numberLength == 1)
|| ((op2.numberLength == 2) && (op2.Digits[1] > 0))) {
op2 = BigInteger.ValueOf(Division.GcdBinary(op1.ToInt64(),
op2.ToInt64()));
break;
}
// Implements one step of the Euclidean algorithm
// To reduce one operand if it's much smaller than the other one
if (op2.numberLength > op1.numberLength*1.2) {
op2 = op2.Remainder(op1);
if (op2.Sign != 0)
BitLevel.InplaceShiftRight(op2, op2.LowestSetBit);
} else {
// Use Knuth's algorithm of successive subtract and shifting
do {
Elementary.inplaceSubtract(op2, op1); // both are odd
BitLevel.InplaceShiftRight(op2, op2.LowestSetBit); // op2 is even
} while (op2.CompareTo(op1) >= BigInteger.EQUALS);
}
// now op1 >= op2
swap = op2;
op2 = op1;
op1 = swap;
} while (op1.Sign != 0);
return op2.ShiftLeft(pow2Count);
}