IEEE754rUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.math;

  18. import java.util.Objects;

  19. import org.apache.commons.lang3.Validate;

  20. /**
  21.  * Provides IEEE-754r variants of NumberUtils methods.
  22.  *
  23.  * <p>See: <a href="https://en.wikipedia.org/wiki/IEEE_754r">https://en.wikipedia.org/wiki/IEEE_754r</a></p>
  24.  *
  25.  * @since 2.4
  26.  */
  27. public class IEEE754rUtils {

  28.      /**
  29.      * Returns the maximum value in an array.
  30.      *
  31.      * @param array  an array, must not be null or empty
  32.      * @return the minimum value in the array
  33.      * @throws NullPointerException if {@code array} is {@code null}
  34.      * @throws IllegalArgumentException if {@code array} is empty
  35.      * @since 3.4 Changed signature from max(double[]) to max(double...)
  36.      */
  37.     public static double max(final double... array) {
  38.         Objects.requireNonNull(array, "array");
  39.         Validate.isTrue(array.length != 0, "Array cannot be empty.");

  40.         // Finds and returns max
  41.         double max = array[0];
  42.         for (int j = 1; j < array.length; j++) {
  43.             max = max(array[j], max);
  44.         }

  45.         return max;
  46.     }

  47.     /**
  48.      * Gets the maximum of two {@code double} values.
  49.      *
  50.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  51.      *
  52.      * @param a  value 1
  53.      * @param b  value 2
  54.      * @return  the largest of the values
  55.      */
  56.     public static double max(final double a, final double b) {
  57.         if (Double.isNaN(a)) {
  58.             return b;
  59.         }
  60.         if (Double.isNaN(b)) {
  61.             return a;
  62.         }
  63.         return Math.max(a, b);
  64.     }

  65.     /**
  66.      * Gets the maximum of three {@code double} values.
  67.      *
  68.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  69.      *
  70.      * @param a  value 1
  71.      * @param b  value 2
  72.      * @param c  value 3
  73.      * @return  the largest of the values
  74.      */
  75.     public static double max(final double a, final double b, final double c) {
  76.         return max(max(a, b), c);
  77.     }

  78.     /**
  79.      * Returns the maximum value in an array.
  80.      *
  81.      * @param array  an array, must not be null or empty
  82.      * @return the minimum value in the array
  83.      * @throws NullPointerException if {@code array} is {@code null}
  84.      * @throws IllegalArgumentException if {@code array} is empty
  85.      * @since 3.4 Changed signature from max(float[]) to max(float...)
  86.      */
  87.     public static float max(final float... array) {
  88.         Objects.requireNonNull(array, "array");
  89.         Validate.isTrue(array.length != 0, "Array cannot be empty.");

  90.         // Finds and returns max
  91.         float max = array[0];
  92.         for (int j = 1; j < array.length; j++) {
  93.             max = max(array[j], max);
  94.         }

  95.         return max;
  96.     }

  97.     /**
  98.      * Gets the maximum of two {@code float} values.
  99.      *
  100.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  101.      *
  102.      * @param a  value 1
  103.      * @param b  value 2
  104.      * @return  the largest of the values
  105.      */
  106.     public static float max(final float a, final float b) {
  107.         if (Float.isNaN(a)) {
  108.             return b;
  109.         }
  110.         if (Float.isNaN(b)) {
  111.             return a;
  112.         }
  113.         return Math.max(a, b);
  114.     }

  115.     /**
  116.      * Gets the maximum of three {@code float} values.
  117.      *
  118.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  119.      *
  120.      * @param a  value 1
  121.      * @param b  value 2
  122.      * @param c  value 3
  123.      * @return  the largest of the values
  124.      */
  125.     public static float max(final float a, final float b, final float c) {
  126.         return max(max(a, b), c);
  127.     }

  128.     /**
  129.      * Returns the minimum value in an array.
  130.      *
  131.      * @param array  an array, must not be null or empty
  132.      * @return the minimum value in the array
  133.      * @throws NullPointerException if {@code array} is {@code null}
  134.      * @throws IllegalArgumentException if {@code array} is empty
  135.      * @since 3.4 Changed signature from min(double[]) to min(double...)
  136.      */
  137.     public static double min(final double... array) {
  138.         Objects.requireNonNull(array, "array");
  139.         Validate.isTrue(array.length != 0, "Array cannot be empty.");

  140.         // Finds and returns min
  141.         double min = array[0];
  142.         for (int i = 1; i < array.length; i++) {
  143.             min = min(array[i], min);
  144.         }

  145.         return min;
  146.     }

  147.     /**
  148.      * Gets the minimum of two {@code double} values.
  149.      *
  150.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  151.      *
  152.      * @param a  value 1
  153.      * @param b  value 2
  154.      * @return  the smallest of the values
  155.      */
  156.     public static double min(final double a, final double b) {
  157.         if (Double.isNaN(a)) {
  158.             return b;
  159.         }
  160.         if (Double.isNaN(b)) {
  161.             return a;
  162.         }
  163.         return Math.min(a, b);
  164.     }

  165.     /**
  166.      * Gets the minimum of three {@code double} values.
  167.      *
  168.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  169.      *
  170.      * @param a  value 1
  171.      * @param b  value 2
  172.      * @param c  value 3
  173.      * @return  the smallest of the values
  174.      */
  175.     public static double min(final double a, final double b, final double c) {
  176.         return min(min(a, b), c);
  177.     }

  178.     /**
  179.      * Returns the minimum value in an array.
  180.      *
  181.      * @param array  an array, must not be null or empty
  182.      * @return the minimum value in the array
  183.      * @throws NullPointerException if {@code array} is {@code null}
  184.      * @throws IllegalArgumentException if {@code array} is empty
  185.      * @since 3.4 Changed signature from min(float[]) to min(float...)
  186.      */
  187.     public static float min(final float... array) {
  188.         Objects.requireNonNull(array, "array");
  189.         Validate.isTrue(array.length != 0, "Array cannot be empty.");

  190.         // Finds and returns min
  191.         float min = array[0];
  192.         for (int i = 1; i < array.length; i++) {
  193.             min = min(array[i], min);
  194.         }

  195.         return min;
  196.     }

  197.     /**
  198.      * Gets the minimum of two {@code float} values.
  199.      *
  200.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  201.      *
  202.      * @param a  value 1
  203.      * @param b  value 2
  204.      * @return  the smallest of the values
  205.      */
  206.     public static float min(final float a, final float b) {
  207.         if (Float.isNaN(a)) {
  208.             return b;
  209.         }
  210.         if (Float.isNaN(b)) {
  211.             return a;
  212.         }
  213.         return Math.min(a, b);
  214.     }

  215.     /**
  216.      * Gets the minimum of three {@code float} values.
  217.      *
  218.      * <p>NaN is only returned if all numbers are NaN as per IEEE-754r.</p>
  219.      *
  220.      * @param a  value 1
  221.      * @param b  value 2
  222.      * @param c  value 3
  223.      * @return  the smallest of the values
  224.      */
  225.     public static float min(final float a, final float b, final float c) {
  226.         return min(min(a, b), c);
  227.     }

  228.     /**
  229.      * Make private in 4.0.
  230.      *
  231.      * @deprecated TODO Make private in 4.0.
  232.      */
  233.     @Deprecated
  234.     public IEEE754rUtils() {
  235.         // empty
  236.     }
  237. }