Java Array Average average(double... vals)

Here you can find the source of average(double... vals)

Description

If you think your values will overflow this operation, then roll your own

License

BSD License

Parameter

Parameter Description
vals a parameter

Declaration

public static double average(double... vals) 

Method Source Code

//package com.java2s;
/**//from  www . j a va2  s .  c om
 * <p>
 * Useful math functions. :)
 * </p>
 * <p>
 * <span class="BSDLicense"> This software is distributed under the <a
 * href="http://hci.stanford.edu/research/copyright.txt">BSD License</a>. </span>
 * </p>
 * 
 * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu)
 */

public class Main {
    /**
     * If you think your values will overflow this operation, then roll your own
     * 
     * @param vals
     * @return
     */
    public static double average(double... vals) {
        if (vals == null || vals.length == 0) {
            return 0;
        }
        return sum(vals) / vals.length;
    }

    /**
     * @param vals
     * @return
     */
    private static double sum(double... vals) {
        double sum = 0;
        for (double val : vals) {
            sum += val;
        }
        return sum;
    }
}

Related

  1. average(Double sum, Double size)
  2. average(double values[])
  3. average(double x, double y)
  4. average(double x1, double x2)
  5. average(double... args)
  6. average(double[] a)
  7. average(double[] a)
  8. average(Double[] arr)
  9. average(double[] array)