if (new BigDecimal(someprice).compareTo(BigDecimal.ZERO) == 0) // see below
private static int compareDoubleToLong(final double a, final long b) { // Use BigDecimal when comparing integers vs floating points, a convenient way to handle all cases (like // fractional values, values out of range of max long/max int) without worrying about them ourselves. return BigDecimal.valueOf(a).compareTo(BigDecimal.valueOf(b)); } }
@Override public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions, TypeInfo typeInfo) { List<MutablePair<String, String>> intervals = new ArrayList<>(); DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo)typeInfo; int scale = decimalTypeInfo.getScale(); BigDecimal decimalLower = new BigDecimal(lowerBound); BigDecimal decimalUpper = new BigDecimal(upperBound); BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions), MathContext.DECIMAL64); BigDecimal splitDecimalLower, splitDecimalUpper; for (int i=0;i<numPartitions;i++) { splitDecimalLower = decimalLower.add(decimalInterval.multiply(new BigDecimal(i))).setScale(scale, RoundingMode.HALF_EVEN); splitDecimalUpper = decimalLower.add(decimalInterval.multiply(new BigDecimal(i+1))).setScale(scale, RoundingMode.HALF_EVEN); if (splitDecimalLower.compareTo(splitDecimalUpper) < 0) { intervals.add(new MutablePair<String, String>(splitDecimalLower.toPlainString(), splitDecimalUpper.toPlainString())); } } return intervals; } }
BigDecimal step = new BigDecimal("0.1"); for (BigDecimal value = BigDecimal.ZERO; value.compareTo(BigDecimal.ONE) < 0; value = value.add(step)) { System.out.println(value); }
final BigDecimal one = BigDecimal.valueOf(10, 1); // That's what you get for "1.0". if (to instanceof BigDecimal) { BigDecimal to1 = (BigDecimal) to; if (self.compareTo(to1) <= 0) { for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) { closure.call(i); ") to upto() cannot be less than the value (" + self + ") it's called on."); } else if (to instanceof BigInteger) { BigDecimal to1 = new BigDecimal((BigInteger) to); if (self.compareTo(to1) <= 0) { for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) { closure.call(i); ") to upto() cannot be less than the value (" + self + ") it's called on."); } else { BigDecimal to1 = new BigDecimal(to.toString()); if (self.compareTo(to1) <= 0) { for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) { closure.call(i);
final BigDecimal one = BigDecimal.valueOf(10, 1); // Quick way to get "1.0". if (to instanceof BigDecimal) { BigDecimal to1 = (BigDecimal) to; if (self.compareTo(to1) >= 0) { for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i); throw new GroovyRuntimeException("The argument (" + to + ") to downto() cannot be greater than the value (" + self + ") it's called on."); } else if (to instanceof BigInteger) { BigDecimal to1 = new BigDecimal((BigInteger) to); if (self.compareTo(to1) >= 0) { for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i); throw new GroovyRuntimeException("The argument (" + to + ") to downto() cannot be greater than the value (" + self + ") it's called on."); } else { BigDecimal to1 = new BigDecimal(to.toString()); if (self.compareTo(to1) >= 0) { for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i);
BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return (leftBigDecimal.compareTo(rightBigDecimal) == 0); BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class); BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class); return (leftBigInteger.compareTo(rightBigInteger) == 0);
/** * Calculate difference between values in percent */ public BigDecimal percentDiff(Var var) { BigDecimal d1 = new Var(Var.Type.DECIMAL).cast(this).decimalValue(); BigDecimal d2 = new Var(Var.Type.DECIMAL).cast(var).decimalValue(); if (d1 != null && d2 != null) { if (d1.compareTo(BigDecimal.ZERO) != 0) { return d1.subtract(d2).abs().multiply(new BigDecimal(100)).divide(d1, 2, RoundingMode.HALF_UP); } } return null; }
private static Number unwrapNumber(final Number n) { Number unwrapped; if (!isPrimitiveNumber(n)) { BigDecimal bigDecimal = new BigDecimal(n.toString()); if (bigDecimal.scale() <= 0) { if (bigDecimal.compareTo(new BigDecimal(Integer.MAX_VALUE)) <= 0) { unwrapped = bigDecimal.intValue(); } else if (bigDecimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0){ unwrapped = bigDecimal.longValue(); } else { unwrapped = bigDecimal; } } else { final double doubleValue = bigDecimal.doubleValue(); if (BigDecimal.valueOf(doubleValue).compareTo(bigDecimal) != 0) { unwrapped = bigDecimal; } else { unwrapped = doubleValue; } } } else { unwrapped = n; } return unwrapped; }
public BigDecimal eval(List<? extends Number> parameters) { assertNotNull(parameters.get(0)); /* * From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003) */ BigDecimal x = (BigDecimal) parameters.get(0); if (x.compareTo(BigDecimal.ZERO) == 0) { return new BigDecimal(0); } if (x.signum() < 0) { throw new ExpressionException("Argument to SQRT() function must not be negative"); } BigInteger n = x.movePointRight(mc.getPrecision() << 1).toBigInteger(); int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; BigInteger test; do { ixPrev = ix; ix = ix.add(n.divide(ix)).shiftRight(1); // Give other threads a chance to work; Thread.yield(); test = ix.subtract(ixPrev).abs(); } while (test.compareTo(BigInteger.ZERO) != 0 && test.compareTo(BigInteger.ONE) != 0); return new BigDecimal(ix, mc.getPrecision()); } });
/** * Canonicalize the current latitude and longitude values such that: * * <pre> * -90 <= latitude <= +90 - 180 < longitude <= +180 * </pre> */ private void canonicalize(DecimalType aLat, DecimalType aLon) { latitude = flat.add(aLat.toBigDecimal()).remainder(circle); longitude = aLon.toBigDecimal(); if (latitude.compareTo(BigDecimal.ZERO) == -1) { latitude.add(circle); } latitude = latitude.subtract(flat); if (latitude.compareTo(right) == 1) { latitude = flat.subtract(latitude); longitude = longitude.add(flat); } else if (latitude.compareTo(right.negate()) == -1) { latitude = flat.negate().subtract(latitude); longitude = longitude.add(flat); } longitude = flat.add(longitude).remainder(circle); if (longitude.compareTo(BigDecimal.ZERO) <= 0) { longitude = longitude.add(circle); } longitude = longitude.subtract(flat); }
/** * Convert original value insertion of type 'BIGINT' into the correct BIGINT UNSIGNED representation * Note: Unsigned BIGINT (16-bit) is represented in 'BigDecimal' data type. Reference: https://kafka.apache.org/0102/javadoc/org/apache/kafka/connect/data/Schema.Type.html * * @param originalNumber {@link BigDecimal} the original insertion value * @return {@link BigDecimal} the correct representation of the original insertion value */ public static BigDecimal convertUnsignedBigint(BigDecimal originalNumber) { if (originalNumber.compareTo(BigDecimal.ZERO) == -1) { return originalNumber.add(BIGINT_CORRECTION); } else { return originalNumber; } } }
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; }
private void validateValue(BigDecimal value) { if (BigDecimal.ZERO.compareTo(value) > 0 || new BigDecimal(100).compareTo(value) < 0) { throw new IllegalArgumentException("Value must be between 0 and 100"); } }
public static LimitOrder adaptOrder(CexIOFullOrder cexIOOrder) { OrderType orderType = cexIOOrder.type.equals("sell") ? OrderType.ASK : OrderType.BID; BigDecimal originalAmount = new BigDecimal(cexIOOrder.amount); CurrencyPair currencyPair = new CurrencyPair(cexIOOrder.symbol1, cexIOOrder.symbol2); Date timestamp = new Date(cexIOOrder.time); BigDecimal limitPrice = new BigDecimal(cexIOOrder.price); Order.OrderStatus status = adaptOrderStatus(cexIOOrder); BigDecimal cumulativeAmount = null; BigDecimal remains = new BigDecimal(cexIOOrder.remains); cumulativeAmount = originalAmount.subtract(remains); ? new BigDecimal(cexIOOrder.totalAmountTaker) : BigDecimal.ZERO; BigDecimal tradedAmount = totalAmountMaker.add(totalAmountTaker); if (cumulativeAmount != null && tradedAmount.compareTo(BigDecimal.ZERO) > 0) { averagePrice = tradedAmount.divide(cumulativeAmount, 2, RoundingMode.HALF_UP); BigDecimal feeTaker = cexIOOrder.feeTaker != null ? new BigDecimal(cexIOOrder.feeTaker) : BigDecimal.ZERO; BigDecimal fee = feeMaker.add(feeTaker); return new LimitOrder( orderType, averagePrice, cumulativeAmount, fee.compareTo(BigDecimal.ZERO) > 0 ? fee : null, status);
if (x.compareTo(BigDecimal.ZERO) < 0) { return cos(x.negate()); } else if (x.compareTo(BigDecimal.ZERO) == 0) { return BigDecimal.ONE; } else { ifac = ifac.multiply(BigInteger.valueOf(2 * i)); xpowi = xpowi.multiply(xhighpr).multiply(xhighpr); BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay); resul = resul.add(corr);
final BigDecimal one = BigDecimal.valueOf(10, 1); BigDecimal self1 = new BigDecimal(self); BigDecimal to1 = (BigDecimal) to; if (self1.compareTo(to1) <= 0) { for (BigDecimal i = self1; i.compareTo(to1) <= 0; i = i.add(one)) { closure.call(i); final BigInteger one = BigInteger.valueOf(1); BigInteger to1 = (BigInteger) to; if (self.compareTo(to1) <= 0) { for (BigInteger i = self; i.compareTo(to1) <= 0; i = i.add(one)) { closure.call(i); final BigInteger one = BigInteger.valueOf(1); BigInteger to1 = new BigInteger(to.toString()); if (self.compareTo(to1) <= 0) { for (BigInteger i = self; i.compareTo(to1) <= 0; i = i.add(one)) { closure.call(i);
final BigDecimal one = BigDecimal.valueOf(10, 1); // That's what you get for "1.0". final BigDecimal to1 = (BigDecimal) to; final BigDecimal selfD = new BigDecimal(self); if (selfD.compareTo(to1) >= 0) { for (BigDecimal i = selfD; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i.toBigInteger()); final BigInteger one = BigInteger.valueOf(1); final BigInteger to1 = (BigInteger) to; if (self.compareTo(to1) >= 0) { for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i); final BigInteger one = BigInteger.valueOf(1); final BigInteger to1 = new BigInteger(to.toString()); if (self.compareTo(to1) >= 0) { for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i);