Java Number Sum sum(double start, double end, double step)

Here you can find the source of sum(double start, double end, double step)

Description

sum

License

Mozilla Public License

Declaration

public static double sum(double start, double end, double step) 

Method Source Code

//package com.java2s;
/** This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *///w  ww.  j  a v a  2 s.c  o  m

public class Main {
    public static double sum(double start, double end, double step) {
        if (start > end) {
            throw new IllegalArgumentException("Lower limit > upper limit!");
        } else if (start + step > end) {
            throw new IllegalArgumentException("Lower limit + step > upper limit!");
        } else if (step <= 0) {
            throw new IllegalArgumentException("Step = 0!");
        } else if (start == end) {
            return start;
        } else {
            double sum = 0;
            for (double i = start; i <= end; i += step) {
                sum += i;
            }
            return sum;
        }
    }
}

Related

  1. sum(double a, double b)
  2. sum(double x, double y)
  3. sum(double x, double y)
  4. sum(final Iterable values)
  5. sum(final Iterable ns)