Java Code Examples for java.math.BigInteger#min()

The following examples show how to use java.math.BigInteger#min() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigIntegerCompareTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * min(BigInteger val).
 * numbers are equal.
 */
public void testMinEqual() {
    byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    int aSign = 1;
    int bSign = 1;        
    byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.min(bNumber);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }    
    assertTrue("incorrect sign", result.signum() == 1);
}
 
Example 2
Source File: BigIntegerCompareTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * min(BigInteger val).
 * the first is less.
 */
public void testMinLess() {
    byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
    int aSign = 1;
    int bSign = 1;        
    byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.min(bNumber);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }    
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 3
Source File: BigIntegerCompareTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * min(BigInteger val).
 * the first is greater.
 */
public void testMinGreater() {
    byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
    byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    int aSign = 1;
    int bSign = 1;        
    byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = new BigInteger(bSign, bBytes);
    BigInteger result = aNumber.min(bNumber);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }    
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 4
Source File: MarkerSetImpl.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private BigInteger getIndex(BigInteger bigStarty, BigDecimal bigHeight, BigInteger numIndexes,
		BigInteger numIndexesMinus1) {
	BigDecimal total = new BigDecimal(bigStarty.multiply(numIndexes));
	BigDecimal div = total.divideToIntegralValue(bigHeight);
	BigInteger index = div.toBigInteger();

	index = index.min(numIndexesMinus1);
	return index;
}
 
Example 5
Source File: SSOZJ.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
public static BigInteger Bsqrt(BigInteger x) {
	BigInteger div = BigInteger.ZERO.setBit(x.bitLength() >> 1);
	BigInteger div2 = div;
	// Loop until we hit the same value twice in a row, or wind up alternating.
	for(;;) {
		BigInteger y = div.add(x.divide(div)).shiftRight(1);
		if (y.equals(div) || y.equals(div2))
			return div.min(div2);
		div2 = div;
		div = y;
	}
}
 
Example 6
Source File: AlgebraicBigIntegerMathBase.java    From spork with Apache License 2.0 5 votes vote down vote up
private static BigInteger doWork(BigInteger arg1, BigInteger arg2, KNOWN_OP op) {
    if (arg1 == null) {
        return arg2;
    } else if (arg2 == null) {
        return arg1;
    } else {
        BigInteger retVal = null;
        switch (op) {
        case SUM:
            retVal = arg1.add(arg2);
            break;
        case MAX:
            if (BigIntegerWrapper.class.isInstance(arg1) && (((BigIntegerWrapper)arg1).isNegativeInfinity())) {
                retVal = arg2;
            } else if(BigIntegerWrapper.class.isInstance(arg2) && (((BigIntegerWrapper)arg2).isNegativeInfinity())) {
                retVal = arg1;
            } else {
                retVal = arg1.max(arg2);
            }
            break;
        case MIN:
            if (BigIntegerWrapper.class.isInstance(arg1) && (((BigIntegerWrapper)arg1).isPositiveInfinity())) {
                retVal = arg2;
            } else if (BigIntegerWrapper.class.isInstance(arg2) && (((BigIntegerWrapper)arg2).isPositiveInfinity())) {
                retVal = arg1;
            } else{
                retVal = arg1.min(arg2);
            }
            break;
        default:
            retVal = null;
            break;
        }
        return retVal;
    }
}
 
Example 7
Source File: BigIntegerCompareTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * max(BigInteger val).
 * min of positive and ZERO.
 */
public void testMinPosZero() {
    byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91};
    int aSign = 1;
    byte rBytes[] = {0};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger bNumber = BigInteger.ZERO;
    BigInteger result = aNumber.min(bNumber);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }    
    assertTrue("incorrect sign", result.signum() == 0);
}
 
Example 8
Source File: BCMathFunctions.java    From jphp with Apache License 2.0 5 votes vote down vote up
private static BigDecimal bcpowImpl(BigDecimal base, BigInteger exp, int scale) {
    if (exp.compareTo(BigInteger.ZERO) == 0)
        return BigDecimal.ONE;

    boolean isNeg;
    if (exp.compareTo(BigInteger.ZERO) < 0) {
        isNeg = true;
        exp = exp.negate();
    }
    else
        isNeg = false;

    BigDecimal result = BigDecimal.ZERO;

    while (exp.compareTo(BigInteger.ZERO) > 0) {
        BigInteger expSub = exp.min(INTEGER_MAX);
        exp = exp.subtract(expSub);

        result = result.add(base.pow(expSub.intValue()));
    }

    if (isNeg)
        result = BigDecimal.ONE.divide(result, scale + 2, RoundingMode.DOWN);

    result = result.setScale(scale, RoundingMode.DOWN);

    if (result.compareTo(BigDecimal.ZERO) == 0)
        return BigDecimal.ZERO;

    result = result.stripTrailingZeros();
    return result;
}
 
Example 9
Source File: TypeCalculation.java    From rainbow with Apache License 2.0 5 votes vote down vote up
@Override
public BigInteger visitBinaryFunction(BinaryFunctionContext ctx)
{
    BigInteger left = visit(ctx.left);
    BigInteger right = visit(ctx.right);
    switch (ctx.binaryFunctionName().name.getType()) {
        case MIN:
            return left.min(right);
        case MAX:
            return left.max(right);
        default:
            throw new IllegalArgumentException("Unsupported binary function " + ctx.binaryFunctionName().getText());
    }
}
 
Example 10
Source File: TypeCalculation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public BigInteger visitBinaryFunction(BinaryFunctionContext ctx)
{
    BigInteger left = visit(ctx.left);
    BigInteger right = visit(ctx.right);
    switch (ctx.binaryFunctionName().name.getType()) {
        case MIN:
            return left.min(right);
        case MAX:
            return left.max(right);
        default:
            throw new IllegalArgumentException("Unsupported binary function " + ctx.binaryFunctionName().getText());
    }
}
 
Example 11
Source File: SimpleTypeBuilder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 12
Source File: SimpleTypeBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 13
Source File: SimpleTypeBuilder.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 14
Source File: SimpleTypeBuilder.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 15
Source File: SimpleTypeBuilder.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 16
Source File: BigIntegerTarget.java    From AVM with MIT License 4 votes vote down vote up
@Callable
public static byte[] min(byte[] val) {
    BigInteger testValue = new BigInteger(val);
    BigInteger result = testValue.min(testValue);
    return result.toByteArray();
}
 
Example 17
Source File: SimpleTypeBuilder.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 18
Source File: BigIntegerMutatorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
BigInteger apply(BigInteger left, BigInteger right) {
  return left.min(right);
}
 
Example 19
Source File: SimpleTypeBuilder.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private BigInteger min(BigInteger a, BigInteger b) {
    if(a==null) return b;
    if(b==null) return a;
    return a.min(b);
}
 
Example 20
Source File: PowUtil.java    From snowblossom with Apache License 2.0 2 votes vote down vote up
public static BigInteger calcNextTarget(BlockSummary prev_summary, NetworkParams params, long clock_time)
{
  if (prev_summary.getHeader().getTimestamp() == 0) return params.getMaxTarget();


  long weight = params.getAvgWeight();
  long decay = 1000L - weight;

  long delta_t = clock_time - prev_summary.getHeader().getTimestamp();

  long prev_block_time = prev_summary.getBlocktimeAverageMs();

  long averaged_delta_t = (prev_block_time * decay + delta_t * weight) / 1000L ;

  long target_delta_t = params.getBlockTimeTarget();


  // Scale > 0 means that the time is too long -> decrease difficulty
  // Scale < 0 means the time is too short -> increase difficulty
  long scale = averaged_delta_t * 1000 / target_delta_t - 1000;

  // Take whatever we thing the correction factor is and
  // half that
  scale = scale / 2;


  // Max change is 50% per block
  scale = Math.min(scale, 500);
  scale = Math.max(scale, -500);

  // Larger target -> less work

  BigInteger prev_target_average = new BigInteger(prev_summary.getTargetAverage());
  BigInteger scale_bi = BigInteger.valueOf(scale);
  BigInteger thousand = BigInteger.valueOf(1000L);


  BigInteger new_target = prev_target_average.add( 
    prev_target_average.multiply(scale_bi).divide(thousand) );
  //long new_target = prev_summary.getTargetAverage() + prev_summary.getTargetAverage() * scale / 1000;

  logger.log(Level.FINE, String.format("Delta_t: %d (%d) scale: %d",averaged_delta_t, target_delta_t, scale));

  ByteBuffer bb = ByteBuffer.allocate(8);

  new_target = new_target.min(params.getMaxTarget());

  ByteString new_target_display = BlockchainUtil.targetBigIntegerToBytes(new_target).substring(0,16);

  double diff = getDiffForTarget(new_target);
  double avg_diff = getDiffForTarget( prev_target_average );
  DecimalFormat df = new DecimalFormat("0.000");
  logger.log(Level.FINE,String.format("New target: %s, %s, (avg %s)", 
    HashUtils.getHexString(new_target_display), 
    df.format(diff),
    df.format(avg_diff)));

  return new_target;

}