Example usage for java.lang Math ceil

List of usage examples for java.lang Math ceil

Introduction

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

Prototype

public static double ceil(double a) 

Source Link

Document

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

Usage

From source file:Main.java

public static boolean[] is_prime_up_to_with_sieve(int num) {
    boolean[] prime = new boolean[num + 1];
    Arrays.fill(prime, true);//from w  w w. j  av  a  2s  . c  o m
    prime[0] = false;
    prime[1] = false;
    int num_sqrt = (int) Math.ceil(Math.sqrt(num));

    for (int i = 2; i <= num_sqrt; i++) {
        if (prime[i])
            for (int j = i * i; j <= num; j += i)
                prime[j] = false;
    }
    return prime;
}

From source file:Main.java

/**
 *
 * @param paint/*from ww  w  .  j  a va 2 s .c o m*/
 * @param cellHeight
 * @return
 */
public static float getTextFitY(Paint paint, float cellHeight) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = (float) (Math.ceil(fm.descent - fm.ascent) + 2f);
    return (cellHeight + textHeight) / 2 - fm.bottom;
}

From source file:Main.java

public static int getFontHeight(TextView textView) {
    Paint paint = new Paint();
    paint.setTextSize(textView.getTextSize());
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.bottom - fm.top);
}

From source file:Main.java

/**
 * Returns the appropriate number of decimals to be used for the provided
 * number./*from w w w . j ava 2s.  co  m*/
 *
 * @param number
 * @return
 */
public static int getDecimals(float number) {

    float i = roundToNextSignificant(number);
    return (int) Math.ceil(-Math.log10(i)) + 2;
}

From source file:Main.java

/**
 * Rounds the given number to the given number of significant digits. Based on an answer on <a
 * href="http://stackoverflow.com/questions/202302">Stack Overflow</a>.
 *///w  w w.  ja  v a  2 s .c om
public static float roundToOneSignificantFigure(double num) {
    final float d = (float) Math.ceil((float) Math.log10(num < 0 ? -num : num));
    final int power = 1 - (int) d;
    final float magnitude = (float) Math.pow(10, power);
    final long shifted = Math.round(num * magnitude);
    return shifted / magnitude;
}

From source file:Main.java

public static Bitmap scrambleImage(Bitmap image, int piecesPerLine) {
    int px = (int) Math.ceil(image.getWidth() / (double) piecesPerLine);
    int ph = (int) Math.ceil(image.getHeight() / (double) piecesPerLine);
    int count = piecesPerLine * piecesPerLine;
    int[] arr = shuffle(arrayRange(count));

    Config config = image.getConfig() == null ? Bitmap.Config.ARGB_8888 : image.getConfig();
    Bitmap destBmp = Bitmap.createBitmap(image.getWidth(), image.getHeight(), config);
    Canvas canvas = new Canvas(destBmp);

    for (int i = 0; i < piecesPerLine; i++) {
        for (int j = 0; j < piecesPerLine; j++) {
            int n = arr[i * piecesPerLine + j];
            int row = n / piecesPerLine;
            int col = n % piecesPerLine;

            Rect fromRect = new Rect(row * px, col * ph, row * px + px, col * ph + ph);
            Rect toRect = new Rect(j * px, i * ph, j * px + px, i * ph + ph);

            canvas.drawBitmap(image, fromRect, toRect, null);
        }/*from  w  w w .  j  a va  2 s.  c  om*/
    }

    return destBmp;
}

From source file:Main.java

/**
 * Performs a division and rounds upwards to the next integer.
 *
 * @param numerator   the numerator.//from www .jav a 2  s. c o  m
 * @param denominator the denominator.
 * @return an integer value.
 */
public static int divideToCeil(int numerator, int denominator) {
    Double result = Math.ceil((double) numerator / denominator);

    return result.intValue();
}

From source file:Main.java

/**
 * downsamples the data by the given factor
 *
 * @param factor downsample factor//from  w w w .  j a  va  2s. c o m
 * @param data the data
 */
public static double[] downsample(int factor, double[] data) {
    double[] downsampledData = new double[(int) Math.ceil(data.length / factor)];
    int counter = 0;
    for (int i = 0; i < data.length; i += factor) {
        downsampledData[counter] = data[i];
        counter++;
    }
    return downsampledData;
}

From source file:Main.java

private static int getFontHeight(TextView tv) {
    Paint paint = new Paint();
    paint.setTextSize(tv.getTextSize());
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.bottom - fm.top);
}

From source file:Main.java

public static int roll() {
    return (int) Math.ceil(Math.random() * 6);
}