Java mean meanSml(final int a, final int b)

Here you can find the source of meanSml(final int a, final int b)

Description

mean Sml

License

Open Source License

Return

Mean without overflow, rounded to the value of smallest magnitude (i.e. mathematical (a+b)/2, using integer division).

Declaration

public static int meanSml(final int a, final int b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w ww  .  j av  a 2  s  .  c  o m*/
     * @return Mean without overflow, rounded to the value of smallest magnitude (i.e. mathematical (a+b)/2, using
     *         integer division).
     */
    public static int meanSml(final int a, final int b) {
        int result = meanLow(a, b);
        if (!haveSameEvenness(a, b)) {
            // inexact
            if ((a & b) < 0 || (a | b) < 0 && a + b < 0) {
                // both < 0, or only one is < 0 and it has the largest magnitude
                result++;
            }
        }
        return result;
    }

    /**
     * @return Mean without overflow, rounded to the value of smallest magnitude (i.e. mathematical (a+b)/2, using
     *         integer division).
     */
    public static long meanSml(final long a, final long b) {
        long result = meanLow(a, b);
        if (!haveSameEvenness(a, b)) {
            // inexact
            if ((a & b) < 0 || (a | b) < 0 && a + b < 0) {
                // both < 0, or only one is < 0 and it has the largest magnitude
                result++;
            }
        }
        return result;
    }

    /**
     * @return Mean without overflow, rounded to the lowest value (i.e. mathematical floor((a+b)/2), using floating
     *         point division).
     */
    public static int meanLow(final int a, final int b) {
        return (a & b) + ((a ^ b) >> 1);
    }

    /**
     * @return Mean without overflow, rounded to the lowest value (i.e. mathematical floor((a+b)/2), using floating
     *         point division).
     */
    public static long meanLow(final long a, final long b) {
        return (a & b) + ((a ^ b) >> 1);
    }

    /**
     * @return True if the specified values are both even or both odd, false otherwise.
     */
    public static boolean haveSameEvenness(final int a, final int b) {
        return ((a ^ b) & 1) == 0;
    }

    /**
     * @return True if the specified values are both even or both odd, false otherwise.
     */
    public static boolean haveSameEvenness(final long a, final long b) {
        // faster to work on ints
        return haveSameEvenness((int) a, (int) b);
    }
}

Related

  1. meanGreenwichSideralTime(double t)
  2. meanImage(float[][]... images)
  3. meanLow(final int a, final int b)
  4. means(double[][] input)
  5. meanSlow(final double[] values)
  6. meanSquare(float[] a, int off, int length)
  7. meanSquaredError(double[] x, double[] y)
  8. meanSquaredError(double[][] vectorBatch1, double[][] vectorBatch2)
  9. meanWithoutZeros(int[] in, int x1, int x2)