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

/**
 * Converts an Oid in dot-separated string representation to a
 * DER-encoded byte array.//  w w w  . jav  a  2  s.c om
 *
 * @param oid Oid in dot-separated String representation
 * @return DER-encoded Oid byte array
 *
 */
public static byte[] OidString2DER(String oid) {

    int octet = 0;
    int base = 0;
    int times = 0;
    int tbMaj = 0;

    ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
    ByteArrayOutputStream tmpArray = new ByteArrayOutputStream();

    /* Convert String to int[] */
    String[] tmp = oid.split("\\.");
    int[] input = new int[tmp.length];
    for (int i = 0; i < tmp.length; i++) {
        input[i] = Integer.parseInt(tmp[i]);
    }

    /* Add tag for OID (0x06) */
    bytearray.write(6);

    /* Calculate first byte */
    tmpArray.write((input[0] * 40) + input[1]);

    /* Encode the rest of the OID nodes in DER format */
    for (int j = 2; j < input.length; j++) {

        if (input[j] <= 127) {

            /* Encode directly */
            tmpArray.write(input[j]);

        } else if (input[j] > 127) {

            /* Reset variables */
            octet = input[j];
            base = 128;
            times = 0;
            tbMaj = 8;

            /* If bigger than 16383 */
            if (octet > 16383) {

                base = 262144;
                times = (int) Math.floor(octet / base);
                tbMaj = 8 + times;
                octet = octet - (times * base);

                base = 16384;
                times = (int) Math.floor(octet / base);
                tmpArray.write((16 * tbMaj) + times);

                /* Reset tbMaj in case we're skipping next if */
                tbMaj = 8;
            }

            /* 2047 < octet <= 16383 */
            if (octet > 2047 && octet <= 16383) {

                base = 2048;
                times = (int) Math.floor(octet / base);
                tbMaj = 8 + times;
                octet = octet - (times * base);
            }

            /* 127 < octet < 2047 */
            base = 128;
            times = (int) Math.floor(octet / base);
            tmpArray.write((16 * tbMaj) + times);
            tmpArray.write(octet - (times * 128));
        }
    }

    byte[] convArray = tmpArray.toByteArray();
    bytearray.write(convArray.length);
    for (int k = 0; k < convArray.length; k++) {
        bytearray.write(convArray[k]);
    }

    return bytearray.toByteArray();

}

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 == -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;
    }/*from  ww w . ja  va 2s . co  m*/

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

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*  w w  w . j  av a  2s. c  o  m*/
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:Rnd.java

/**
 * Gets a random number from 0(inclusive) to n(exclusive)
 * /*ww w  .  ja v  a  2 s .c  o m*/
 * @param n
 *            The superior limit (exclusive)
 * @return A number from 0 to n-1
 */
public static int get(int n) {
    return (int) Math.floor(rnd.nextDouble() * n);
}

From source file:Main.java

/**
 * Computes a reasonable number of labels for a data range.
 * // w ww  .j  a  va 2 s.c  o  m
 * @param start start value
 * @param end final value
 * @param approxNumLabels desired number of labels
 * @return double[] array containing {start value, end value, increment}
 */
private static double[] computeLabels(final double start, final double end, final int approxNumLabels,
        double minimumJump) {
    if (Math.abs(start - end) < 0.0000001f) {
        return new double[] { start, start, 0 };
    }
    double s = start;
    double e = end;
    boolean switched = false;
    if (s > e) {
        switched = true;
        double tmp = s;
        s = e;
        e = tmp;
    }
    double xStep = roundUp(Math.max(Math.abs(s - e) / approxNumLabels, minimumJump));
    // Compute x starting point so it is a multiple of xStep.
    double xStart = xStep * Math.ceil(s / xStep);
    double xEnd = xStep * Math.floor(e / xStep);
    if (switched) {
        return new double[] { xEnd, xStart, -1.0 * xStep };
    }
    return new double[] { xStart, xEnd, xStep };
}

From source file:com.weib.concert.CDPlayer.java

public void playShuffle() {
    this.cd.pull();
    for (int i = 0; i < 20; i++) {
        int index = (int) Math.floor(Math.random() * (double) this.cd.getTrackNumber());
        this.cd.play(index);
    }/*from  w w  w  . j  a v  a  2  s .  c  o  m*/
    this.cd.push();
}

From source file:Main.java

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

    final int UNCONSTRAINED = -1;
    int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == UNCONSTRAINED) ? 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 .  jav a2 s .c  o  m*/

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

From source file:Main.java

public static <T> List<T>[] split(Collection<T> set, int n) {
    if (set.size() >= n) {
        @SuppressWarnings("unchecked")
        List<T>[] arrays = new List[n];
        int minSegmentSize = (int) Math.floor(set.size() / (double) n);

        int start = 0;
        int stop = minSegmentSize;

        Iterator<T> it = set.iterator();

        for (int i = 0; i < n - 1; i++) {
            int segmentSize = stop - start;
            List<T> segment = new ArrayList<T>(segmentSize);
            for (int k = 0; k < segmentSize; k++) {
                segment.add(it.next());//  w  w w .  j a v a2s  .  c  o m
            }
            arrays[i] = segment;
            start = stop;
            stop += segmentSize;
        }

        int segmentSize = set.size() - start;
        List<T> segment = new ArrayList<T>(segmentSize);
        for (int k = 0; k < segmentSize; k++) {
            segment.add(it.next());
        }
        arrays[n - 1] = segment;

        return arrays;
    } else {
        throw new IllegalArgumentException("n must not be smaller set size!");
    }
}

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 == UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == UNCONSTRAINED) ? 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 ava  2  s .  c  o m

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

From source file:boutiqueEnLigne.service.GenerationNouveauCodePromoService.java

public void generate(int length, TypeCode tc, Double valeur) {
    String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Tu supprimes les lettres dont tu ne veux pas
    String pass = "";
    for (int x = 0; x < length; x++) {
        int i = (int) Math.floor(Math.random() * 62); // Si tu supprimes des lettres tu diminues ce nb
        pass += chars.charAt(i);/*from   w ww  .  j ava  2s . co m*/
    }
    System.out.println(pass);
    CodePromo cp = new CodePromo();
    cp.setCode(pass);
    cp.setTypecode(tc);
    cp.setValeur(valeur);
    codePromoService.save(cp);

}