java.math.BigInteger.signum()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(224)

本文整理了Java中java.math.BigInteger.signum()方法的一些代码示例,展示了BigInteger.signum()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.signum()方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:signum

BigInteger.signum介绍

[英]sign field, used for serialization.
[中]符号字段,用于序列化。

代码示例

代码示例来源:origin: google/guava

/** Returns {@code true} if {@code x} represents a power of two. */
public static boolean isPowerOfTwo(BigInteger x) {
 checkNotNull(x);
 return x.signum() > 0 && x.getLowestSetBit() == x.bitLength() - 1;
}

代码示例来源:origin: stackoverflow.com

/** the constant 2^64 */
private static final BigInteger TWO_64 = BigInteger.ONE.shiftLeft(64);

public String asUnsignedDecimalString(long l) {
  BigInteger b = BigInteger.valueOf(l);
  if(b.signum() < 0) {
   b = b.add(TWO_64);
  }
  return b.toString();
}

代码示例来源:origin: org.apache.commons/commons-math3

/**
 * <p>
 * Compares this object to another based on size.
 * </p>
 *
 * @param object
 *            the object to compare to, must not be <code>null</code>.
 * @return -1 if this is less than {@code object}, +1 if this is greater
 *         than {@code object}, 0 if they are equal.
 * @see java.lang.Comparable#compareTo(java.lang.Object)
 */
public int compareTo(final BigFraction object) {
  int lhsSigNum = numerator.signum();
  int rhsSigNum = object.numerator.signum();
  if (lhsSigNum != rhsSigNum) {
    return (lhsSigNum > rhsSigNum) ? 1 : -1;
  }
  if (lhsSigNum == 0) {
    return 0;
  }
  BigInteger nOd = numerator.multiply(object.denominator);
  BigInteger dOn = denominator.multiply(object.numerator);
  return nOd.compareTo(dOn);
}

代码示例来源:origin: org.apache.commons/commons-compress

BigInteger toSkip = len.add(BigInteger.valueOf(DWORD - suspectLocalFileHeader.length
  - APK_SIGNING_BLOCK_MAGIC.length));
byte[] magic = new byte[APK_SIGNING_BLOCK_MAGIC.length];
  if (toSkip.signum() < 0) {
    while (toSkip.compareTo(LONG_MAX) > 0) {
      realSkip(Long.MAX_VALUE);
      toSkip = toSkip.add(LONG_MAX.negate());

代码示例来源:origin: robovm/robovm

if (remainder.signum() == 0) {
  return new BigDecimal(quotient, scale);
int sign = scaledDividend.signum() * scaledDivisor.signum();
int compRem;                                      // 'compare to remainder'
if(scaledDivisor.bitLength() < 63) { // 63 in order to avoid out of long after *2
  long rem = remainder.longValue();
  long divisor = scaledDivisor.longValue();
  compRem = remainder.abs().shiftLeftOneBit().compareTo(scaledDivisor.abs());
  compRem = roundingBehavior(quotient.testBit(0) ? 1 : 0,
      sign * (5 + compRem), roundingMode);
  if(quotient.bitLength() < 63) {
    return valueOf(quotient.longValue() + compRem,scale);
  quotient = quotient.add(BigInteger.valueOf(compRem));
  return new BigDecimal(quotient, scale);

代码示例来源:origin: robovm/robovm

if (this.a.signum() < 0 || this.a.compareTo(p) >= 0) {
  throw new IllegalArgumentException("the a is not in the field");
if (this.b.signum() < 0 || this.b.compareTo(p) >= 0) {
  throw new IllegalArgumentException("the b is not in the field");
if (!(this.a.bitLength() <= fieldSizeInBits)) {
  throw new IllegalArgumentException("the a is not in the field");
if (!(this.b.bitLength() <= fieldSizeInBits)) {
  throw new IllegalArgumentException("the b is not in the field");

代码示例来源:origin: robovm/robovm

if (k.compareTo(q) != -1) {
  continue;
if (r.signum() == 0) {
  continue;
s = k.modInverse(q).multiply(digestBI.add(x.multiply(r)).mod(q))
    .mod(q);
if (s.signum() != 0) {
  break;

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Power of a BigInteger to a BigInteger certain exponent. Called by the '**' operator.
 *
 * @param self     a BigInteger
 * @param exponent a BigInteger exponent
 * @return a BigInteger to the power of a the exponent
 * @since 2.3.8
 */
public static BigInteger power(BigInteger self, BigInteger exponent) {
  if ((exponent.signum() >= 0) && (exponent.compareTo(BI_INT_MAX) <= 0)) {
    return self.pow(exponent.intValue());
  } else {
    return BigDecimal.valueOf(Math.pow(self.doubleValue(), exponent.doubleValue())).toBigInteger();
  }
}

代码示例来源:origin: RoaringBitmap/RoaringBitmap

/**
  * JDK8 Long.toUnsignedString was too complex to backport. Go for a slow version relying on
  * BigInteger
  */
 // https://stackoverflow.com/questions/7031198/java-signed-long-to-unsigned-long-string
 static String toUnsignedString(long l) {
  BigInteger b = BigInteger.valueOf(l);
  if (b.signum() < 0) {
   b = b.add(TWO_64);
  }
  return b.toString();
 }
}

代码示例来源:origin: google/guava

/**
 * Returns a {@code UnsignedInteger} representing the same value as the specified {@link
 * BigInteger}. This is the inverse operation of {@link #bigIntegerValue()}.
 *
 * @throws IllegalArgumentException if {@code value} is negative or {@code value >= 2^32}
 */
public static UnsignedInteger valueOf(BigInteger value) {
 checkNotNull(value);
 checkArgument(
   value.signum() >= 0 && value.bitLength() <= Integer.SIZE,
   "value (%s) is outside the range for an unsigned integer value",
   value);
 return fromIntBits(value.intValue());
}

代码示例来源:origin: stackoverflow.com

if(denominator.signum() < 0)
return new BigFraction(numerator.multiply(f.denominator).add(denominator.multiply(f.numerator)),
            denominator.multiply(f.denominator));
return new BigFraction(numerator.add(denominator.multiply(b)),
            denominator, true);
return numerator.signum();
 return numerator.compareTo(f.numerator);
return numerator.multiply(f.denominator).compareTo(denominator.multiply(f.numerator));
int precision = Math.max(numerator.bitLength(), denominator.bitLength());
precision = (int)Math.ceil(precision / LG10);
if(denominator.signum() < 0)

代码示例来源:origin: robovm/robovm

n = p.bitLength();
if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
  throw new InvalidKeyException("bad p");
if (q.signum() != 1 && q.bitLength() != 160) {
  throw new InvalidKeyException("bad q");
if (x.signum() != 1 || x.compareTo(q) != -1) {
  throw new InvalidKeyException("x <= 0 || x >= q");

代码示例来源:origin: stackoverflow.com

if (denominator.signum() == 0) {
    throw new IllegalArgumentException("denominator is zero");
  if (numerator.signum() == 0) {
    return ZERO;
  if (denominator.signum() < 0) {
    numerator = numerator.negate();
    denominator = denominator.negate();
  if (numerator.signum() != o.numerator.signum()) {
    return numerator.signum() - o.numerator.signum();
  } else {
  if (o.numerator.signum() == 0) {
    return this;
  } else if (numerator.signum() == 0) {
    return o;
  } else if (denominator.equals(o.denominator)) {
    return new BigRational(numerator.add(o.numerator), denominator);
  } else {
    return canonical(numerator.multiply(o.denominator).add(o.numerator.multiply(denominator)), denominator.multiply(o.denominator), true);
  if (numerator.signum() == 0 || o.numerator.signum( )== 0) {
    return ZERO;
  } else if (numerator.equals(o.denominator)) {
public boolean isInteger() { return numerator.signum() == 0 || denominator.equals(BigInteger.ONE); }
public BigRational negate() { return new BigRational(numerator.negate(), denominator); }

代码示例来源:origin: google/guava

@GwtIncompatible // TODO
public void testSqrtCeiling() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  for (RoundingMode mode : asList(CEILING, UP)) {
   BigInteger result = BigIntegerMath.sqrt(x, mode);
   assertTrue(result.compareTo(ZERO) > 0);
   assertTrue(result.pow(2).compareTo(x) >= 0);
   assertTrue(result.signum() == 0 || result.subtract(ONE).pow(2).compareTo(x) < 0);
  }
 }
}

代码示例来源:origin: web3j/web3j

@Override
protected synchronized BigInteger getNonce() throws IOException {
  if (nonce.signum() == -1) {
    // obtain lock
    nonce = super.getNonce();
  } else {
    nonce = nonce.add(BigInteger.ONE);
  }
  return nonce;
}

代码示例来源:origin: google/guava

/**
 * Returns a {@code UnsignedLong} representing the same value as the specified {@code BigInteger}.
 * This is the inverse operation of {@link #bigIntegerValue()}.
 *
 * @throws IllegalArgumentException if {@code value} is negative or {@code value >= 2^64}
 */
@CanIgnoreReturnValue
public static UnsignedLong valueOf(BigInteger value) {
 checkNotNull(value);
 checkArgument(
   value.signum() >= 0 && value.bitLength() <= Long.SIZE,
   "value (%s) is outside the range for an unsigned long value",
   value);
 return fromLongBits(value.longValue());
}

代码示例来源:origin: stackoverflow.com

if (n.signum() >= 0)
  final int bitLength = n.bitLength();
  BigInteger root = BigInteger.ONE.shiftLeft(bitLength / 2);
    root = root.add(n.divide(root)).divide(TWO);
final BigInteger upperBound = root.add(BigInteger.ONE).pow(2);
return lowerBound.compareTo(n) <= 0
  && n.compareTo(upperBound) < 0;

代码示例来源:origin: robovm/robovm

n1 = p.bitLength();
if (p.compareTo(BigInteger.valueOf(1)) != 1 || n1 < 512 || n1 > 1024 || (n1 & 077) != 0) {
  throw new InvalidKeyException("bad p");
if (q.signum() != 1 || q.bitLength() != 160) {
  throw new InvalidKeyException("bad q");
if (y.signum() != 1) {
  throw new InvalidKeyException("y <= 0");

代码示例来源:origin: web3j/web3j

verifyPrecondition(sig.r.signum() >= 0, "r must be positive");
verifyPrecondition(sig.s.signum() >= 0, "s must be positive");
verifyPrecondition(message != null, "message cannot be null");
BigInteger x = sig.r.add(i.multiply(n));
if (x.compareTo(prime) >= 0) {

代码示例来源:origin: org.apache.commons/commons-math3

MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
if (den.signum() == 0) {
  throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
if (num.signum() == 0) {
  numerator   = BigInteger.ZERO;
  denominator = BigInteger.ONE;
  if (BigInteger.ONE.compareTo(gcd) < 0) {
    num = num.divide(gcd);
    den = den.divide(gcd);
  if (den.signum() == -1) {
    num = num.negate();
    den = den.negate();

相关文章