private static long saturatedFloorCastToLong(double value, long minValue, double minValueAsDouble, long maxValue, double maxValuePlusOneAsDouble) { if (value <= minValueAsDouble) { return minValue; } if (value + 1 >= maxValuePlusOneAsDouble) { return maxValue; } return DoubleMath.roundToLong(value, FLOOR); }
private static long saturatedFloorCastToLong(long valueBits, long minValue, float minValueAsDouble, long maxValue, float maxValuePlusOneAsDouble) { float value = intBitsToFloat((int) valueBits); if (value <= minValueAsDouble) { return minValue; } if (value + 1 >= maxValuePlusOneAsDouble) { return maxValue; } return DoubleMath.roundToLong(value, FLOOR); }
long totalIntervals = Long.MAX_VALUE; try { totalIntervals = DoubleMath.roundToLong( (double) highWatermarkValue / partitionInterval - (double) lowWatermarkValue / partitionInterval, RoundingMode.CEILING); outputInterval = DoubleMath.roundToLong( (double) highWatermarkValue / maxIntervals - (double) lowWatermarkValue / maxIntervals, RoundingMode.CEILING);
public boolean matchesDaily(float fraction) { Preconditions.checkArgument(fraction >= 0 && fraction <= 1, "fraction must be in [0..1]"); long fracInMs = DoubleMath.roundToLong((double) fraction * WorldTime.DAY_LENGTH, RoundingMode.HALF_UP); long diff = getDayTimeInMs() - fracInMs; return 2 * diff < WorldTime.TICK_EVENT_RATE && 2 * diff >= -WorldTime.TICK_EVENT_RATE; }
/** * Returns an estimate for the total number of distinct elements that have been added to this * Bloom filter. This approximation is reasonably accurate if it does not exceed the value of * {@code expectedInsertions} that was used when constructing the filter. * * @since 22.0 */ public long approximateElementCount() { long bitSize = bits.bitSize(); long bitCount = bits.bitCount(); /** * Each insertion is expected to reduce the # of clear bits by a factor of * `numHashFunctions/bitSize`. So, after n insertions, expected bitCount is `bitSize * (1 - (1 - * numHashFunctions/bitSize)^n)`. Solving that for n, and approximating `ln x` as `x - 1` when x * is close to 1 (why?), gives the following formula. */ double fractionOfBitsSet = (double) bitCount / bitSize; return DoubleMath.roundToLong( -Math.log1p(-fractionOfBitsSet) * bitSize / numHashFunctions, RoundingMode.HALF_UP); }
@ScalarOperator(CAST) @SqlType(StandardTypes.BIGINT) public static long castToLong(@SqlType(StandardTypes.DOUBLE) double value) { try { return DoubleMath.roundToLong(value, HALF_UP); } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Unable to cast %s to bigint", value), e); } }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundInfiniteToLongAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToLong(Double.POSITIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } try { DoubleMath.roundToLong(Double.NEGATIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundNaNToLongAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToLong(Double.NaN, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
@Override public void set(double v) { long lv; try { // TODO configurable rounding mode lv = DoubleMath.roundToLong(v, RoundingMode.HALF_UP); } catch (ArithmeticException ex) { // NaN / Infinite / -Infinite defaultValue.setLong(pageBuilder, column); return; } pageBuilder.setLong(column, lv); }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundExactFractionalDoubleToLongFails() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { try { DoubleMath.roundToLong(d, UNNECESSARY); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundExactIntegralDoubleToLong() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { // every mode except UNNECESSARY BigDecimal expected = new BigDecimal(d).setScale(0, UNNECESSARY); boolean isInBounds = expected.compareTo(MAX_LONG_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_LONG_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.longValue(), DoubleMath.roundToLong(d, UNNECESSARY)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundFractionalDoubleToLong() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { BigDecimal expected = new BigDecimal(d).setScale(0, mode); boolean isInBounds = expected.compareTo(MAX_LONG_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_LONG_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.longValue(), DoubleMath.roundToLong(d, mode)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } } }
@GwtIncompatible // DoubleMath.roundToLong(double, RoundingMode) public void testRoundIntegralDoubleToLong() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { BigDecimal expected = new BigDecimal(d).setScale(0, mode); boolean isInBounds = expected.compareTo(MAX_LONG_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_LONG_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.longValue(), DoubleMath.roundToLong(d, mode)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } } }
/** * Returns an estimate for the total number of distinct elements that have been added to this * Bloom filter. This approximation is reasonably accurate if it does not exceed the value of * {@code expectedInsertions} that was used when constructing the filter. * * @since 22.0 */ public long approximateElementCount() { long bitSize = bits.bitSize(); long bitCount = bits.bitCount(); /** * Each insertion is expected to reduce the # of clear bits by a factor of * `numHashFunctions/bitSize`. So, after n insertions, expected bitCount is `bitSize * (1 - (1 - * numHashFunctions/bitSize)^n)`. Solving that for n, and approximating `ln x` as `x - 1` when x * is close to 1 (why?), gives the following formula. */ double fractionOfBitsSet = (double) bitCount / bitSize; return DoubleMath.roundToLong( -Math.log1p(-fractionOfBitsSet) * bitSize / numHashFunctions, RoundingMode.HALF_UP); }
/** * Returns an estimate for the total number of distinct elements that have been added to this * Bloom filter. This approximation is reasonably accurate if it does not exceed the value of * {@code expectedInsertions} that was used when constructing the filter. * * @since 22.0 */ public long approximateElementCount() { long bitSize = bits.bitSize(); long bitCount = bits.bitCount(); /** * Each insertion is expected to reduce the # of clear bits by a factor of * `numHashFunctions/bitSize`. So, after n insertions, expected bitCount is `bitSize * (1 - (1 - * numHashFunctions/bitSize)^n)`. Solving that for n, and approximating `ln x` as `x - 1` when x * is close to 1 (why?), gives the following formula. */ double fractionOfBitsSet = (double) bitCount / bitSize; return DoubleMath.roundToLong( -Math.log1p(-fractionOfBitsSet) * bitSize / numHashFunctions, RoundingMode.HALF_UP); }
private static long toFixedPrecision(double ordinate, RoundingMode mode) { long fixedPrecisionOrdinate = DoubleMath.roundToLong(ordinate * FIXED_PRECISION_FACTOR, mode); return fixedPrecisionOrdinate; }
private static long toFixedPrecision(double ordinate, RoundingMode mode) { long fixedPrecisionOrdinate = DoubleMath.roundToLong(ordinate * FIXED_PRECISION_FACTOR, mode); return fixedPrecisionOrdinate; }
private static long doubleToLong(double value, boolean doubleToLongBits) { if (doubleToLongBits) { return Double.doubleToLongBits(value); } else { return DoubleMath.roundToLong(value, RoundingMode.HALF_UP); } }
/** * Computes the travel time for this vehicle to any point. * @param p The point to calculate travel time to. * @param timeUnit The time unit used in the simulation. * @return The travel time in the used time unit. */ protected long computeTravelTimeTo(Point p, Unit<Duration> timeUnit) { return DoubleMath.roundToLong( getRoadModel().getPathTo(this, p, timeUnit, speed.get(), routeHeuristic) .getTravelTime(), RoundingMode.CEILING); }
@Benchmark long roundToLong(int reps) { long tmp = 0; for (int i = 0; i < reps; i++) { int j = i & ARRAY_MASK; tmp += DoubleMath.roundToLong(doubleInLongRange[j], mode); } return tmp; }