本文整理汇总了Java中java.math.BigInteger.abs方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.abs方法的具体用法?Java BigInteger.abs怎么用?Java BigInteger.abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.abs方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeSmallBig
import java.math.BigInteger; //导入方法依赖的package包/类
public ETFWriter writeSmallBig(BigInteger num) {
if (num.equals(BigInteger.ZERO)) {
writeToBuffer(SMALL_BIG_EXT, (byte) 0, (byte) 0);
} else {
byte signum = num.signum() == -1 ? (byte) 1 : (byte) 0;
num = num.abs();
int n = (int) Math.ceil(num.bitLength()/8)+1; //Equivalent to Math.ceil(log256(num)) + 1
byte[] bytes = new byte[n];
writeToBuffer(SMALL_BIG_EXT, (byte) (n & 0xFF), signum);
n -= 1;
while (n >= 0) {
BigInteger[] res = num.divideAndRemainder(BigInteger.valueOf(256).pow(n));
bytes[n] = res[0].byteValue(); //Quotient
num = res[1]; //Remainder
n--;
}
writeToBuffer(bytes);
}
return this;
}
示例2: writeLargeBig
import java.math.BigInteger; //导入方法依赖的package包/类
public ETFWriter writeLargeBig(BigInteger num) {
if (num.equals(BigInteger.ZERO)) {
writeToBuffer(LARGE_BIG_EXT, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
} else {
byte signum = num.signum() == -1 ? (byte) 1 : (byte) 0;
num = num.abs();
int n = (int) Math.ceil(num.bitLength()/8)+1; //Equivalent to Math.ceil(log256(num)) + 1
byte[] bytes = new byte[n];
writeToBuffer(LARGE_BIG_EXT, (byte) ((n >>> 24) & 0xFF), (byte) ((n >>> 16) & 0xFF),
(byte) ((n >>> 8) & 0xFF), (byte) (n & 0xFF), signum);
n -= 1;
while (n >= 0) {
BigInteger[] res = num.divideAndRemainder(BigInteger.valueOf(256).pow(n));
bytes[n] = res[0].byteValue(); //Quotient
num = res[1]; //Remainder
n--;
}
writeToBuffer(bytes);
}
return this;
}
示例3: packBigNumber
import java.math.BigInteger; //导入方法依赖的package包/类
public MessagePacker packBigNumber(BigInteger bn) throws IOException {
byte sign = 0;
if (bn.compareTo(new BigInteger("0")) < 0) {
sign = '-';
bn = bn.abs();
} else {
sign = '+';
}
byte[] data = Arrays.prepend(bn.toByteArray(), sign);
this.packExtensionTypeHeader((byte) 0, data.length);
this.addPayload(data);
return this;
}
示例4: main
import java.math.BigInteger; //导入方法依赖的package包/类
public static void main()throws IOException
{
BufferedReader dr=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
String n=dr.readLine();
BigInteger a=new BigInteger(n);
BigInteger b=a.abs();
System.out.println("absolute value is "+b);
}
示例5: bigToDouble
import java.math.BigInteger; //导入方法依赖的package包/类
static double bigToDouble(BigInteger x) {
// This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending.
BigInteger absX = x.abs();
int exponent = absX.bitLength() - 1;
// exponent == floor(log2(abs(x)))
if (exponent < Long.SIZE - 1) {
return x.longValue();
} else if (exponent > MAX_EXPONENT) {
return x.signum() * POSITIVE_INFINITY;
}
/*
* We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make
* rounding easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us
* round up or down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and
* signifFloor the top SIGNIFICAND_BITS + 1.
*
* It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent).
*/
int shift = exponent - SIGNIFICAND_BITS - 1;
long twiceSignifFloor = absX.shiftRight(shift).longValue();
long signifFloor = twiceSignifFloor >> 1;
signifFloor &= SIGNIFICAND_MASK; // remove the implied bit
/*
* We round up if either the fractional part of signif is strictly greater than 0.5 (which is
* true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is
* >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set).
*/
boolean increment = (twiceSignifFloor & 1) != 0
&& ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift);
long signifRounded = increment ? signifFloor + 1 : signifFloor;
long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS;
bits += signifRounded;
/*
* If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to
* the exponent. This is exactly the behavior we get from just adding signifRounded to bits
* directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to
* Double.POSITIVE_INFINITY.
*/
bits |= x.signum() & SIGN_MASK;
return longBitsToDouble(bits);
}
示例6: bigToDouble
import java.math.BigInteger; //导入方法依赖的package包/类
static double bigToDouble(BigInteger x) {
// This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending.
BigInteger absX = x.abs();
int exponent = absX.bitLength() - 1;
// exponent == floor(log2(abs(x)))
if (exponent < Long.SIZE - 1) {
return x.longValue();
} else if (exponent > MAX_EXPONENT) {
return x.signum() * POSITIVE_INFINITY;
}
/*
* We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding
* easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or
* down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the
* top SIGNIFICAND_BITS + 1.
*
* It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent).
*/
int shift = exponent - SIGNIFICAND_BITS - 1;
long twiceSignifFloor = absX.shiftRight(shift).longValue();
long signifFloor = twiceSignifFloor >> 1;
signifFloor &= SIGNIFICAND_MASK; // remove the implied bit
/*
* We round up if either the fractional part of signif is strictly greater than 0.5 (which is
* true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is
* >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set).
*/
boolean increment =
(twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift);
long signifRounded = increment ? signifFloor + 1 : signifFloor;
long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS;
bits += signifRounded;
/*
* If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to
* the exponent. This is exactly the behavior we get from just adding signifRounded to bits
* directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to
* Double.POSITIVE_INFINITY.
*/
bits |= x.signum() & SIGN_MASK;
return longBitsToDouble(bits);
}
示例7: abs
import java.math.BigInteger; //导入方法依赖的package包/类
@Override
public BigInteger abs(BigInteger arg) {
return arg.abs();
}
示例8: intOp
import java.math.BigInteger; //导入方法依赖的package包/类
@Override
public BigInteger intOp() {
BigInteger bi = args.get(0).valInt();
return bi.abs();
}
示例9: abs
import java.math.BigInteger; //导入方法依赖的package包/类
public static BigInteger abs(BigInteger a) { return a.abs(); }