Example usage for java.lang Math round

List of usage examples for java.lang Math round

Introduction

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

Prototype

public static long round(double a) 

Source Link

Document

Returns the closest long to the argument, with ties rounding to positive infinity.

Usage

From source file:Main.java

public static Bitmap loadMpoBitmapFromFile(File file, long offset, int maxWidth, int maxHeight)
        throws IOException {
    // First, decode the width and height of the image, so that we know how much to scale
    // it down when loading it into our ImageView (so we don't need to do a huge allocation).
    BitmapFactory.Options opts = new BitmapFactory.Options();
    InputStream fs = null;//from w ww . j a va2 s.c o m
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(fs, null, opts);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    int scale = 1;
    if (opts.outHeight > maxHeight || opts.outWidth > maxWidth) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(maxWidth / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5)));
    }
    if ((opts.outHeight <= 0) || (opts.outWidth <= 0)) {
        return null;
    }
    // Decode the image for real, but now with a sampleSize.
    // We have to reopen the file stream, and re-skip to the correct offset, since
    // FileInputStream doesn't support reset().
    Bitmap bmp = null;
    fs = null;
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        BitmapFactory.Options opts2 = new BitmapFactory.Options();
        opts2.inSampleSize = scale;
        bmp = BitmapFactory.decodeStream(fs, null, opts2);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    return bmp;
}

From source file:edu.byu.nlp.math.GammaFunctions.java

/**
 * Computes ln Gamma(numerator) - ln Gamma(denominator).
 *//* www .j  av a 2s. c  o  m*/
public static double logRatioOfGammas(double numerator, double denominator) {
    // There are potential speed ups when the difference is integral
    double diff = numerator - denominator;
    if (Math2.isIntegral(diff, IS_INTEGER_EPS)) {
        // When n = numerator - denominator is an integer, then 
        // Gamma(numerator) / Gamma(denominator) = x * (x + 1) * ... * (x + n - 1)
        // Which is, by definition the rising factorial of x^(n)
        return logRisingFactorial(denominator, (int) (Math.round(diff)));
    }
    return Gamma.logGamma(numerator) - Gamma.logGamma(denominator);
}

From source file:com.tinyhydra.botd.GoogleOperations.java

public static List<JavaShop> GetShops(Handler handler, Location currentLocation, String placesApiKey) {
    // Use google places to get all the shops within '500' (I believe meters is the default measurement they use)
    // make a list of JavaShops and pass it to the ListView adapter
    List<JavaShop> shopList = new ArrayList<JavaShop>();
    try {//from  w  w w .j a v a 2 s .co  m
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        int accuracy = Math.round(currentLocation.getAccuracy());
        if (accuracy < 500)
            accuracy = 500;
        request.setURI(URI.create("https://maps.googleapis.com/maps/api/place/search/json?location="
                + currentLocation.getLatitude() + "," + currentLocation.getLongitude() + "&radius=" + accuracy
                + "&types=" + URLEncoder.encode("cafe|restaurant|food", "UTF-8")
                + "&keyword=coffee&sensor=true&key=" + placesApiKey));
        HttpResponse response = client.execute(request);
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }

        JSONObject predictions = new JSONObject(sb.toString());
        // Google passes back a status string. if we screw up, it won't say "OK". Alert the user.
        String jstatus = predictions.getString("status");
        if (jstatus.equals("ZERO_RESULTS")) {
            Utils.PostToastMessageToHandler(handler, "No shops found in your area.", Toast.LENGTH_SHORT);
            return shopList;
        } else if (!jstatus.equals("OK")) {
            Utils.PostToastMessageToHandler(handler, "Error retrieving local shops.", Toast.LENGTH_SHORT);
            return shopList;
        }

        // This section may fail if there's no results, but we'll just display an empty list.
        //TODO: alert the user and cancel the dialog if this fails
        JSONArray ja = new JSONArray(predictions.getString("results"));

        for (int i = 0; i < ja.length(); i++) {
            JSONObject jo = (JSONObject) ja.get(i);
            shopList.add(new JavaShop(jo.getString("name"), jo.getString("id"), "", jo.getString("reference"),
                    jo.getString("vicinity")));
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return shopList;
}

From source file:Main.java

public static int extendDimension(int x) {
    if (x < 1)
        throw new IllegalArgumentException("x must be greater or equal 1");
    int nextExp = nextExp2(x);
    int nextPow = nextExp + 1;
    int extDim = (int) Math.round(Math.pow(2.0, (double) nextPow));
    return extDim;
}

From source file:Timer.java

/**
 * @param duration//from  w  w w. jav a2s  . co  m
 *            The amount of time, in milliseconds, that this timer will last
 *            for.
 */
public Timer(long duration, double percentDone) {
    if (duration <= 0) {
        throw new IllegalArgumentException("The duration cannot be less or equal to 0, given: " + duration);
    } else if (percentDone < 0) {
        throw new IllegalArgumentException(
                "The percentDone cannot be less or equal to 0, given: " + percentDone);
    }

    this.duration = duration;
    this.startTime = System.currentTimeMillis() - Math.round(duration * percentDone);
    this.endTime = startTime + duration;
}

From source file:org.example.fis.MyTransformer.java

public String transform() {
    // let's return a random string
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 3; i++) {
        int number = (int) (Math.round(Math.random() * 1000) % 10);
        char letter = (char) ('0' + number);
        buffer.append(letter);/*from   w w w.j a va  2s  .co  m*/
    }
    return buffer.toString();
}

From source file:com.cds.pcrj.consultaMoeda.MyTransformer.java

public String transform() {
    // lets return a random string
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 3; i++) {
        int number = (int) (Math.round(Math.random() * 1000) % 10);
        char letter = (char) ('0' + number);
        buffer.append(letter);//from w  ww  .  j  ava  2 s  .  com
    }
    return buffer.toString();
}

From source file:edu.purdue.cc.bionet.ui.CustomTickUnit.java

@Override
public String valueToString(double value) {
    if (Math.round(value) > units.size() - 1) {
        return "";
    }//from  w  w w .ja  v  a2  s  .c  om
    return this.units.get(Math.max(0, (int) Math.round(value))).toString();
}

From source file:Main.java

private static Rect getBitmapRectCenterInsideHelper(int bitmapWidth, int bitmapHeight, int viewWidth,
        int viewHeight) {
    double viewToBitmapWidthRatio = 1.0D / 0.0;
    double viewToBitmapHeightRatio = 1.0D / 0.0;
    if (viewWidth < bitmapWidth) {
        viewToBitmapWidthRatio = (double) viewWidth / (double) bitmapWidth;
    }/*from  w ww.  ja  va2s .c  o  m*/

    if (viewHeight < bitmapHeight) {
        viewToBitmapHeightRatio = (double) viewHeight / (double) bitmapHeight;
    }

    double resultWidth;
    double resultHeight;
    if (viewToBitmapWidthRatio == 1.0D / 0.0 && viewToBitmapHeightRatio == 1.0D / 0.0) {
        resultHeight = (double) bitmapHeight;
        resultWidth = (double) bitmapWidth;
    } else if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio) {
        resultWidth = (double) viewWidth;
        resultHeight = (double) bitmapHeight * resultWidth / (double) bitmapWidth;
    } else {
        resultHeight = (double) viewHeight;
        resultWidth = (double) bitmapWidth * resultHeight / (double) bitmapHeight;
    }

    int resultX;
    int resultY;
    if (resultWidth == (double) viewWidth) {
        resultX = 0;
        resultY = (int) Math.round(((double) viewHeight - resultHeight) / 2.0D);
    } else if (resultHeight == (double) viewHeight) {
        resultX = (int) Math.round(((double) viewWidth - resultWidth) / 2.0D);
        resultY = 0;
    } else {
        resultX = (int) Math.round(((double) viewWidth - resultWidth) / 2.0D);
        resultY = (int) Math.round(((double) viewHeight - resultHeight) / 2.0D);
    }

    Rect result = new Rect(resultX, resultY, resultX + (int) Math.ceil(resultWidth),
            resultY + (int) Math.ceil(resultHeight));
    return result;
}

From source file:Main.java

public static void smoothFillHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    Graphics2D g2D = (Graphics2D) g;
    Paint savedPaint = g2D.getPaint();
    int steps = colors.length;
    double dy = (double) h / (double) (steps - 1);
    int y1 = y;/*  w w  w.jav a  2 s  . c o m*/
    for (int i = 0; i < steps; i++) {
        int y2 = y + (int) Math.round((double) i * dy);
        if (i == (steps - 1)) {
            g2D.setPaint(null);
            g2D.setColor(colors[i]);
            g2D.fillRect(x, y1, w, y + h - y1);
        } else {
            g2D.setPaint(new GradientPaint(0, y1, colors[i], 0, y2, colors[i + 1]));
            g2D.fillRect(x, y1, w, y2 - y1);
        }
        y1 = y2;
    }
    g2D.setPaint(savedPaint);
}