Java Array Max Value max(double[] x)

Here you can find the source of max(double[] x)

Description

Returns the highest element of `x`, NaN values are ignored.

License

Open Source License

Exception

Parameter Description
NullPointerException if `x` is null.

Declaration

public static double max(double[] x) 

Method Source Code

//package com.java2s;
/*//from   ww w  . ja  va2  s  .  com
 *  Copyright (C) 2013, Peter Decsi.
 * 
 *  This library is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation, either 
 *  version 3 of the License, or (at your option) any later version.
 * 
 *  This library 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 Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns the highest element of `x`, NaN values are ignored.
     * 
     * @throws NullPointerException if `x` is null.
     */
    public static double max(double[] x) {
        return max(x, true);
    }

    /**
     * Returns the highest element of `x`. If `ignoreNaN` is false, 
     * and `x` contains a NaN value, then the result is NaN.
     * 
     * @throws NullPointerException if `x` is null.
     */
    public static double max(double[] x, boolean ignoreNaN) {
        int size = x.length;
        if (size == 0)
            return Double.NaN;

        double max = x[0];
        for (int i = 1; i < size; i++) {
            double v = x[i];
            if (Double.isNaN(v)) {
                if (!ignoreNaN)
                    return Double.NaN;
            } else {
                if (v > max)
                    max = v;
            }
        }
        return max;
    }
}

Related

  1. max(double[] matrix)
  2. max(double[] series)
  3. max(double[] vals)
  4. max(double[] values)
  5. max(double[] values)
  6. max(final double aval, double[] b, final int bi, final int len)
  7. max(final double... doubles)
  8. max(final double[] array)
  9. Max(final double[] input)