本文整理匯總了C#中BigInteger.Abs方法的典型用法代碼示例。如果您正苦於以下問題:C# BigInteger.Abs方法的具體用法?C# BigInteger.Abs怎麽用?C# BigInteger.Abs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類BigInteger
的用法示例。
在下文中一共展示了BigInteger.Abs方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: TestAbs
public void TestAbs()
{
var value = new BigInteger(-50);
var absolute = value.Abs();
Assert.AreEqual(new BigInteger(50), absolute);
}
示例2: Multiply
public virtual ECPoint Multiply(ECPoint p, BigInteger k)
{
int sign = k.SignValue;
if (sign == 0 || p.IsInfinity)
return p.Curve.Infinity;
ECPoint positive = MultiplyPositive(p, k.Abs());
return sign > 0 ? positive : positive.Negate();
}
示例3: 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);
}
示例4: CalculateB
protected virtual BigInteger CalculateB(BigInteger k, BigInteger g, int t)
{
bool negative = (g.SignValue < 0);
BigInteger b = k.Multiply(g.Abs());
bool extra = b.TestBit(t - 1);
b = b.ShiftRight(t);
if (extra)
{
b = b.Add(BigInteger.One);
}
return negative ? b.Negate() : b;
}
示例5: Multiply
public virtual ECPoint Multiply(ECPoint p, BigInteger k)
{
int sign = k.SignValue;
if (sign == 0 || p.IsInfinity)
return p.Curve.Infinity;
ECPoint positive = MultiplyPositive(p, k.Abs());
ECPoint result = sign > 0 ? positive : positive.Negate();
/*
* Although the various multipliers ought not to produce invalid output under normal
* circumstances, a final check here is advised to guard against fault attacks.
*/
return ECAlgorithms.ValidatePoint(result);
}
示例6: ReferenceMultiply
/**
* Simple shift-and-add multiplication. Serves as reference implementation
* to verify (possibly faster) implementations, and for very small scalars.
*
* @param p
* The point to multiply.
* @param k
* The multiplier.
* @return The result of the point multiplication <code>kP</code>.
*/
public static ECPoint ReferenceMultiply(ECPoint p, BigInteger k)
{
BigInteger x = k.Abs();
ECPoint q = p.Curve.Infinity;
int t = x.BitLength;
if (t > 0)
{
if (x.TestBit(0))
{
q = p;
}
for (int i = 1; i < t; i++)
{
p = p.Twice();
if (x.TestBit(i))
{
q = q.Add(p);
}
}
}
return k.SignValue < 0 ? q.Negate() : q;
}
示例7: ModReduce
protected virtual BigInteger ModReduce(BigInteger x)
{
if (r == null)
{
x = x.Mod(q);
}
else
{
bool negative = x.SignValue < 0;
if (negative)
{
x = x.Abs();
}
int qLen = q.BitLength;
if (r.SignValue > 0)
{
BigInteger qMod = BigInteger.One.ShiftLeft(qLen);
bool rIsOne = r.Equals(BigInteger.One);
while (x.BitLength > (qLen + 1))
{
BigInteger u = x.ShiftRight(qLen);
BigInteger v = x.Remainder(qMod);
if (!rIsOne)
{
u = u.Multiply(r);
}
x = u.Add(v);
}
}
else
{
int d = ((qLen - 1) & 31) + 1;
BigInteger mu = r.Negate();
BigInteger u = mu.Multiply(x.ShiftRight(qLen - d));
BigInteger quot = u.ShiftRight(qLen + d);
BigInteger v = quot.Multiply(q);
BigInteger bk1 = BigInteger.One.ShiftLeft(qLen + d);
v = v.Remainder(bk1);
x = x.Remainder(bk1);
x = x.Subtract(v);
if (x.SignValue < 0)
{
x = x.Add(bk1);
}
}
while (x.CompareTo(q) >= 0)
{
x = x.Subtract(q);
}
if (negative && x.SignValue != 0)
{
x = q.Subtract(x);
}
}
return x;
}
示例8: BIDivide
public static object BIDivide(BigInteger n, BigInteger d)
{
if (d.Equals(BigInteger.ZERO))
throw new ArithmeticException("Divide by zero");
//BigInteger gcd = n.gcd(d);
BigInteger gcd = n.Gcd(d);
if (gcd.Equals(BigInteger.ZERO))
return 0;
//n = n.divide(gcd);
//d = d.divide(gcd);
n = n / gcd;
d = d / gcd;
if (d.Equals(BigInteger.ONE))
return reduce(n);
//return new Ratio((d.signum() < 0 ? n.negate() : n),
// (d.signum() < 0 ? d.negate() : d));
return new Ratio((d.Signum < 0 ? -n : n), d.Abs());
}
示例9: PrintInteger
void PrintInteger(StringBuilder sb, BigInteger val)
{
StringBuilder sb1 = new StringBuilder();
bool neg = val.IsNegative;
BigInteger v = val.Abs();
PrintLeadingSign(sb1,neg);
if ( _conversion == ConversionAux.DecimalInteger )
PrintMagnitude(sb1,neg,v.ToString());
else
{
string s = v.ToString( _conversion == ConversionAux.OctalInteger ? 8u : 16u );
PrintIntOctHex(sb1,s,neg,true);
}
PrintTrailingSign(sb1,neg);
PrintWithJustification(sb,sb1.ToString());
}
示例10: Abs_zero_is_zero
public void Abs_zero_is_zero()
{
BigInteger z = new BigInteger(0);
Expect(z.Abs().IsZero);
}
示例11: ToBinary
internal static string ToBinary(BigInteger val) {
string res = ToBinary(val.Abs(), true, true);
if (val.IsNegative()) {
res = "-" + res;
}
return res;
}
示例12: ImplShamirsTrickWNaf
internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPointMap pointMapQ, BigInteger l)
{
bool negK = k.SignValue < 0, negL = l.SignValue < 0;
k = k.Abs();
l = l.Abs();
int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(System.Math.Max(k.BitLength, l.BitLength))));
ECPoint Q = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMapQ);
WNafPreCompInfo infoP = WNafUtilities.GetWNafPreCompInfo(P);
WNafPreCompInfo infoQ = WNafUtilities.GetWNafPreCompInfo(Q);
ECPoint[] preCompP = negK ? infoP.PreCompNeg : infoP.PreComp;
ECPoint[] preCompQ = negL ? infoQ.PreCompNeg : infoQ.PreComp;
ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg;
ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg;
byte[] wnafP = WNafUtilities.GenerateWindowNaf(width, k);
byte[] wnafQ = WNafUtilities.GenerateWindowNaf(width, l);
return ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ);
}
示例13: 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");
}
}
示例14: BitLength
// Like GetBitCount(Abs(x)), except 0 maps to 0
public static int BitLength(BigInteger x)
{
if (x.IsZero())
{
return 0;
}
return x.Abs().GetBitCount();
}
示例15: IntToOctets
/// <summary>
/// Convert a BigInteger to bytes
/// </summary>
///
/// <param name="X">The BigInteger</param>
///
/// <returns>Returns the BigInteger as a byte array</returns>
public static byte[] IntToOctets(BigInteger X)
{
byte[] valBytes = X.Abs().ToByteArray();
// check whether the array includes a sign bit
if ((X.BitLength & 7) != 0)
return valBytes;
// get rid of the sign bit (first byte)
byte[] tmp = new byte[X.BitLength >> 3];
Array.Copy(valBytes, 1, tmp, 0, tmp.Length);
return tmp;
}