/** * recalculate interval(in hours) if total number of partitions greater than maximum number of allowed partitions * * @param diffInMilliSecs difference in range * @param hourInterval hour interval (ex: 4 hours) * @param maxIntervals max number of allowed partitions * @return calculated interval in hours */ private static int getInterval(long diffInMilliSecs, long hourInterval, int maxIntervals) { int totalHours = DoubleMath.roundToInt((double) diffInMilliSecs / (60 * 60 * 1000), RoundingMode.CEILING); int totalIntervals = DoubleMath.roundToInt((double) totalHours / hourInterval, RoundingMode.CEILING); if (totalIntervals > maxIntervals) { hourInterval = DoubleMath.roundToInt((double) totalHours / maxIntervals, RoundingMode.CEILING); } return Ints.checkedCast(hourInterval); }
/** * recalculate interval(in hours) if total number of partitions greater than maximum number of allowed partitions * * @param diffInMilliSecs difference in range * @param hourInterval hour interval (ex: 4 hours) * @param maxIntervals max number of allowed partitions * @return calculated interval in hours */ private static int getInterval(long diffInMilliSecs, long hourInterval, int maxIntervals) { long totalHours = DoubleMath.roundToInt((double) diffInMilliSecs / (60 * 60 * 1000), RoundingMode.CEILING); long totalIntervals = DoubleMath.roundToInt((double) totalHours / hourInterval, RoundingMode.CEILING); if (totalIntervals > maxIntervals) { hourInterval = DoubleMath.roundToInt((double) totalHours / maxIntervals, RoundingMode.CEILING); } return Ints.checkedCast(hourInterval); }
/** * Compute the target partition size. */ private int computeTargetPartitionSize(Histogram histogram, int minTargetPartitionSize, int maxPartitions) { return Math.max(minTargetPartitionSize, DoubleMath.roundToInt((double) histogram.totalRecordCount / maxPartitions, RoundingMode.CEILING)); }
private Color findClosestColor(float findex) { int index = DoubleMath.roundToInt(findex * (double) (colors.size() - 1), RoundingMode.HALF_UP); Color color = colors.get(index); return color; }
/** * recalculate interval(in hours) if total number of partitions greater than maximum number of allowed partitions * * @param diffInMilliSecs difference in range * @param hourInterval hour interval (ex: 24 hours) * @param maxIntervals max number of allowed partitions * @return calculated interval in days */ private static int getInterval(long diffInMilliSecs, long hourInterval, int maxIntervals) { long dayInterval = TimeUnit.HOURS.toDays(hourInterval); int totalHours = DoubleMath.roundToInt((double) diffInMilliSecs / (60 * 60 * 1000), RoundingMode.CEILING); int totalIntervals = DoubleMath.roundToInt((double) totalHours / (dayInterval * 24), RoundingMode.CEILING); if (totalIntervals > maxIntervals) { hourInterval = DoubleMath.roundToInt((double) totalHours / maxIntervals, RoundingMode.CEILING); dayInterval = DoubleMath.roundToInt((double) hourInterval / 24, RoundingMode.CEILING); } return Ints.checkedCast(dayInterval); }
@Override public void set(Float value) { int val = DoubleMath.roundToInt(value, roundingMode); intBinding.set(val); } }
public static void convertFileToTexture() throws IOException { float[][] heightmap = readFile(); double scaleFactor = 256 * 256 * 12.8; // Slick's PNGDecoder does not support 16 bit textures // BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_USHORT_GRAY); // DataBufferUShort buffer = (DataBufferUShort) image.getRaster().getDataBuffer(); // scaleFactor *= 256.0f; // Slick's PNGDecoder does not support grayscale textures // BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_BYTE_GRAY); // DataBufferByte buffer = (DataBufferByte) image.getRaster().getDataBuffer(); BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB); DataBufferInt buffer = (DataBufferInt) image.getRaster().getDataBuffer(); for (int x = 0; x < 512; x++) { for (int z = 0; z < 512; z++) { double doubleVal = heightmap[x][z] * scaleFactor; int val = DoubleMath.roundToInt(doubleVal, RoundingMode.HALF_UP); buffer.setElem(z * 512 + x, val); } } ImageIO.write(image, "png", new File("platec_heightmap.png")); }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundInfiniteToIntAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToInt(Double.POSITIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } try { DoubleMath.roundToInt(Double.NEGATIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundNaNToIntAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToInt(Double.NaN, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundExactFractionalDoubleToIntFails() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { try { DoubleMath.roundToInt(d, UNNECESSARY); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
/** * Computes the color of the facet at a given world coordinate. * @param facet the underlying facet * @param x the world x coordinate * @param z the world z coordinate * @return the color at the given world coords. Never <code>null</code>. */ protected Color getColor(FieldFacet2D facet, int x, int z) { double value = facet.get(x, z); if (Double.isFinite(value)) { int round = DoubleMath.roundToInt(config.offset + config.scale * value, RoundingMode.HALF_UP); int idx = TeraMath.clamp(round, 0, 255); return GRAYS.get(idx); } else { return MISSING; } }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundExactIntegralDoubleToInt() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { BigDecimal expected = new BigDecimal(d).setScale(0, UNNECESSARY); boolean isInBounds = expected.compareTo(MAX_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.intValue(), DoubleMath.roundToInt(d, UNNECESSARY)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundIntegralDoubleToInt() { 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_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.intValue(), DoubleMath.roundToInt(d, mode)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } } }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundFractionalDoubleToInt() { 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_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0; try { assertEquals( "Rounding " + d + " with mode " + mode, expected.intValue(), DoubleMath.roundToInt(d, mode)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } } }
private String getSpatialPartitioning(Rectangle extent, List<OGCGeometry> geometries, int partitionCount) { ImmutableList.Builder<Rectangle> rectangles = ImmutableList.builder(); for (OGCGeometry geometry : geometries) { Envelope envelope = new Envelope(); geometry.getEsriGeometry().queryEnvelope(envelope); rectangles.add(new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax())); } return KdbTreeUtils.toJson(buildKdbTree(roundToInt(geometries.size() * 1.0 / partitionCount, CEILING), extent, rectangles.build())); } }
@Override public boolean[][] getBooleanValueArray(String hash) { Preconditions.checkArgument(!StringUtils.isEmpty(hash) && hash.length() >= 16, "illegal argument hash:not null and size >= 16"); this.hash = hash; boolean[][] array = new boolean[6][5]; //初始化字符串 for (int i = 0; i < 6; i++) { for (int j = 0; j < 5; j++) { array[i][j] = false; } } for (int i = 0; i < hash.length(); i += 2) { int s = i / 2; //只取hash字符串偶数编号(从0开始)的字符 boolean v = DoubleMath.roundToInt(Integer.parseInt(hash.charAt(i) + "", 16) / 10.0, RoundingMode.HALF_UP) > 0 ? true : false; if (s % 3 == 0) { array[s / 3][0] = v; array[s / 3][4] = v; } else if (s % 3 == 1) { array[s / 3][1] = v; array[s / 3][3] = v; } else { array[s / 3][2] = v; } } this.booleanValueArray = array; return this.booleanValueArray; }
static int computeRoundedTravelTime(Measure<Double, Velocity> speed, Measure<Double, Length> dist, Unit<Duration> outputTimeUnit) { return DoubleMath.roundToInt( RoadModels.computeTravelTime(speed, dist, outputTimeUnit), RoundingMode.CEILING); }
static int[] toRemainingServiceTimes(GlobalStateObject state, Unit<Duration> outputTimeUnit) { final UnmodifiableIterator<VehicleStateObject> iterator = state.vehicles .iterator(); final int[] remainingServiceTimes = new int[state.vehicles.size()]; for (int i = 0; i < state.vehicles.size(); i++) { remainingServiceTimes[i] = DoubleMath.roundToInt( Measure.valueOf(iterator.next().remainingServiceTime, state.timeUnit) .doubleValue(outputTimeUnit), RoundingMode.CEILING); } return remainingServiceTimes; }
@GwtIncompatible // DoubleMath.roundToInt(double, RoundingMode) public void testRoundInfiniteToIntAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToInt(Double.POSITIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) {} try { DoubleMath.roundToInt(Double.NEGATIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) {} } }
private String getSpatialPartitioning(Rectangle extent, List<OGCGeometry> geometries, int partitionCount) { ImmutableList.Builder<Rectangle> rectangles = ImmutableList.builder(); for (OGCGeometry geometry : geometries) { Envelope envelope = new Envelope(); geometry.getEsriGeometry().queryEnvelope(envelope); rectangles.add(new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax())); } return KdbTreeUtils.toJson(buildKdbTree(roundToInt(geometries.size() * 1.0 / partitionCount, CEILING), extent, rectangles.build())); } }