当前位置: 首页>>代码示例>>Java>>正文


Java BigInteger.negate方法代码示例

本文整理汇总了Java中java.math.BigInteger.negate方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.negate方法的具体用法?Java BigInteger.negate怎么用?Java BigInteger.negate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.math.BigInteger的用法示例。


在下文中一共展示了BigInteger.negate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.math.BigInteger; //导入方法依赖的package包/类
public static void main(String[] args) {
    Random rnd = new Random(1234);

    for (int i=0; i<2000; i++) {
        BigInteger m = new BigInteger(800, rnd);
        BigInteger base = new BigInteger(16, rnd);
        if (rnd.nextInt() % 1 == 0)
            base = base.negate();
        BigInteger exp = new BigInteger(8, rnd);

        BigInteger z = base.modPow(exp, m);
        BigInteger w = base.pow(exp.intValue()).mod(m);
        if (!z.equals(w)){
            System.err.println(base +" ** " + exp + " mod "+ m);
            System.err.println("modPow : " + z);
            System.err.println("pow.mod: " + w);
            throw new RuntimeException("BigInteger modPow failure.");
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:21,代码来源:ModPow.java

示例2: roundToBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Returns the {@code BigInteger} value that is equal to {@code x} rounded with the specified
 * rounding mode, if possible.
 *
 * @throws ArithmeticException if
 *         <ul>
 *         <li>{@code x} is infinite or NaN
 *         <li>{@code x} is not a mathematical integer and {@code mode} is
 *         {@link RoundingMode#UNNECESSARY}
 *         </ul>
 */
@GwtIncompatible("#roundIntermediate, java.lang.Math.getExponent, "
    + "com.google_voltpatches.common.math.DoubleUtils")
public static BigInteger roundToBigInteger(double x, RoundingMode mode) {
  x = roundIntermediate(x, mode);
  if (MIN_LONG_AS_DOUBLE - x < 1.0 & x < MAX_LONG_AS_DOUBLE_PLUS_ONE) {
    return BigInteger.valueOf((long) x);
  }
  int exponent = getExponent(x);
  long significand = getSignificand(x);
  BigInteger result = BigInteger.valueOf(significand).shiftLeft(exponent - SIGNIFICAND_BITS);
  return (x < 0) ? result.negate() : result;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:24,代码来源:DoubleMath.java

示例3: sMod

import java.math.BigInteger; //导入方法依赖的package包/类
public void sMod(DataWord word) {

        if (word.isZero()) {
            this.and(ZERO);
            return;
        }

        BigInteger result = sValue().abs().mod(word.sValue().abs());
        result = (sValue().signum() == -1) ? result.negate() : result;

        this.data = ByteUtil.copyToArray(result.and(MAX_VALUE));
    }
 
开发者ID:talentchain,项目名称:talchain,代码行数:13,代码来源:DataWord.java

示例4: decodeBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * 将字符串类型的数字转换成BigInteger,支持十进制、十六进制、八进制写法
 * 
 * @param value
 * @return
 */
private static BigInteger decodeBigInteger(String value) {
	int radix = 10;
	int index = 0;
	boolean negative = false;

	if (value.startsWith("-")) {
		negative = true;
		index++;
	}

	if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
		index += 2;
		radix = 16;
	}
	else if (value.startsWith("#", index)) {
		index++;
		radix = 16;
	}
	else if (value.startsWith("0", index) && value.length() > 1 + index) {
		index++;
		radix = 8;
	}

	BigInteger result = new BigInteger(value.substring(index), radix);
	return (negative ? result.negate() : result);
}
 
开发者ID:penggle,项目名称:xproject,代码行数:33,代码来源:NumberUtils.java

示例5: format

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Format a BigInteger to produce a string.
 * @param number    The BigInteger to format
 * @param result    where the text is to be appended
 * @param delegate notified of locations of sub fields
 * @return The formatted number string
 * @exception        ArithmeticException if rounding is needed with rounding
 *                   mode being set to RoundingMode.UNNECESSARY
 * @see java.text.FieldPosition
 */
private StringBuffer format(BigInteger number, StringBuffer result,
                           FieldDelegate delegate, boolean formatLong) {
    if (multiplier != 1) {
        number = number.multiply(getBigIntegerMultiplier());
    }
    boolean isNegative = number.signum() == -1;
    if (isNegative) {
        number = number.negate();
    }

    synchronized(digitList) {
        int maxIntDigits, minIntDigits, maxFraDigits, minFraDigits, maximumDigits;
        if (formatLong) {
            maxIntDigits = super.getMaximumIntegerDigits();
            minIntDigits = super.getMinimumIntegerDigits();
            maxFraDigits = super.getMaximumFractionDigits();
            minFraDigits = super.getMinimumFractionDigits();
            maximumDigits = maxIntDigits + maxFraDigits;
        } else {
            maxIntDigits = getMaximumIntegerDigits();
            minIntDigits = getMinimumIntegerDigits();
            maxFraDigits = getMaximumFractionDigits();
            minFraDigits = getMinimumFractionDigits();
            maximumDigits = maxIntDigits + maxFraDigits;
            if (maximumDigits < 0) {
                maximumDigits = Integer.MAX_VALUE;
            }
        }

        digitList.set(isNegative, number,
                      useExponentialNotation ? maximumDigits : 0);

        return subformat(result, delegate, isNegative, true,
            maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:47,代码来源:DecimalFormat.java

示例6: divideLarge

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Sanity test for Burnikel-Ziegler division.  The Burnikel-Ziegler division
 * algorithm is used when each of the dividend and the divisor has at least
 * a specified number of ints in its representation.  This test is based on
 * the observation that if {@code w = u*pow(2,a)} and {@code z = v*pow(2,b)}
 * where {@code abs(u) > abs(v)} and {@code a > b && b > 0}, then if
 * {@code w/z = q1*z + r1} and {@code u/v = q2*v + r2}, then
 * {@code q1 = q2*pow(2,a-b)} and {@code r1 = r2*pow(2,b)}.  The test
 * ensures that {@code v} is just under the B-Z threshold, that {@code z} is
 * over the threshold and {@code w} is much larger than {@code z}. This
 * implies that {@code u/v} uses the standard division algorithm and
 * {@code w/z} uses the B-Z algorithm.  The results of the two algorithms
 * are then compared using the observation described in the foregoing and
 * if they are not equal a failure is logged.
 */
public static void divideLarge() {
    int failCount = 0;

    BigInteger base = BigInteger.ONE.shiftLeft(BITS_BURNIKEL_ZIEGLER + BITS_BURNIKEL_ZIEGLER_OFFSET - 33);
    for (int i=0; i<SIZE; i++) {
        BigInteger addend = new BigInteger(BITS_BURNIKEL_ZIEGLER + BITS_BURNIKEL_ZIEGLER_OFFSET - 34, rnd);
        BigInteger v = base.add(addend);

        BigInteger u = v.multiply(BigInteger.valueOf(2 + rnd.nextInt(Short.MAX_VALUE - 1)));

        if(rnd.nextBoolean()) {
            u = u.negate();
        }
        if(rnd.nextBoolean()) {
            v = v.negate();
        }

        int a = BITS_BURNIKEL_ZIEGLER_OFFSET + rnd.nextInt(16);
        int b = 1 + rnd.nextInt(16);
        BigInteger w = u.multiply(BigInteger.ONE.shiftLeft(a));
        BigInteger z = v.multiply(BigInteger.ONE.shiftLeft(b));

        BigInteger[] divideResult = u.divideAndRemainder(v);
        divideResult[0] = divideResult[0].multiply(BigInteger.ONE.shiftLeft(a - b));
        divideResult[1] = divideResult[1].multiply(BigInteger.ONE.shiftLeft(b));
        BigInteger[] bzResult = w.divideAndRemainder(z);

        if (divideResult[0].compareTo(bzResult[0]) != 0 ||
                divideResult[1].compareTo(bzResult[1]) != 0) {
            failCount++;
        }
    }

    report("divideLarge", failCount);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:51,代码来源:BigIntegerTest.java

示例7: decodeBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Decode a {@link java.math.BigInteger} from the supplied {@link String}
 * value.
 * <p>
 * Supports decimal, hex, and octal notation.
 * 
 * @see BigInteger#BigInteger(String, int)
 */
private static BigInteger decodeBigInteger(String value) {
	int radix = 10;
	int index = 0;
	boolean negative = false;

	// Handle minus sign, if present.
	if (value.startsWith("-")) {
		negative = true;
		index++;
	}

	// Handle radix specifier, if present.
	if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
		index += 2;
		radix = 16;
	} else if (value.startsWith("#", index)) {
		index++;
		radix = 16;
	} else if (value.startsWith("0", index) && value.length() > 1 + index) {
		index++;
		radix = 8;
	}

	BigInteger result = new BigInteger(value.substring(index), radix);
	return (negative ? result.negate() : result);
}
 
开发者ID:zhangjunfang,项目名称:util,代码行数:35,代码来源:NumberUtils.java

示例8: decodeBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Decode a {@link java.math.BigInteger} from the supplied {@link String} value.
 * <p>Supports decimal, hex, and octal notation.
 * @see BigInteger#BigInteger(String, int)
 */
private static BigInteger decodeBigInteger(String value) {
    int radix = 10;
    int index = 0;
    boolean negative = false;

    // Handle minus sign, if present.
    if (value.startsWith("-")) {
        negative = true;
        index++;
    }

    // Handle radix specifier, if present.
    if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
        index += 2;
        radix = 16;
    }
    else if (value.startsWith("#", index)) {
        index++;
        radix = 16;
    }
    else if (value.startsWith("0", index) && value.length() > 1 + index) {
        index++;
        radix = 8;
    }

    BigInteger result = new BigInteger(value.substring(index), radix);
    return (negative ? result.negate() : result);
}
 
开发者ID:keepcosmos,项目名称:beanmother,代码行数:34,代码来源:NumberUtils.java

示例9: sMod

import java.math.BigInteger; //导入方法依赖的package包/类
public void sMod(cm.aptoide.pt.ethereum.ethereumj.vm.DataWord word) {

    if (word.isZero()) {
      this.and(ZERO);
      return;
    }

    BigInteger result = sValue().abs()
        .mod(word.sValue()
            .abs());
    result = (sValue().signum() == -1) ? result.negate() : result;

    this.data = ByteUtil.copyToArray(result.and(MAX_VALUE));
  }
 
开发者ID:Aptoide,项目名称:AppCoins-ethereumj,代码行数:15,代码来源:DataWord.java

示例10: sanitize

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Compute <code>value*signum</code> where value==null is treated as
 * value==0.
 * @param value Value to sanitize.
 * @param signum 0 to sanitize to 0, > 0 to sanitize to <code>value</code>, < 0 to sanitize to negative <code>value</code>.
 *
 * @return non-null {@link BigDecimal}.
 */
private static BigDecimal sanitize(BigInteger value, int signum) {
    if (signum == 0 || value == null) {
        return ZERO;
    }
    if (signum > 0) {
        return new BigDecimal(value);
    }
    return new BigDecimal(value.negate());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:DurationImpl.java

示例11: roundToBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Returns the {@code BigInteger} value that is equal to {@code x} rounded with the specified
 * rounding mode, if possible.
 *
 * @throws ArithmeticException if
 *     <ul>
 *     <li>{@code x} is infinite or NaN
 *     <li>{@code x} is not a mathematical integer and {@code mode} is
 *         {@link RoundingMode#UNNECESSARY}
 *     </ul>
 */
// #roundIntermediate, java.lang.Math.getExponent, com.google.common.math.DoubleUtils
@GwtIncompatible
public static BigInteger roundToBigInteger(double x, RoundingMode mode) {
  x = roundIntermediate(x, mode);
  if (MIN_LONG_AS_DOUBLE - x < 1.0 & x < MAX_LONG_AS_DOUBLE_PLUS_ONE) {
    return BigInteger.valueOf((long) x);
  }
  int exponent = getExponent(x);
  long significand = getSignificand(x);
  BigInteger result = BigInteger.valueOf(significand).shiftLeft(exponent - SIGNIFICAND_BITS);
  return (x < 0) ? result.negate() : result;
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:24,代码来源:DoubleMath.java

示例12: getLucas

import java.math.BigInteger; //导入方法依赖的package包/类
/**
     * Calculates the Lucas Sequence elements <code>U<sub>k-1</sub></code> and
     * <code>U<sub>k</sub></code> or <code>V<sub>k-1</sub></code> and
     * <code>V<sub>k</sub></code>.
     * @param mu The parameter <code>&mu;</code> of the elliptic curve.
     * @param k The index of the second element of the Lucas Sequence to be
     * returned.
     * @param doV If set to true, computes <code>V<sub>k-1</sub></code> and
     * <code>V<sub>k</sub></code>, otherwise <code>U<sub>k-1</sub></code> and
     * <code>U<sub>k</sub></code>.
     * @return An array with 2 elements, containing <code>U<sub>k-1</sub></code>
     * and <code>U<sub>k</sub></code> or <code>V<sub>k-1</sub></code>
     * and <code>V<sub>k</sub></code>.
     */
    public static BigInteger[] getLucas(byte mu, int k, boolean doV)
    {
        if (!((mu == 1) || (mu == -1)))
        {
            throw new IllegalArgumentException("mu must be 1 or -1");
        }

        BigInteger u0;
        BigInteger u1;
        BigInteger u2;

        if (doV)
        {
            u0 = ECConstants.TWO;
            u1 = BigInteger.valueOf(mu);
        }
        else
        {
            u0 = ECConstants.ZERO;
            u1 = ECConstants.ONE;
        }

        for (int i = 1; i < k; i++)
        {
            // u2 = mu*u1 - 2*u0;
            BigInteger s = null;
            if (mu == 1)
            {
                s = u1;
            }
            else
            {
                // mu == -1
                s = u1.negate();
            }
            
            u2 = s.subtract(u0.shiftLeft(1));
            u0 = u1;
            u1 = u2;
//            System.out.println(i + ": " + u2);
//            System.out.println();
        }

        BigInteger[] retVal = {u0, u1};
        return retVal;
    }
 
开发者ID:Appdome,项目名称:ipack,代码行数:61,代码来源:Tnaf.java

示例13: getLucas

import java.math.BigInteger; //导入方法依赖的package包/类
/**
     * Calculates the Lucas Sequence elements <code>U<sub>k-1</sub></code> and
     * <code>U<sub>k</sub></code> or <code>V<sub>k-1</sub></code> and
     * <code>V<sub>k</sub></code>.
     * @param mu The parameter <code>&mu;</code> of the elliptic curve.
     * @param k The index of the second element of the Lucas Sequence to be
     * returned.
     * @param doV If set to true, computes <code>V<sub>k-1</sub></code> and
     * <code>V<sub>k</sub></code>, otherwise <code>U<sub>k-1</sub></code> and
     * <code>U<sub>k</sub></code>.
     * @return An array with 2 elements, containing <code>U<sub>k-1</sub></code>
     * and <code>U<sub>k</sub></code> or <code>V<sub>k-1</sub></code>
     * and <code>V<sub>k</sub></code>.
     */
    public static BigInteger[] getLucas(byte mu, int k, boolean doV)
    {
        if (!((mu == 1) || (mu == -1)))
        {
            throw new IllegalArgumentException("mu must be 1 or -1");
        }

        BigInteger u0;
        BigInteger u1;
        BigInteger u2;

        if (doV)
        {
            u0 = ECConstants.TWO;
            u1 = BigInteger.valueOf(mu);
        }
        else
        {
            u0 = ECConstants.ZERO;
            u1 = ECConstants.ONE;
        }

        for (int i = 1; i < k; i++)
        {
            // u2 = mu*u1 - 2*u0;
            BigInteger s = null;
            if (mu == 1)
            {
                s = u1;
            }
            else
            {
                // mu == -1
                s = u1.negate();
            }

            u2 = s.subtract(u0.shiftLeft(1));
            u0 = u1;
            u1 = u2;
//            System.out.println(i + ": " + u2);
//            System.out.println();
        }

        BigInteger[] retVal = {u0, u1};
        return retVal;
    }
 
开发者ID:BiglySoftware,项目名称:BiglyBT,代码行数:61,代码来源:Tnaf.java

示例14: randomNonZeroBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Equivalent to calling randomPositiveBigInteger(numBits) and then flipping
 * the sign with 50% probability.
 */
static BigInteger randomNonZeroBigInteger(int numBits) {
  BigInteger result = randomPositiveBigInteger(numBits);
  return RANDOM_SOURCE.nextBoolean() ? result : result.negate();
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:9,代码来源:MathBenchmarking.java

示例15: neg

import java.math.BigInteger; //导入方法依赖的package包/类
@Override
public BigInteger neg(BigInteger arg) {
    return arg.negate();
}
 
开发者ID:meteoorkip,项目名称:JavaGraph,代码行数:5,代码来源:BigIntAlgebra.java


注:本文中的java.math.BigInteger.negate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。