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 agents.org.apache.commons.lang.math;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * <p>Provides IEEE-754r variants of NumberUtils methods. </p>
|
---|
21 | *
|
---|
22 | * <p>See: <a href="http://en.wikipedia.org/wiki/IEEE_754r">http://en.wikipedia.org/wiki/IEEE_754r</a></p>
|
---|
23 | *
|
---|
24 | * @since 2.4
|
---|
25 | * @author Apache Software Foundation
|
---|
26 | * @version $Id: IEEE754rUtils.java 905636 2010-02-02 14:03:32Z niallp $
|
---|
27 | */
|
---|
28 | public class IEEE754rUtils {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>Returns the minimum value in an array.</p>
|
---|
32 | *
|
---|
33 | * @param array an array, must not be null or empty
|
---|
34 | * @return the minimum value in the array
|
---|
35 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
|
---|
36 | * @throws IllegalArgumentException if <code>array</code> is empty
|
---|
37 | */
|
---|
38 | public static double min(double[] array) {
|
---|
39 | // Validates input
|
---|
40 | if (array == null) {
|
---|
41 | throw new IllegalArgumentException("The Array must not be null");
|
---|
42 | } else if (array.length == 0) {
|
---|
43 | throw new IllegalArgumentException("Array cannot be empty.");
|
---|
44 | }
|
---|
45 |
|
---|
46 | // Finds and returns min
|
---|
47 | double min = array[0];
|
---|
48 | for (int i = 1; i < array.length; i++) {
|
---|
49 | min = min(array[i], min);
|
---|
50 | }
|
---|
51 |
|
---|
52 | return min;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * <p>Returns the minimum value in an array.</p>
|
---|
57 | *
|
---|
58 | * @param array an array, must not be null or empty
|
---|
59 | * @return the minimum value in the array
|
---|
60 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
|
---|
61 | * @throws IllegalArgumentException if <code>array</code> is empty
|
---|
62 | */
|
---|
63 | public static float min(float[] array) {
|
---|
64 | // Validates input
|
---|
65 | if (array == null) {
|
---|
66 | throw new IllegalArgumentException("The Array must not be null");
|
---|
67 | } else if (array.length == 0) {
|
---|
68 | throw new IllegalArgumentException("Array cannot be empty.");
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Finds and returns min
|
---|
72 | float min = array[0];
|
---|
73 | for (int i = 1; i < array.length; i++) {
|
---|
74 | min = min(array[i], min);
|
---|
75 | }
|
---|
76 |
|
---|
77 | return min;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * <p>Gets the minimum of three <code>double</code> values.</p>
|
---|
82 | *
|
---|
83 | * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
|
---|
84 | *
|
---|
85 | * @param a value 1
|
---|
86 | * @param b value 2
|
---|
87 | * @param c value 3
|
---|
88 | * @return the smallest of the values
|
---|
89 | */
|
---|
90 | public static double min(double a, double b, double c) {
|
---|
91 | return min(min(a, b), c);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * <p>Gets the minimum of two <code>double</code> values.</p>
|
---|
96 | *
|
---|
97 | * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p>
|
---|
98 | *
|
---|
99 | * @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 | }
|
---|