Java List Mean mean(List vals)

Here you can find the source of mean(List vals)

Description

mean

License

Open Source License

Declaration

public static double mean(List<Double> vals) 

Method Source Code

//package com.java2s;
/*//  www  .ja v  a  2  s  .c o m
 * AACalc - Asset Allocation Calculator
 * Copyright (C) 2009, 2011-2016 Gordon Irlam
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    public static double mean(List<Double> vals) {
        return sum(vals) / vals.size();
    }

    public static double mean(double... a) {
        return sum(a) / a.length;
    }

    public static double sum(double... vals) {
        double x = 0.0;
        double floor = (vals.length == 0 || Double.isInfinite(vals[0]) ? 0 : vals[0]); // Avoid loss of precision summing many large nearby values.
        for (Double val : vals) {
            x += val - floor;
        }
        return vals.length * floor + x;
    }

    public static double sum(List<Double> vals) {
        double x = 0.0;
        double floor = (vals.size() == 0 || Double.isInfinite(vals.get(0)) ? 0 : vals.get(0)); // Avoid loss of precision summing many large nearby values.
        for (double val : vals) {
            x += val - floor;
        }
        return vals.size() * floor + x;
    }
}

Related

  1. mean(List data)
  2. mean(List nums, int start, int size)
  3. mean(List data)
  4. mean(List list)
  5. mean(List list)
  6. mean(List values)
  7. mean(List values)
  8. mean(List values)
  9. mean(List numbers)