List of usage examples for java.lang Math floor
public static double floor(double a)
From source file:Main.java
/** * Goal is to return a reasonable string representation * of x, using at most width spaces. (If the parameter width is * unreasonably big or small, its value is adjusted to * lie in the range 6 to 25.)//from w ww. j a v a 2 s. c o m * * @param x value to create string representation of. * @param width maximum number of spaces used in string representation, if possible. * @return a string representation for x. If x is Double.NaN, "undefined" is returned. * If x is infinite, "INF" or "-INF" is returned. */ public static String realToString(double x, int width) { width = Math.min(25, Math.max(6, width)); if (Double.isNaN(x)) return "undefined"; if (Double.isInfinite(x)) if (x < 0) return "-INF"; else return "INF"; String s = String.valueOf(x); if (Math.rint(x) == x && Math.abs(x) < 5e15 && s.length() <= (width + 2)) return String.valueOf((long) x); // return string without trailing ".0" if (s.length() <= width) return s; boolean neg = false; if (x < 0) { neg = true; x = -x; width--; s = String.valueOf(x); } long maxForNonExp = 5 * (long) Math.pow(10, width - 2); if (x >= 0.0005 && x <= maxForNonExp && (s.indexOf('E') == -1 && s.indexOf('e') == -1)) { s = round(s, width); s = trimZeros(s); } else if (x > 1) { // construct exponential form with positive exponent long power = (long) Math.floor(Math.log(x) / Math.log(10)); String exp = "E" + power; int numlength = width - exp.length(); x = x / Math.pow(10, power); s = String.valueOf(x); s = round(s, numlength); s = trimZeros(s); s += exp; } else { // constuct exponential form with negative argument long power = (long) Math.ceil(-Math.log(x) / Math.log(10)); String exp = "E-" + power; int numlength = width - exp.length(); x = x * Math.pow(10, power); s = String.valueOf(x); s = round(s, numlength); s = trimZeros(s); s += exp; } if (neg) return "-" + s; else return s; }
From source file:com.comphenix.xp.SampleRange.java
public int sampleInt(Random rnd) { /*/*from w w w . j a v a2 s . co m*/ * Imagine our range is 0.7 - 5.3: * * 0.7 1 2 3 4 5 5.3 * * |---|----------|----------|----------|----------|---| * | | | | | | | * | | | | | | | * |---|----------|----------|----------|----------|---| * * The integer part, 1 - 5, is easy. To get a random number between and * including 1 and 5, we simply get a random number between 0 and 4 * and add one. * * The beginning, 0.7 - 1.0, covers 30% of an integer. One interpretation is * that this indicates the probability of getting that integer. * * So, we end up with a 30% probability of getting 0 and 5.3 - 5 = 30% * probability of getting 4. */ int value = 0; // Convert the range to an integer equivalent. // Notice that we round to shrink the range. int a = (int) Math.ceil(start); int b = (int) Math.floor(end); // Special case if ((int) start == (int) end) { return sampleIntReduced(rnd); } // The decimal leftover double dA = a - start; double dB = end - b; // Sample an integer from the range [a, b] (inclusive) if (b > a) { value = a + rnd.nextInt(b - a + 1); // Add one since nextInt is exclusive } // The remainder is the probability of choosing the previous value if (dA > 0 && rnd.nextDouble() < dA) value--; // And here it is the probability of choosing the next value if (dB > 0 && rnd.nextDouble() < dB) value++; return value; }
From source file:com.adobe.acs.commons.images.transformers.impl.ScaleImageTransformerImpl.java
@Override public final Layer transform(Layer layer, final ValueMap properties) { if (properties == null || properties.isEmpty()) { log.warn("Transform [ {} ] requires parameters.", TYPE); return layer; }//from w ww.j a v a2s. com log.debug("Transforming with [ {} ]", TYPE); Double scale = properties.get(KEY_SCALE, 1D); String round = StringUtils.trim(properties.get(KEY_ROUND, String.class)); if (scale == null) { log.warn("Could not derive a Double value for key [ {} ] from value [ {} ]", KEY_SCALE, properties.get(KEY_SCALE, String.class)); scale = 1D; } if (scale != 1D) { int currentWidth = layer.getWidth(); int currentHeight = layer.getHeight(); double newWidth = scale * currentWidth; double newHeight = scale * currentHeight; if (StringUtils.equals(ROUND_UP, round)) { newWidth = (int) Math.ceil(newWidth); newHeight = (int) Math.ceil(newHeight); } else if (StringUtils.equals(ROUND_DOWN, round)) { newWidth = (int) Math.floor(newWidth); newHeight = (int) Math.floor(newHeight); } else { // "round" newWidth = (int) Math.round(newWidth); newHeight = (int) Math.round(newHeight); } // Invoke the ResizeImageTransformer with the new values final ValueMap params = new ValueMapDecorator(new HashMap<String, Object>()); params.put(ResizeImageTransformerImpl.KEY_WIDTH, (int) newWidth); params.put(ResizeImageTransformerImpl.KEY_HEIGHT, (int) newHeight); layer = resizeImageTransformer.transform(layer, params); } return layer; }
From source file:com.poscoict.license.util.LmsUtil.java
public byte[] hexToByteArray(String hex) { if (hex == null || hex.length() % 2 != 0) { return new byte[] {}; }/*w ww. j a va2s . com*/ byte[] bytes = new byte[hex.length() / 2]; for (int i = 0; i < hex.length(); i += 2) { byte value = (byte) Integer.parseInt(hex.substring(i, i + 2), 16); bytes[(int) Math.floor(i / 2)] = value; } return bytes; }
From source file:doge.photo.DogePhotoManipulator.java
private BufferedImage manipulate(BufferedImage sourceImage) { double aspectRatio = sourceImage.getHeight() / (double) sourceImage.getWidth(); int height = (int) Math.floor(IMAGE_WIDTH * aspectRatio); BufferedImage destinationImage = new BufferedImage(IMAGE_WIDTH, height, BufferedImage.TYPE_INT_RGB); render(sourceImage, destinationImage); return destinationImage; }
From source file:Main.java
/** * Draws a single arrow head//from w ww . ja va 2s. co m * * @param aG * the canvas to draw on; * @param aXpos * the X position of the arrow head; * @param aYpos * the (center) Y position of the arrow head; * @param aFactor * +1 to have a left-facing arrow head, -1 to have a right-facing * arrow head; * @param aArrowWidth * the total width of the arrow head; * @param aArrowHeight * the total height of the arrow head. */ public static final void drawArrowHead(final Graphics2D aG, final int aXpos, final int aYpos, final int aFactor, final int aArrowWidth, final int aArrowHeight) { final double halfHeight = aArrowHeight / 2.0; final int x1 = aXpos + (aFactor * aArrowWidth); final int y1 = (int) Math.ceil(aYpos - halfHeight); final int y2 = (int) Math.floor(aYpos + halfHeight); final Polygon arrowHead = new Polygon(); arrowHead.addPoint(aXpos, aYpos); arrowHead.addPoint(x1, y1); arrowHead.addPoint(x1, y2); aG.fill(arrowHead); }
From source file:eu.crisis_economics.abm.fund.TestExogenousFund.java
/** * Test whether an instance of {@link ExogenousFund} behaves as * expected. This test operates as follows:<br><br> * //from w w w. jav a 2 s. c o m * {@code (a)} * An {@link ExogenousFund} entity is created. The deposits of this * {@link Fund} are held by an instance of {@link ExogenousDepositHolder};<br> * {@code (b)} * The {@link ExogenousFund} is provided with {@code floor(t)} units of * exogenous cash at simulation time {@code t}, for all {@code t};<br> * {@code (c)} * One {@link DepositingHousehold} {@link Agent} opens an investment * account with the {@link ExogenousFund}. {@code 1.0} is invested at * the time the account is created;<br> * {@code (d)} * Several simulation cycles elapse. After each cycle has elapsed, it is * asserted that the exogenous fund income has been transferred to the * {@link DepositingHousehold}. */ @Test public void testExogenousFundWithExogenousIncome() { final ExogenousDepositHolder depositHolder = new ExogenousDepositHolder(); final ExogenousFund fund = new ExogenousFund(depositHolder, new FundFactory() { @Override public Fund create(final DepositHolder depositHolder) { return new TrivialFund(depositHolder, new ClearingHouse()); } }, new FunctionParameter(new UnivariateFunction() { @Override public double value(double t) { return Math.floor(t); } }, "Parameter")); final DepositingHousehold household = new DepositingHousehold(depositHolder); household.debit(1.0); try { fund.invest(household, 1.); } catch (final InsufficientFundsException neverThrows) { Assert.fail(); } advanceTimeByOneCycle(); advanceTimeByOneCycle(); // Fires at T = 0 advanceTimeByOneCycle(); // Fires at T = 1 // At this point the exogenous fund has been debitted with 1.0 // from exogenous sources. This income should have been passed to // clients. Assert.assertEquals(household.getTotalAssets(), 2.0, 1.e-12); advanceTimeByOneCycle(); // Fires at T = 2 Assert.assertEquals(household.getTotalAssets(), 4.0, 1.e-12); advanceTimeByOneCycle(); // Fires at T = 3 Assert.assertEquals(household.getTotalAssets(), 7.0, 1.e-12); Assert.assertTrue(household.getTotalLiabilities() == 0.); Assert.assertEquals(fund.getTotalLiabilities(), 7.0, 1.e-12); Assert.assertEquals(fund.getTotalAssets(), 7.0, 1.e-12); Assert.assertEquals(fund.getBalance(household), 7.0, 1.e-12); }
From source file:com.googlecode.psiprobe.jsp.VisualScoreTag.java
StringBuffer calculateSuffix(String body) { if (value < minValue) { log.info("value " + value + " is less than min value " + minValue); value = minValue;/* www . ja va2 s. c o m*/ } if (value > maxValue) { log.info("value " + value + " is greater than max value " + maxValue); value = maxValue; } if (value + value2 < minValue || value2 < 0) { log.info("value2 " + value2 + " is less than min value"); value2 = 0; } if (value + value2 > maxValue) { log.info("value2 " + value2 + " is greater than max value"); value2 = maxValue - value; } double unitSize = (maxValue - minValue) / (fullBlocks * partialBlocks); double blockWidth = unitSize * partialBlocks; int fullRedBlockCount = (int) Math.floor(value / blockWidth); int partialRedBlockIndex = (int) Math.floor((value - fullRedBlockCount * blockWidth) / unitSize); int partialBlueBlockIndex1 = (partialRedBlockIndex > 0 ? Math.min((int) Math.floor(value2 / unitSize), partialBlocks - partialRedBlockIndex) : 0); int fullBlueBlockCount = Math.max(0, (int) Math.ceil(value2 / blockWidth) - (partialRedBlockIndex > 0 ? 1 : 0)); int partialBlueBlockIndex2 = (int) Math.floor( (value2 - (fullBlueBlockCount * blockWidth) - (partialBlueBlockIndex1 * unitSize)) / unitSize); StringBuffer buf = new StringBuffer(); // Beginning if (showA) { String format = WHITE_LEFT_BORDER; if (fullRedBlockCount > 0 || partialRedBlockIndex > 0) { format = RED_LEFT_BORDER; } else if (partialBlueBlockIndex1 == 0 && (fullBlueBlockCount > 0 || partialBlueBlockIndex2 > 0)) { format = BLUE_LEFT_BORDER; } buf.append(MessageFormat.format(body, new Object[] { format })); } // Full red blocks String fullRedBody = MessageFormat.format(body, new Object[] { partialBlocks + "+0" }); for (int i = 0; i < fullRedBlockCount; i++) { buf.append(fullRedBody); } // Mixed red/blue block (mid-block transition) if (partialRedBlockIndex > 0) { String partialBody = MessageFormat.format(body, new Object[] { partialRedBlockIndex + "+" + partialBlueBlockIndex1 }); buf.append(partialBody); } // Full blue blocks String fullBlueBody = MessageFormat.format(body, new Object[] { "0+" + partialBlocks }); for (int i = 0; i < fullBlueBlockCount; i++) { buf.append(fullBlueBody); } // Partial blue block if (partialBlueBlockIndex2 > 0) { String partialBody = MessageFormat.format(body, new Object[] { "0+" + partialBlueBlockIndex2 }); buf.append(partialBody); } // Empty blocks int emptyBlocks = showEmptyBlocks ? fullBlocks - (fullRedBlockCount + fullBlueBlockCount + (partialRedBlockIndex > 0 ? 1 : 0) + (partialBlueBlockIndex2 > 0 ? 1 : 0)) : 0; if (emptyBlocks > 0) { String emptyBody = MessageFormat.format(body, new Object[] { "0+0" }); for (int i = 0; i < emptyBlocks; i++) { buf.append(emptyBody); } } // End if (showB) { String format = WHITE_RIGHT_BORDER; if (fullRedBlockCount == fullBlocks) { format = RED_RIGHT_BORDER; } else if (fullRedBlockCount + (partialRedBlockIndex + partialBlueBlockIndex1 == partialBlocks ? 1 : 0) + fullBlueBlockCount == fullBlocks) { format = BLUE_RIGHT_BORDER; } buf.append(MessageFormat.format(body, new Object[] { format })); } return buf; }
From source file:com.github.gdfm.shobaidogu.StatsUtils.java
/** * Computes a similarity level in [0, numLevels - 1]. * /*w w w . j av a2s. co m*/ * @param similarity * similarity score in [0,1]. * @param numLevels * number of discrete levels to use. * @return a quantized similarity level. */ public static int quantizeSimilarity(double similarity, int numLevels) { checkArgument(similarity >= 0 && similarity <= 1, "Similarity should be in [0,1]: " + similarity); checkArgument(numLevels > 1, "Number of levels should be greater than one: " + numLevels); return (int) Math.min(Math.floor(similarity * numLevels), numLevels - 1); }
From source file:com.adobe.acs.commons.data.Variant.java
private void setValue(Cell cell) { int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_FORMULA) { cellType = cell.getCachedFormulaResultType(); }/*w w w .j a va 2 s . co m*/ switch (cellType) { case Cell.CELL_TYPE_BOOLEAN: setValue(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: double number = cell.getNumericCellValue(); if (Math.floor(number) == number) { setValue((long) number); } else { setValue(number); } if (DateUtil.isCellDateFormatted(cell)) { setValue(cell.getDateCellValue()); } DataFormatter dataFormatter = new DataFormatter(); if (cellType == Cell.CELL_TYPE_FORMULA) { setValue(dataFormatter.formatCellValue(cell)); } else { CellStyle cellStyle = cell.getCellStyle(); setValue(dataFormatter.formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString())); } break; case Cell.CELL_TYPE_STRING: setValue(cell.getStringCellValue().trim()); break; case Cell.CELL_TYPE_BLANK: default: clear(); break; } }