private static byte[] combineBuffers(Deque<byte[]> bufs, int totalLen) { byte[] result = new byte[totalLen]; int remaining = totalLen; while (remaining > 0) { byte[] buf = bufs.removeFirst(); int bytesToCopy = Math.min(remaining, buf.length); int resultOffset = totalLen - remaining; System.arraycopy(buf, 0, result, resultOffset, bytesToCopy); remaining -= bytesToCopy; } return result; }
jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px"); this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px"); return this; }
/** Returns the distance between the given line and point. Note the specified line is not a line segment. */ public static float distanceLinePoint (float startX, float startY, float endX, float endY, float pointX, float pointY) { float normalLength = (float)Math.sqrt((endX - startX) * (endX - startX) + (endY - startY) * (endY - startY)); return Math.abs((pointX - startX) * (endY - startY) - (pointY - startY) * (endX - startX)) / normalLength; }
public static String humanReadableByteCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); }
@Override public EventExecutor next() { return executors[Math.abs(idx.getAndIncrement() % executors.length)]; } }
/** * Computes the optimal k (number of hashes per element inserted in Bloom filter), given the * expected insertions and total number of bits in the Bloom filter. * * <p>See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula. * * @param n expected insertions (must be positive) * @param m total number of bits in Bloom filter (must be positive) */ @VisibleForTesting static int optimalNumOfHashFunctions(long n, long m) { // (m / n) * log(2), but avoid truncation due to division! return Math.max(1, (int) Math.round((double) m / n * Math.log(2))); }
/** Ratio of circumradius to shortest edge as a measure of triangle quality. * <p> * Gary L. Miller, Dafna Talmor, Shang-Hua Teng, and Noel Walkington. A Delaunay Based Numerical Method for Three Dimensions: * Generation, Formulation, and Partition. */ static public float triangleQuality (float x1, float y1, float x2, float y2, float x3, float y3) { float length1 = (float)Math.sqrt(x1 * x1 + y1 * y1); float length2 = (float)Math.sqrt(x2 * x2 + y2 * y2); float length3 = (float)Math.sqrt(x3 * x3 + y3 * y3); return Math.min(length1, Math.min(length2, length3)) / triangleCircumradius(x1, y1, x2, y2, x3, y3); }
int calculateCapacity(CharSequence sequence, Charset charset) { float maxBytesPerChar = this.charsetToMaxBytesPerChar .computeIfAbsent(charset, cs -> cs.newEncoder().maxBytesPerChar()); float maxBytesForSequence = sequence.length() * maxBytesPerChar; return (int) Math.ceil(maxBytesForSequence); }
@Override public void update (int screenWidth, int screenHeight, boolean centerCamera) { Vector2 scaled = scaling.apply(getWorldWidth(), getWorldHeight(), screenWidth, screenHeight); int viewportWidth = Math.round(scaled.x); int viewportHeight = Math.round(scaled.y); // Center. setScreenBounds((screenWidth - viewportWidth) / 2, (screenHeight - viewportHeight) / 2, viewportWidth, viewportHeight); apply(centerCamera); }
private static int sqrtFloor(int x) { // There is no loss of precision in converting an int to a double, according to // http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2 return (int) Math.sqrt(x); }
public String getServerId() { if (this.serverId == null) { this.serverId = String.valueOf(Math.abs(getUuid().getMostSignificantBits()) % 1000); } return this.serverId; }
value = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1 Math.floor(value) // -900719925474100 Math.ceil(value) // -900719925474099 Math.round(value) // -900719925474099 Math.trunc(value) // -900719925474099 parseInt(value) // -900719925474099 value | 0 // -858993459 ~~value // -858993459 value >> 0 // -858993459 value >>> 0 // 3435973837 value - value % 1 // -900719925474099
/** Ratio of circumradius to shortest edge as a measure of triangle quality. * <p> * Gary L. Miller, Dafna Talmor, Shang-Hua Teng, and Noel Walkington. A Delaunay Based Numerical Method for Three Dimensions: * Generation, Formulation, and Partition. */ static public float triangleQuality (float x1, float y1, float x2, float y2, float x3, float y3) { float length1 = (float)Math.sqrt(x1 * x1 + y1 * y1); float length2 = (float)Math.sqrt(x2 * x2 + y2 * y2); float length3 = (float)Math.sqrt(x3 * x3 + y3 * y3); return Math.min(length1, Math.min(length2, length3)) / triangleCircumradius(x1, y1, x2, y2, x3, y3); }