Java Number Max Value max(double a, double b)

Here you can find the source of max(double a, double b)

Description

Binary max, without handling of special values.

License

Open Source License

Parameter

Parameter Description
a First value
b Second value

Return

Maximum

Declaration

public static double max(double a, double b) 

Method Source Code

//package com.java2s;
/*//from www .j a va 2  s .  co  m
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures
    
 Copyright (C) 2015
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team
    
 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/>.
 */

public class Main {
    /**
     * Binary max, <i>without</i> handling of special values.
     *
     * Because of the lack of special case handling, this is faster than
     * {@link Math#max}. But usually, it should be written inline as
     * {@code (a >= b) ? a : b}
     *
     * The result is asymmetric in case of {@code Double.NaN}:<br />
     * {@code MathUtil.max(Double.NaN, 1.)} is 1, but <br />
     * {@code MathUtil.max(1., Double.NaN)} is {@code Double.NaN}.
     *
     * @param a First value
     * @param b Second value
     * @return Maximum
     */
    public static double max(double a, double b) {
        return a >= b ? a : b;
    }

    /**
     * Ternary max, <i>without</i> handling of special values.
     *
     * Because of the lack of special case handling, this is faster than
     * {@link Math#max}. But usually, it should be written inline.
     *
     * @param a First value
     * @param b Second value
     * @param c Third value
     * @return Maximum
     */
    public static double max(double a, double b, double c) {
        return a >= b ? (a >= c ? a : c) : (b >= c ? b : c);
    }

    /**
     * Quadrary max, <i>without</i> handling of special values.
     *
     * Because of the lack of special case handling, this is faster than
     * {@link Math#max}. But usually, it should be written inline.
     *
     * @param a First value
     * @param b Second value
     * @param c Third value
     * @param d Fourth value
     * @return Maximum
     */
    public static double max(double a, double b, double c, double d) {
        return a >= b ? //
                a >= c ? (a >= d ? a : d) : (c >= d ? c : d) //
                : //
                b >= c ? (b >= d ? b : d) : (c >= d ? c : d);
    }

    /**
     * Binary max. If possible, inline.
     *
     * @param a First value
     * @param b Second value
     * @return Maximum
     */
    public static int max(int a, int b) {
        return a >= b ? a : b;
    }

    /**
     * Ternary max. If possible, inline.
     *
     * @param a First value
     * @param b Second value
     * @param c Third value
     * @return Maximum
     */
    public static int max(int a, int b, int c) {
        return a >= b ? (a >= c ? a : c) : (b >= c ? b : c);
    }

    /**
     * Quadrary max, <i>without</i> handling of special values.
     *
     * Because of the lack of special case handling, this is faster than
     * {@link Math#max}. But usually, it should be written inline.
     *
     * @param a First value
     * @param b Second value
     * @param c Third value
     * @param d Fourth value
     * @return Maximum
     */
    public static int max(int a, int b, int c, int d) {
        return a >= b ? //
                a >= c ? (a >= d ? a : d) : (c >= d ? c : d) //
                : //
                b >= c ? (b >= d ? b : d) : (c >= d ? c : d);
    }
}

Related

  1. max(byte value, byte max)
  2. max(byte value1, byte value2)
  3. max(double a, double b)
  4. max(double a, double b)
  5. max(double a, double b, double c, double d)
  6. max(double a, double b, double c, double d)
  7. max(double d1, double d2)