Example usage for java.lang Math floor

List of usage examples for java.lang Math floor

Introduction

In this page you can find the example usage for java.lang Math floor.

Prototype

public static double floor(double a) 

Source Link

Document

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

Usage

From source file:Main.java

private static int computeInitialSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == -1) ? 128
            : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }/* ww w  . jav  a2s  . c o m*/

    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}

From source file:Main.java

/**
 * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain
 * precision, BigDecimal objects are used.
 *
 * @param rgb// www. ja  v  a2 s  .  c o m
 * @return
 */
public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) {
    int[] hsb = new int[3];
    int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]);
    int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]);
    int diff = maxChroma - minChroma;

    // Hue
    BigDecimal hue;
    if (diff == 0) {
        hue = BigDecimal.ZERO;
    } else if (maxChroma == rgb[0]) {
        float tmp = (rgb[1] - rgb[2]) / (float) diff;
        if (tmp < 0) {
            tmp += 6 * Math.ceil(-tmp / 6.0);
        } else {
            tmp -= 6 * Math.floor(tmp / 6.0);
        }
        hue = BigDecimal.valueOf(tmp);
    } else if (maxChroma == rgb[1]) {
        hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2);
    } else {
        hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4);
    }
    // [0, 360] -> [0, 0xffffffff]
    hue = hue.multiply(BigDecimal.valueOf(0xffffffffL));
    hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR);
    hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4);

    // Saturation
    if (maxChroma == 0) {
        hsb[1] = 0;
    } else {
        // [0, 1] -> [0, 0xffffffff]
        BigDecimal sat = BigDecimal.valueOf(diff);
        sat = sat.multiply(BigDecimal.valueOf(0xffffffffL));
        sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR);
        hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4);
    }

    // Brightness
    // [0, 255] -> [0, 0xffffffff]
    BigDecimal brightness = BigDecimal.valueOf(maxChroma);
    brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL));
    brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR);
    hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4);

    return hsb;
}

From source file:Main.java

/**
 * Ceil function in original algorithm/*from w  w  w  .j  a  v a 2  s  .  com*/
 * 
 * @param double1
 * @param double2
 * @return long
 */
public static long ceil(double double1, double double2) {
    return (long) (double1 - double2 * Math.floor(double1 / double2));
}

From source file:Main.java

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
        int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels < 0) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength < 0) ? 128
            : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }//from   w  w w .j  a  v  a 2  s  .  com

    if (maxNumOfPixels < 0 && minSideLength < 0) {
        return 1;
    } else if (minSideLength < 0) {
        return lowerBound;
    } else {
        return upperBound;
    }
}

From source file:Main.java

/**
 * Method to add a random delay or if none to yield to give any waiting
 * threads a chance. This helps make test threaded code more random to track
 * synchronized issues./*w  ww  .  j  a va 2 s . co m*/
 *
 * @param millis
 *            up to the many ms
 * @return actual sleep time
 */
public static final int sleepUpTo(final long millis) {
    try {
        if (millis > 0) {
            int realSleep = (int) Math.floor(Math.random() * (millis + 1));
            if (realSleep > 0) {
                Thread.sleep(realSleep);
                return realSleep;
            } else {
                // Give any waiting threads a go
                Thread.yield();
                return 0;
            }
        }
    } catch (final InterruptedException e) {
        // It's not an error that we were Interrupted!! Don't log the
        // exception !
        return -1;
    }
    return 0;
}

From source file:Main.java

public static double getJulDate() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    double extra = (100.0 * year) + month - 190002.5;
    double julianDay = (367.0 * year) - (Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0))
            + Math.floor((275.0 * month) / 9.0) + day + ((hour + ((minute + (second / 60.0)) / 60.0)) / 24.0)
            + 1721013.5 - ((0.5 * extra) / Math.abs(extra)) + 0.5;
    DecimalFormat sixDigitFormat = new DecimalFormat("#.######");
    return Double.valueOf(sixDigitFormat.format(julianDay));
}

From source file:Main.java

public static double getQuantile(Collection<Double> l, double alpha) {
    List<Double> sorted = new ArrayList<Double>(l);
    Collections.sort(sorted);//from   w w  w.  j a  va2  s  .  c o  m
    int size = sorted.size();

    if (size == 0)
        return Double.NaN;

    double kDouble = (size - 1) * alpha;
    int k = (int) Math.floor(kDouble);
    double g = kDouble - k;

    double lowerValue = sorted.get(k);

    if (sorted.size() <= k + 1)
        return lowerValue;

    double upperValue = sorted.get(k + 1);

    return (1 - g) * lowerValue + g * upperValue;

}

From source file:Main.java

/**
 * Calculate the Julian Day for a given date using the following formula:
 * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
 *      + D + 1721013.5 + UT/24/*w  w  w  .j a  v  a2s. c  om*/
 *
 * Note that this is only valid for the year range 1900 - 2099.
 */
public static double calculateJulianDay(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);

    double hour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0f
            + cal.get(Calendar.SECOND) / 3600.0f;

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);

    double jd = 367.0 * year - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
            + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour / 24.0;
    return jd;
}

From source file:Main.java

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int sampleSize = 1;
    int height = options.outHeight;
    int width = options.outWidth;
    if (height > reqHeight || width > reqWidth) {
        final int heightRatio;
        final int widthRatio;
        if (reqHeight == 0) {
            sampleSize = (int) Math.floor((float) width / (float) reqWidth);
        } else if (reqWidth == 0) {
            sampleSize = (int) Math.floor((float) height / (float) reqHeight);
        } else {/*from  w ww  . j  a va  2s .  c  o m*/
            heightRatio = (int) Math.floor((float) height / (float) reqHeight);
            widthRatio = (int) Math.floor((float) width / (float) reqWidth);
            sampleSize = Math.max(heightRatio, widthRatio);
        }
    }
    return sampleSize;
}

From source file:Main.java

/**
 * Returns the 'percentile' value in array
 * @param array   A {@code double} array 
 * @param percentile The percentile value to obtain between 0-1
 * @return returns value at {@code percentile} in {@code array}
 * *///from w  w w  . ja va  2  s  .  c  o  m
public static double percentile(double[] array, double percentile) {
    Arrays.sort(array);
    if (array.length == 0 || percentile < 0 || percentile > 1)
        throw new IllegalArgumentException();
    double k = (array.length - 1) * percentile;
    double f = Math.floor(k);
    double c = Math.ceil(k);
    if (f == c)
        return array[(int) k];
    return array[(int) f] * (c - k) + array[(int) c] * (k - f);
}