Java List Sum sum(List vals)

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

Description

sum

License

Open Source License

Declaration

public static double sum(List<Double> vals) 

Method Source Code

//package com.java2s;
/*//  w w w .j a  v  a 2  s . c om
 * 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 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. sum(int[] list)
  2. sum(List list)
  3. sum(List a)
  4. sum(List data)
  5. sum(List scores)
  6. sum(List l)
  7. sum(List list)
  8. sum(List lst)
  9. sum(List numbers)