本文整理了Java中java.math.BigInteger.negate()
方法的一些代码示例,展示了BigInteger.negate()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.negate()
方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:negate
[英]Returns a BigInteger whose value is the -this.
[中]返回一个BigInteger,其值为-this。
代码示例来源:origin: google/guava
@Override
public BigInteger apply(BigInteger x) {
return x.negate();
}
};
代码示例来源:origin: spring-projects/spring-framework
/**
* 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);
}
代码示例来源:origin: prestodb/presto
private static BigInteger bigDecimalToBigintFloorSaturatedCast(BigDecimal bigDecimal, int resultPrecision, int resultScale)
{
BigDecimal rescaledValue = bigDecimal.setScale(resultScale, FLOOR);
BigInteger unscaledValue = rescaledValue.unscaledValue();
BigInteger maxUnscaledValue = bigIntegerTenToNth(resultPrecision).subtract(ONE);
if (unscaledValue.compareTo(maxUnscaledValue) > 0) {
return maxUnscaledValue;
}
BigInteger minUnscaledValue = maxUnscaledValue.negate();
if (unscaledValue.compareTo(minUnscaledValue) < 0) {
return minUnscaledValue;
}
return unscaledValue;
}
代码示例来源:origin: prestodb/presto
@Override
public BigInteger visitArithmeticUnary(ArithmeticUnaryContext ctx)
{
BigInteger value = visit(ctx.expression());
switch (ctx.operator.getType()) {
case PLUS:
return value;
case MINUS:
return value.negate();
default:
throw new IllegalStateException("Unsupported unary operator " + ctx.operator.getText());
}
}
代码示例来源:origin: google/guava
/**
* 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();
}
代码示例来源:origin: google/guava
/**
* 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;
}
代码示例来源:origin: google/guava
/**
* Chooses a number in (-2^numBits, 2^numBits) at random, with density concentrated in numbers of
* lower magnitude.
*/
static BigInteger randomBigInteger(int numBits) {
while (true) {
if (RANDOM_SOURCE.nextBoolean()) {
return randomNonNegativeBigInteger(numBits);
}
BigInteger neg = randomNonNegativeBigInteger(numBits).negate();
if (neg.signum() != 0) {
return neg;
}
}
}
代码示例来源:origin: prestodb/presto
@Test
public void testAddReturnOverflow()
{
assertAddReturnOverflow(TWO, TWO);
assertAddReturnOverflow(MAX_DECIMAL_UNSCALED_VALUE, MAX_DECIMAL_UNSCALED_VALUE);
assertAddReturnOverflow(MAX_DECIMAL_UNSCALED_VALUE.negate(), MAX_DECIMAL_UNSCALED_VALUE);
assertAddReturnOverflow(MAX_DECIMAL_UNSCALED_VALUE, MAX_DECIMAL_UNSCALED_VALUE.negate());
assertAddReturnOverflow(MAX_DECIMAL_UNSCALED_VALUE.negate(), MAX_DECIMAL_UNSCALED_VALUE.negate());
}
代码示例来源:origin: google/guava
public void testCheckPositive_negativeBigInteger() {
try {
MathPreconditions.checkPositive("BigInteger", BigInteger.ZERO.negate());
fail();
} catch (IllegalArgumentException expected) {
}
}
代码示例来源:origin: google/guava
public void testCheckNonNegative_negativeBigInteger() {
try {
MathPreconditions.checkNonNegative("int", BigInteger.ONE.negate());
fail();
} catch (IllegalArgumentException expected) {
}
}
代码示例来源:origin: prestodb/presto
@Test
public void testCombineUnderflow()
{
addToState(state, TWO.pow(125).negate());
addToState(state, TWO.pow(126).negate());
LongDecimalWithOverflowState otherState = new LongDecimalWithOverflowStateFactory().createSingleState();
addToState(otherState, TWO.pow(125).negate());
addToState(otherState, TWO.pow(126).negate());
DecimalSumAggregation.combine(state, otherState);
assertEquals(state.getOverflow(), -1);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126).negate()));
}
代码示例来源:origin: prestodb/presto
@Test
public void testUnderflowAfterOverflow()
{
addToState(state, TWO.pow(126));
addToState(state, TWO.pow(126));
addToState(state, TWO.pow(125));
assertEquals(state.getOverflow(), 1);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(125)));
addToState(state, TWO.pow(126).negate());
addToState(state, TWO.pow(126).negate());
addToState(state, TWO.pow(126).negate());
assertEquals(state.getOverflow(), 0);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(125).negate()));
assertEquals(average(state, TYPE), new BigDecimal(TWO.pow(125).negate().divide(BigInteger.valueOf(6))));
}
代码示例来源:origin: prestodb/presto
@Test
public void testUnderflow()
{
addToState(state, TWO.pow(126).negate());
assertEquals(state.getLong(), 1);
assertEquals(state.getOverflow(), 0);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126).negate()));
addToState(state, TWO.pow(126).negate());
assertEquals(state.getLong(), 2);
assertEquals(state.getOverflow(), -1);
assertEquals(UnscaledDecimal128Arithmetic.compare(state.getLongDecimal(), unscaledDecimal(0)), 0);
assertEquals(average(state, TYPE), new BigDecimal(TWO.pow(126).negate()));
}
代码示例来源:origin: prestodb/presto
@Test
public void testUnderflow()
{
addToState(state, TWO.pow(126).negate());
assertEquals(state.getOverflow(), 0);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126).negate()));
addToState(state, TWO.pow(126).negate());
assertEquals(state.getOverflow(), -1);
assertEquals(UnscaledDecimal128Arithmetic.compare(state.getLongDecimal(), unscaledDecimal(0)), 0);
}
代码示例来源:origin: prestodb/presto
private SqlDecimal randomDecimal(DecimalType type)
{
int maxBits = (int) (Math.log(Math.pow(10, type.getPrecision())) / Math.log(2));
BigInteger bigInteger = new BigInteger(maxBits, random);
if (bigInteger.equals(ZERO)) {
bigInteger = ONE;
}
if (random.nextBoolean()) {
bigInteger = bigInteger.negate();
}
return new SqlDecimal(bigInteger, type.getPrecision(), type.getScale());
}
}
代码示例来源:origin: prestodb/presto
@Test
public void testUnderflowAfterOverflow()
{
addToState(state, TWO.pow(126));
addToState(state, TWO.pow(126));
addToState(state, TWO.pow(125));
assertEquals(state.getOverflow(), 1);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(125)));
addToState(state, TWO.pow(126).negate());
addToState(state, TWO.pow(126).negate());
addToState(state, TWO.pow(126).negate());
assertEquals(state.getOverflow(), 0);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(125).negate()));
}
代码示例来源:origin: prestodb/presto
@LiteralParameters({"p", "s"})
@SqlType("decimal(p, s)")
public static Slice negate(@SqlType("decimal(p, s)") Slice arg)
{
BigInteger argBigInteger = Decimals.decodeUnscaledValue(arg);
return encodeUnscaledValue(argBigInteger.negate());
}
}
代码示例来源:origin: prestodb/presto
@Test
public void testLongDecimals()
throws IOException
{
assertReadsLongValue(BigInteger.valueOf(0L));
assertReadsLongValue(BigInteger.valueOf(1L));
assertReadsLongValue(BigInteger.valueOf(-1L));
assertReadsLongValue(BigInteger.valueOf(-1).shiftLeft(126));
assertReadsLongValue(BigInteger.valueOf(1).shiftLeft(126));
assertReadsLongValue(BIG_INTEGER_127_BIT_SET);
assertReadsLongValue(BIG_INTEGER_127_BIT_SET.negate());
assertReadsLongValue(MAX_DECIMAL_UNSCALED_VALUE);
assertReadsLongValue(MIN_DECIMAL_UNSCALED_VALUE);
}
代码示例来源:origin: prestodb/presto
@Test
public void testUnscaledBigIntegerToDecimal()
{
assertConvertsUnscaledBigIntegerToDecimal(MAX_DECIMAL_UNSCALED_VALUE);
assertConvertsUnscaledBigIntegerToDecimal(MIN_DECIMAL_UNSCALED_VALUE);
assertConvertsUnscaledBigIntegerToDecimal(BigInteger.ZERO);
assertConvertsUnscaledBigIntegerToDecimal(BigInteger.ONE);
assertConvertsUnscaledBigIntegerToDecimal(BigInteger.ONE.negate());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void parseNumberAsNegativeHex() {
String aByte = "-0x80";
String aShort = "-0x8000";
String anInteger = "-0x80000000";
String aLong = "-0x8000000000000000";
String aReallyBigInt = "FEBD4E677898DFEBFFEE44";
assertNegativeByteEquals(aByte);
assertNegativeShortEquals(aShort);
assertNegativeIntegerEquals(anInteger);
assertNegativeLongEquals(aLong);
assertEquals("BigInteger did not parse",
new BigInteger(aReallyBigInt, 16).negate(), NumberUtils.parseNumber("-0x" + aReallyBigInt, BigInteger.class));
}
内容来源于网络,如有侵权,请联系作者删除!