Example usage for java.lang Math abs

List of usage examples for java.lang Math abs

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double abs(double a) 

Source Link

Document

Returns the absolute value of a double value.

Usage

From source file:Main.java

/**
 * Checks if a given location is a valid (i.e. physically possible) location
 * on Earth. Note: The special separator locations (which have latitude =
 * 100) will not qualify as valid. Neither will locations with lat=0 and lng=0
 * as these are most likely "bad" measurements which often cause trouble.
 * //from w w  w.  ja v  a 2s.  co m
 * @param location the location to test
 * @return true if the location is a valid location.
 */
public static boolean isValidLocation(Location location) {
    return location != null && Math.abs(location.getLatitude()) <= 90
            && Math.abs(location.getLongitude()) <= 180;
}

From source file:com.qwazr.utils.HashUtils.java

public static final int getMurmur3Mod(final String hashString, Charset charset, final int mod) {
    HashFunction m3 = Hashing.murmur3_128();
    if (charset == null)
        charset = Charset.defaultCharset();
    return (Math.abs(m3.hashString(hashString, charset).asInt()) % mod);
}

From source file:Main.java

public static int convertDoubleToPluralInteger(double value) {
    double absoluteValue = Math.abs(value);
    if (absoluteValue > 2.5) {
        return (int) Math.round(absoluteValue);
    } else {/*w  w  w .  j av a  2s  . c o m*/
        if (absoluteValue == 0.0 || absoluteValue == 1.0 || absoluteValue == 2.0) {
            return (int) absoluteValue;
        } else {
            // Random Number to get into the "other" keyword for values like 0.99 or 2.001 seconds or degrees
            // in hopefully all possible languages
            return TRANSLATION_PLURAL_OTHER_INTEGER;
        }
    }
}

From source file:Main.java

static String applyHash(String string) {
    if (string == null)
        return null;

    double[] hashedStrings = new double[HASH_CONSTANTS.length];
    for (int i = 0; i < hashedStrings.length; i++) {
        hashedStrings[i] = applyUserHash(HASH_CONSTANTS[i], string);
    }//  ww  w . j  a va  2  s.  c o  m

    double product = 1;
    for (double hashedString : hashedStrings) {
        product *= hashedString;
    }

    return Long.toHexString(Math.round(Math.abs(product) / 65536d));
}

From source file:Main.java

/**
 * Generates a random word.//from  www .ja v a2  s.c  o  m
 */

@Nonnull
public static String generateWord(@Nonnull final Random random, @Nonnull final int[] codePointSet) {
    final StringBuilder builder = new StringBuilder();
    // 8 * 4 = 32 chars max, but we do it the following way so as to bias the random toward
    // longer words. This should be closer to natural language, and more importantly, it will
    // exercise the algorithms in dicttool much more.
    final int count = 1 + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5)
            + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5)
            + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5)
            + (Math.abs(random.nextInt()) % 5) + (Math.abs(random.nextInt()) % 5);
    while (builder.length() < count) {
        builder.appendCodePoint(codePointSet[Math.abs(random.nextInt()) % codePointSet.length]);
    }
    return builder.toString();
}

From source file:geo.cic.ipn.mx.accelerometer_sensor.RestApi.java

public static String obtenerClase(Float x, Float y, Float z, Integer k) {
    String clase = "";
    try {/*from w ww  .j  a  va  2s  . c  o m*/
        // TODO code application logic here

        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/octet-stream");
        Request request = new Request.Builder()
                .url(URL + "ObtenerClase.php?x=" + Math.abs(x * 1000) + "&y=" + Math.abs(y * 1000) + "&z="
                        + Math.abs(z * 1000) + "&k=" + k)
                .get().addHeader("cache-control", "no-cache")
                .addHeader("postman-token", "b983b2f6-8cd7-5956-32f5-bc7cf4e53b9f").build();

        JSONObject jsonObject = new RequestApi().execute(request).get();

        Integer estatus = jsonObject.getInt("estado");

        if (estatus == 1) { // exito
            clase = jsonObject.getString("clase");

        }

    } catch (JSONException ex) {
        ex.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        clase = "-1";
    }
    return clase;
}

From source file:Main.java

/**
 * The popup width is determined by the minimum width excluding content
 * (usually the width of the pointy mark and padding), the maximum width
 * (usually the width of the screen), a desired width (including content),
 * the width of the anchor and the position along the anchor at which the
 * pointy mark should point. Normally the popup takes the desired width or
 * the maximum width, unless the target arrow would otherwise be positioned
 * outside of the popup, in which case the popup is expanded to include the
 * pointy mark.//ww w . j  a v  a  2 s.  c  om
        
 * @param minWidth - the minimum width of the popup (not including content)
 * @param maxWidth - the maximum width of the popup
 * @param desiredWidth - the desired with of the popup (including content)
 * @param anchorWidth - the width of the anchor view
 * @param target - the position on the anchor at which the pointy mark should point
 */
public static int getPopupWidth(int minWidth, int maxWidth, int desiredWidth, int anchorWidth, float target) {
    // Minimum width as a percentage of the anchor width
    final double width = 2 * Math.abs(0.5 - target);

    // Minimum width as an absolute value
    int popupWidth = (int) (anchorWidth * width) + minWidth;

    // Make sure not smaller than the minimum width
    popupWidth = popupWidth > desiredWidth ? popupWidth : desiredWidth;

    // Make sure not larger than the max width
    return popupWidth > maxWidth ? maxWidth : popupWidth;
}

From source file:Main.java

/**
 * Calculates the constrast between two colors, using the algorithm provided by the WCAG v2.
 *///from   ww w  .j a v  a2s. c  o  m
public static float computeContrastBetweenColors(int bg, int fg) {
    float bgR = Color.red(bg) / 255f;
    float bgG = Color.green(bg) / 255f;
    float bgB = Color.blue(bg) / 255f;
    bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
    bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
    bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
    float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;

    float fgR = Color.red(fg) / 255f;
    float fgG = Color.green(fg) / 255f;
    float fgB = Color.blue(fg) / 255f;
    fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
    fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
    fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
    float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;

    return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
}

From source file:GraphicsUtil.java

public static void drawOptimizedLine(Graphics g, int x1, int y1, int x2, int y2) {
    if (g.getColor().getAlpha() < 255 && (x1 == x2 || y1 == y2))
        g.fillRect(x1 < x2 ? x1 : x2, y1 < y2 ? y1 : y2, Math.abs(x2 - x1) + 1, Math.abs(y2 - y1) + 1);
    else/*w  w  w  .  j  av a2s .co m*/
        g.drawLine(x1, y1, x2, y2);
}

From source file:Main.java

public static int[] closetFramerate(Camera.Parameters parameters, float frameRate) {
    int framerate = (int) (frameRate * 1000);
    List<int[]> rates = parameters.getSupportedPreviewFpsRange();
    int[] bestFramerate = rates.get(0);
    for (int i = 0; i < rates.size(); i++) {
        int[] rate = rates.get(i);
        Log.e(TAG, "supported preview pfs min " + rate[0] + " max " + rate[1]);
        int curDelta = Math.abs(rate[1] - framerate);
        int bestDelta = Math.abs(bestFramerate[1] - framerate);
        if (curDelta < bestDelta) {
            bestFramerate = rate;/*  w  w w  .  j a  v a2s .com*/
        } else if (curDelta == bestDelta) {
            bestFramerate = bestFramerate[0] < rate[0] ? rate : bestFramerate;
        }
    }
    return bestFramerate;
}