Java Array Average avg(double values[], int size, boolean ignoreNan)

Here you can find the source of avg(double values[], int size, boolean ignoreNan)

Description

avg

License

Apache License

Declaration

public final static double avg(double values[], int size, boolean ignoreNan) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public final static double avg(double values[], int size, boolean ignoreNan) {
        if (values == null || size == 0) {
            return Double.NaN;
        }/*w  w  w. j a v  a2s .c  o m*/
        int cnt = 0;
        double sum = 0;
        for (int i = 0; i < size; i++) {
            double v = values[i];
            if (!Double.isNaN(v)) {
                cnt++;
                sum += v;
            } else if (!ignoreNan) {
                cnt++;
            }
        }
        return cnt == 0 ? Double.NaN : sum / cnt;
    }
}

Related

  1. averagePt(double[][] ndPoints)
  2. averageX(long[][] intImg, int width, int height, int i, int j, int dl, int dr)
  3. avg(byte[] values)
  4. avg(double a, double b)
  5. avg(double v1, double v2)
  6. avg(double... a)
  7. avg(double[] a)
  8. avg(double[] array)
  9. avg(double[] nums)