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:ColorHelper.java

public static float[] HSVtoRGB(float h, float s, float v) {
    // H is given on [0->6] or -1. S and V are given on [0->1].
    // RGB are each returned on [0->1].
    float m, n, f;
    int i;/*ww w. j  a  v a2s.co  m*/

    float[] hsv = new float[3];
    float[] rgb = new float[3];

    hsv[0] = h;
    hsv[1] = s;
    hsv[2] = v;

    if (hsv[0] == -1) {
        rgb[0] = rgb[1] = rgb[2] = hsv[2];
        return rgb;
    }
    i = (int) (Math.floor(hsv[0]));
    f = hsv[0] - i;
    if (i % 2 == 0) {
        f = 1 - f; // if i is even
    }
    m = hsv[2] * (1 - hsv[1]);
    n = hsv[2] * (1 - hsv[1] * f);
    switch (i) {
    case 6:
    case 0:
        rgb[0] = hsv[2];
        rgb[1] = n;
        rgb[2] = m;
        break;
    case 1:
        rgb[0] = n;
        rgb[1] = hsv[2];
        rgb[2] = m;
        break;
    case 2:
        rgb[0] = m;
        rgb[1] = hsv[2];
        rgb[2] = n;
        break;
    case 3:
        rgb[0] = m;
        rgb[1] = n;
        rgb[2] = hsv[2];
        break;
    case 4:
        rgb[0] = n;
        rgb[1] = m;
        rgb[2] = hsv[2];
        break;
    case 5:
        rgb[0] = hsv[2];
        rgb[1] = m;
        rgb[2] = n;
        break;
    }

    return rgb;

}

From source file:gov.nih.nci.calims2.business.inventory.container.CoordinateHelper.java

private static String convertValue(char firstCharacter, int maximum, int value) {
    int length = (int) Math.floor(Math.log(maximum) / Math.log(LETTER_BASE)) + 1;
    if (value == 0) {
        return StringUtils.repeat(Character.toString(firstCharacter), length);
    }//from   ww w . j  ava  2s  .c  om
    String result = "";
    int remaining = value;
    while (remaining > 0) {
        result = Character.toString((char) (firstCharacter + remaining % LETTER_BASE)) + result;
        remaining /= LETTER_BASE;
    }
    while (result.length() < length) {
        result = firstCharacter + result;
    }
    return result;
}

From source file:Main.java

/**
 * Returns n!. Shorthand for <code>n</code> <a
 * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
 * product of the numbers <code>1,...,n</code> as a <code>double</code>.
 * <p>//from  w  w w  .  ja  v a  2  s .c  o  m
 * <Strong>Preconditions</strong>:
 * <ul>
 * <li> <code>n >= 0</code> (otherwise
 * <code>IllegalArgumentException</code> is thrown)</li>
 * <li> The result is small enough to fit into a <code>double</code>. The
 * largest value of <code>n</code> for which <code>n!</code> <
 * Double.MAX_VALUE</code> is 170. If the computed value exceeds
 * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned</li>
 * </ul>
 * </p>
 * 
 * @param n argument
 * @return <code>n!</code>
 * @throws IllegalArgumentException if n < 0
 */
public static double factorialDouble(final int n) {
    if (n < 0) {
        throw new IllegalArgumentException("must have n >= 0 for n!");
    }
    return Math.floor(Math.exp(factorialLog(n)) + 0.5);
}

From source file:Main.java

/**
 * Returns the closest power-of-two number less than or equal to x.
 *
 * @param x input value//  w  w w .  j a v  a2s  .c o  m
 *
 * @return the closest power-of-two number less then or equal to x
 */
public static int prevPow2(int x) {
    if (x < 1) {
        throw new IllegalArgumentException("x must be greater or equal 1");
    }
    return (int) Math.pow(2, Math.floor(Math.log(x) / Math.log(2)));
}

From source file:utils.RandomVariable.java

/**
 * Generate a random integer.//from   w w w  .  ja v  a 2 s  .  c o  m
 *
 * @param i0 min of the random variable.
 * @param i1 max of the random variable.
 * @return an int between i0 and i1.
 */
public static int randInt(int i0, int i1) {
    double x = rand();
    int i = i0 + (int) Math.floor((i1 - i0 + 1) * x);
    return i;
}

From source file:Main.java

/**
 * Auxilliary method required for merging two consecutive sorted lists in
 * place./*from   www .  j a v  a  2 s. co m*/
 *
 * <p>This implementation is based on:</p>
 *
 * <p>J. Chen, "<a href="http://dx.doi.org/10.1016/j.ipl.2005.11.018">A
 * simple algorithm for in-place merging</a>", Information Processing
 * Letters 98:34-40, 2006.</p>.
 *
 * This method is a direct transcription of Fig. 4.
 */
private static <T extends Comparable<? super T>> int findNextXBlock(List<T> A, int x0, int z, int y, int k,
        int f, int b1, int b2) {
    T min1 = null, min2 = null;
    int m = (int) Math.floor(((z - x0 - f) / (double) k)) * k + f + x0;
    if (m <= z) {
        m += k;
    }
    int i = m;
    int j, x = i;
    while ((i + k) <= y) {
        if ((i != b1) && (i != b2)) {
            if ((i < b1) && (b1 < i + k)) {
                j = m - 1;
            } else {
                j = i + k - 1;
            }
            if (min1 == null || (A.get(i).compareTo(min1) <= 0 && A.get(j).compareTo(min2) <= 0)) {
                x = i;
                min1 = A.get(i);
                min2 = A.get(j);
            }
        }
        i += k;
    }
    return x;
}

From source file:Rnd.java

/**
 * @param min/*from   w  w  w .  j  a v  a2s. c  o  m*/
 * @param max
 * @return value
 */
public static int get(int min, int max) // get random number from
// min to max (not max-1 !)
{
    return min + (int) Math.floor(rnd.nextDouble() * (max - min + 1));
}

From source file:TimeFormatUtil.java

public static String getTimeLabel(double seconds, boolean longFormat) {
    int secs = new Double(Math.floor(seconds)).intValue();

    int hours = secs / 3600;
    secs = secs % 3600;/*w  w w  .  jav a 2s  .  c  o m*/

    int minutes = secs / 60;
    secs = secs % 60;

    StringBuilder b = new StringBuilder();

    if (hours > 0) {
        if (hours < 10)
            b.append(0);

        b.append(hours);
        b.append(longFormat ? "h " : ":");
    }

    if (minutes < 10)
        b.append(0);

    b.append(minutes);
    b.append(longFormat ? "m " : ":");

    if (secs < 10)
        b.append(0);

    b.append(secs);

    if (longFormat) {
        int hundreths = Double.valueOf((seconds - secs) * 100).intValue();
        b.append(".");
        b.append(hundreths);
        b.append("s");
    }

    return b.toString();
}

From source file:StringUtils.java

/**
 * Format a percentage for presentation to the user.
 * @param done the percentage to format (0.0 to 1.0)
 * @param digits the number of digits past the decimal point
 * @return a string representation of the percentage
 *///w w  w. java2s.  c o  m
public static String formatPercent(double done, int digits) {
    DecimalFormat percentFormat = new DecimalFormat("0.00%");
    double scale = Math.pow(10.0, digits + 2);
    double rounded = Math.floor(done * scale);
    percentFormat.setDecimalSeparatorAlwaysShown(false);
    percentFormat.setMinimumFractionDigits(digits);
    percentFormat.setMaximumFractionDigits(digits);
    return percentFormat.format(rounded / scale);
}

From source file:boutiqueenligne.service.CalculPrixTotalAvecPromoService.java

public void generationCodePromo(int valeur, TypeCode TypeCode) {
    CodePromo code = new CodePromo();
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    String codeg = "";
    for (int x = 0; x < 5; x++) {
        int i = (int) Math.floor(Math.random() * 36);
        codeg += chars.charAt(i);/*  w  w w . j a v a  2s . c  o  m*/
    }
    code.setCode(codeg);
    code.setType(TypeCode);
    code.setValeur(valeur);
    codePromoService.save(code);
}