    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.lang3.math;
    018
    019    /**
    020     * <p>Provides IEEE-754r variants of NumberUtils methods. </p>
    021     *
    022     * <p>See: <a href="http://en.wikipedia.org/wiki/IEEE_754r">http://en.wikipedia.org/wiki/IEEE_754r</a></p>
    023     *
    024     * @since 2.4
    025     * @author Apache Software Foundation
    026     * @version $Id: IEEE754rUtils.java 889215 2009-12-10 11:56:38Z bayard $
    027     */
    028    public class IEEE754rUtils {
    029
    030         /**
    031         * <p>Returns the minimum value in an array.</p>
    032         *
    033         * @param array  an array, must not be null or empty
    034         * @return the minimum value in the array
    035         * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
    036         * @throws IllegalArgumentException if <code>array</code> is empty
    037         */
    038        public static double min(double[] array) {
    039            // Validates input
    040            if (array == null) {
    041                throw new IllegalArgumentException("The Array must not be null");
    042            } else if (array.length == 0) {
    043                throw new IllegalArgumentException("Array cannot be empty.");
    044            }
    045
    046            // Finds and returns min
    047            double min = array[0];
    048            for (int i = 1; i < array.length; i++) {
    049                min = min(array[i], min);
    050            }
    051
    052            return min;
    053        }
    054
    055        /**
    056         * <p>Returns the minimum value in an array.</p>
    057         *
    058         * @param array  an array, must not be null or empty
    059         * @return the minimum value in the array
    060         * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
    061         * @throws IllegalArgumentException if <code>array</code> is empty
    062         */
    063        public static float min(float[] array) {
    064            // Validates input
    065            if (array == null) {
    066                throw new IllegalArgumentException("The Array must not be null");
    067            } else if (array.length == 0) {
    068                throw new IllegalArgumentException("Array cannot be empty.");
    069            }
    070
    071            // Finds and returns min
    072            float min = array[0];
    073            for (int i = 1; i < array.length; i++) {
    074                min = min(array[i], min);
    075            }
    076
    077            return min;
    078        }
    079
    080        /**
    081         * <p>Gets the minimum of three <code>double</code> values.</p>
    082         *
    083         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    084         *
    085         * @param a  value 1
    086         * @param b  value 2
    087         * @param c  value 3
    088         * @return  the smallest of the values
    089         */
    090        public static double min(double a, double b, double c) {
    091            return min(min(a, b), c);
    092        }
    093
    094        /**
    095         * <p>Gets the minimum of two <code>double</code> values.</p>
    096         *
    097         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    098         *
    099         * @param a  value 1
    100         * @param b  value 2
    101         * @return  the smallest of the values
    102         */
    103        public static double min(double a, double b) {
    104            if(Double.isNaN(a)) {
    105                return b;
    106            } else
    107            if(Double.isNaN(b)) {
    108                return a;
    109            } else {
    110                return Math.min(a, b);
    111            }
    112        }
    113
    114        /**
    115         * <p>Gets the minimum of three <code>float</code> values.</p>
    116         *
    117         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    118         *
    119         * @param a  value 1
    120         * @param b  value 2
    121         * @param c  value 3
    122         * @return  the smallest of the values
    123         */
    124        public static float min(float a, float b, float c) {
    125            return min(min(a, b), c);
    126        }
    127
    128        /**
    129         * <p>Gets the minimum of two <code>float</code> values.</p>
    130         *
    131         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    132         *
    133         * @param a  value 1
    134         * @param b  value 2
    135         * @return  the smallest of the values
    136         */
    137        public static float min(float a, float b) {
    138            if(Float.isNaN(a)) {
    139                return b;
    140            } else
    141            if(Float.isNaN(b)) {
    142                return a;
    143            } else {
    144                return Math.min(a, b);
    145            }
    146        }
    147
    148        /**
    149         * <p>Returns the maximum value in an array.</p>
    150         *
    151         * @param array  an array, must not be null or empty
    152         * @return the minimum value in the array
    153         * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
    154         * @throws IllegalArgumentException if <code>array</code> is empty
    155         */
    156        public static double max(double[] array) {
    157            // Validates input
    158            if (array== null) {
    159                throw new IllegalArgumentException("The Array must not be null");
    160            } else if (array.length == 0) {
    161                throw new IllegalArgumentException("Array cannot be empty.");
    162            }
    163
    164            // Finds and returns max
    165            double max = array[0];
    166            for (int j = 1; j < array.length; j++) {
    167                max = max(array[j], max);
    168            }
    169
    170            return max;
    171        }
    172
    173        /**
    174         * <p>Returns the maximum value in an array.</p>
    175         *
    176         * @param array  an array, must not be null or empty
    177         * @return the minimum value in the array
    178         * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
    179         * @throws IllegalArgumentException if <code>array</code> is empty
    180         */
    181        public static float max(float[] array) {
    182            // Validates input
    183            if (array == null) {
    184                throw new IllegalArgumentException("The Array must not be null");
    185            } else if (array.length == 0) {
    186                throw new IllegalArgumentException("Array cannot be empty.");
    187            }
    188
    189            // Finds and returns max
    190            float max = array[0];
    191            for (int j = 1; j < array.length; j++) {
    192                max = max(array[j], max);
    193            }
    194
    195            return max;
    196        }
    197
    198        /**
    199         * <p>Gets the maximum of three <code>double</code> values.</p>
    200         *
    201         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    202         *
    203         * @param a  value 1
    204         * @param b  value 2
    205         * @param c  value 3
    206         * @return  the largest of the values
    207         */
    208        public static double max(double a, double b, double c) {
    209            return max(max(a, b), c);
    210        }
    211
    212        /**
    213         * <p>Gets the maximum of two <code>double</code> values.</p>
    214         *
    215         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    216         *
    217         * @param a  value 1
    218         * @param b  value 2
    219         * @return  the largest of the values
    220         */
    221        public static double max(double a, double b) {
    222            if(Double.isNaN(a)) {
    223                return b;
    224            } else
    225            if(Double.isNaN(b)) {
    226                return a;
    227            } else {
    228                return Math.max(a, b);
    229            }
    230        }
    231
    232        /**
    233         * <p>Gets the maximum of three <code>float</code> values.</p>
    234         *
    235         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    236         *
    237         * @param a  value 1
    238         * @param b  value 2
    239         * @param c  value 3
    240         * @return  the largest of the values
    241         */
    242        public static float max(float a, float b, float c) {
    243            return max(max(a, b), c);
    244        }
    245
    246        /**
    247         * <p>Gets the maximum of two <code>float</code> values.</p>
    248         *
    249         * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
    250         *
    251         * @param a  value 1
    252         * @param b  value 2
    253         * @return  the largest of the values
    254         */
    255        public static float max(float a, float b) {
    256            if(Float.isNaN(a)) {
    257                return b;
    258            } else
    259            if(Float.isNaN(b)) {
    260                return a;
    261            } else {
    262                return Math.max(a, b);
    263            }
    264        }
    265
    266    }