public BigDecimal eval(Number v1, Number v2) { assertNotNull(v1, v2); return ((BigDecimal) v1).remainder((BigDecimal) v2, mc); } });
BigDecimal bd = new BigDecimal( "23452.4523434" ); BigDecimal fractionalPart = bd.remainder( BigDecimal.ONE ); // Result: 0.4523434
private static BigDecimal removeTrailingZeroFractionOrScale( BigDecimal a, int maxScale ) { if ( a.remainder( BigDecimal.ONE ).compareTo( BigDecimal.ZERO ) == 0 ) { return a.setScale( 0 ); } return a.setScale( maxScale, RoundingMode.HALF_EVEN ); }
@Override public boolean contains(BigDecimal value) { if (value == null || value.compareTo(from) < 0 || value.compareTo(to) >= 0) { return false; } return value.subtract(from).remainder(incrementUnit).compareTo(BigDecimal.ZERO) == 0; }
public void set(BigDecimal totalSecondsBd) { long totalSeconds = totalSecondsBd.longValue(); BigDecimal fractionalSecs = totalSecondsBd.remainder(BigDecimal.ONE); int nanos = fractionalSecs.multiply(IntervalDayTimeUtils.NANOS_PER_SEC_BD).intValue(); set(totalSeconds, nanos); }
public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) { try { BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL); int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue(); if (nanos < 0) { nanos += 1000000000; } long seconds = nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue(); Timestamp t = new Timestamp(seconds * 1000); t.setNanos(nanos); return t; } catch (NumberFormatException nfe) { return null; } catch (IllegalArgumentException iae) { return null; } }
public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) { try { BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL); int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue(); if (nanos < 0) { nanos += 1000000000; } long seconds = nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue(); return Timestamp.ofEpochSecond(seconds, nanos); } catch (IllegalArgumentException | DateTimeException nfe) { return null; } }
/** * 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); }
public static Memory bcmod(Memory left, Memory modus){ BigDecimal base = toBigDecimal(left).setScale(0, RoundingMode.DOWN); BigDecimal mod = toBigDecimal(modus).setScale(0, RoundingMode.DOWN); if (mod.compareTo(BigDecimal.ZERO) == 0) { return Memory.NULL; } return new StringMemory(base.remainder(mod, MathContext.UNLIMITED).toString()); }
@Override public ValueDecimal modulus(Value v) { ValueDecimal dec = (ValueDecimal) v; if (dec.value.signum() == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } BigDecimal bd = value.remainder(dec.value); return ValueDecimal.get(bd); }
@HiveDecimalVersionV1 public HiveDecimalV1 remainder(HiveDecimalV1 dec) { return create(bd.remainder(dec.bd)); }
public BigDecimal eval(Number vv1, Number vv2) { BigDecimal v1 = (BigDecimal) vv1; BigDecimal v2 = (BigDecimal) vv2; assertNotNull(v1, v2); /*- * Thanks to Gene Marin: * http://stackoverflow.com/questions/3579779/how-to-do-a-fractional-power-on-bigdecimal-in-java */ int signOf2 = ((BigDecimal) v2).signum(); double dn1 = v1.doubleValue(); v2 = v2.multiply(new BigDecimal(signOf2)); // n2 is now positive BigDecimal remainderOf2 = v2.remainder(BigDecimal.ONE); BigDecimal n2IntPart = v2.subtract(remainderOf2); BigDecimal intPow = v1.pow(n2IntPart.intValueExact(), mc); BigDecimal doublePow = new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue())); BigDecimal result = intPow.multiply(doublePow, mc); if (signOf2 == -1) { result = BigDecimal.ONE.divide(result, mc.getPrecision(), RoundingMode.HALF_UP); } return result; } });
public static String bcpowmod(Environment env, Memory _base, Memory _exp, Memory _modulus, int scale) { BigDecimal base = toBigDecimal(_base); BigDecimal exp = toBigDecimal(_exp); BigDecimal modulus = toBigDecimal(_modulus); if (base.scale() != 0) { BigInteger expI = exp.toBigInteger(); BigDecimal pow = bcpowImpl(base, expI, scale); return pow.remainder(modulus, MathContext.UNLIMITED).toString(); } else { BigInteger baseI = base.toBigInteger(); BigInteger expI = exp.toBigInteger(); BigInteger modulusI = modulus.toBigInteger(); BigInteger result = baseI.modPow(expI, modulusI); return result.toString(); } }
/** * Returns the remainder amount if the passed in totalAmount was divided by the * quantity taking into account the normal unit of the currency (e.g. .01 for US). * * @param totalAmount * @param quantity * @return */ public static int calculateRemainder(Money totalAmount, int quantity) { if (totalAmount == null || totalAmount.isZero() || quantity == 0) { return 0; } // Use this to convert to a whole number (e.g. 1.05 becomes 105 in US currency). BigDecimal multiplier = new BigDecimal(10).pow(totalAmount.getAmount().scale()); BigDecimal amount = totalAmount.getAmount().multiply(multiplier); BigDecimal remainder = amount.remainder(new BigDecimal(quantity), ROUND_FLOOR_MATH_CONTEXT); return remainder.toBigInteger().intValue(); }
@Override public ValueDecimal modulus(Value v) { ValueDecimal dec = (ValueDecimal) v; if (dec.value.signum() == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } BigDecimal bd = value.remainder(dec.value); return ValueDecimal.get(bd); }
private static boolean valueIsLong(final JsonNode node) { if (!node.canConvertToLong()) return false; if (NodeType.getNodeType(node) == NodeType.INTEGER) return true; return node.decimalValue().remainder(BigDecimal.ONE) .compareTo(BigDecimal.ZERO) == 0; }
BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
/** * Returns the remainder (modulus) of A / B. * * @param metaA * @param dataA * The dividend * @param metaB * @param dataB * The divisor * @return The remainder * @throws KettleValueException */ public static Object remainder( ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB, Object dataB ) throws KettleValueException { if ( dataA == null || dataB == null ) { return null; } switch ( metaA.getType() ) { case ValueMetaInterface.TYPE_NUMBER: return new Double( metaA.getNumber( dataA ).doubleValue() % metaB.getNumber( dataB ).doubleValue() ); case ValueMetaInterface.TYPE_INTEGER: return new Long( metaA.getInteger( dataA ) % metaB.getInteger( dataB ) ); case ValueMetaInterface.TYPE_BIGNUMBER: BigDecimal aValue = metaA.getBigNumber( dataA ); BigDecimal bValue = metaA.getBigNumber( dataB ); BigDecimal result = aValue.remainder( bValue, new MathContext( getMaxPrecision( aValue, bValue ), RoundingMode.HALF_EVEN ) ); return removeTrailingZeroFractionOrScale( result, result.scale() ); default: throw new KettleValueException( "The 'remainder' function only works on numeric data" ); } }
@Override protected final void validateDecimal(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final JsonNode node = data.getInstance().getNode(); final BigDecimal instanceValue = node.decimalValue(); final BigDecimal decimalValue = number.decimalValue(); final BigDecimal remainder = instanceValue.remainder(decimalValue); /* * We cannot use equality! As far as BigDecimal goes, * "0" and "0.0" are NOT equal. But .compareTo() returns the correct * result. */ if (remainder.compareTo(BigDecimal.ZERO) == 0) return; report.error(newMsg(data, bundle, "err.common.divisor.nonZeroRemainder") .putArgument("value", node).putArgument("divisor", number)); } }
static Object executeRemainder( final IExpressionContext context, final RemainderExpression expression, final StandardExpressionExecutionContext expContext) { if (logger.isTraceEnabled()) { logger.trace("[THYMELEAF][{}] Evaluating remainder expression: \"{}\"", TemplateEngine.threadIndex(), expression.getStringRepresentation()); } Object leftValue = expression.getLeft().execute(context, expContext); Object rightValue = expression.getRight().execute(context, expContext); if (leftValue == null) { leftValue = "null"; } if (rightValue == null) { rightValue = "null"; } final BigDecimal leftNumberValue = EvaluationUtils.evaluateAsNumber(leftValue); final BigDecimal rightNumberValue = EvaluationUtils.evaluateAsNumber(rightValue); if (leftNumberValue != null && rightNumberValue != null) { // Addition will act as a mathematical 'plus' return leftNumberValue.remainder(rightNumberValue); } throw new TemplateProcessingException( "Cannot execute division: operands are \"" + LiteralValue.unwrap(leftValue) + "\" and \"" + LiteralValue.unwrap(rightValue) + "\""); }