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


Java BigInteger.abs方法代码示例

本文整理汇总了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;
}
 
开发者ID:austinv11,项目名称:ETF-Java,代码行数:21,代码来源:ETFWriter.java

示例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;
}
 
开发者ID:austinv11,项目名称:ETF-Java,代码行数:22,代码来源:ETFWriter.java

示例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;
}
 
开发者ID:cheahjs,项目名称:JLoopix,代码行数:14,代码来源:Packer.java

示例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);
}
 
开发者ID:shikhar29111996,项目名称:Java-programs,代码行数:13,代码来源:absolute.java

示例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);
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:44,代码来源:DoubleUtils.java

示例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);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:44,代码来源:DoubleUtils.java

示例7: abs

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

示例8: intOp

import java.math.BigInteger; //导入方法依赖的package包/类
@Override
public BigInteger intOp() {
    BigInteger bi = args.get(0).valInt();
    return bi.abs();
}
 
开发者ID:actiontech,项目名称:dble,代码行数:6,代码来源:ItemFuncAbs.java

示例9: abs

import java.math.BigInteger; //导入方法依赖的package包/类
public static BigInteger abs(BigInteger a) { return a.abs(); } 
开发者ID:CenPC434,项目名称:java-tools,代码行数:2,代码来源:Lang.java


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