View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.util;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.hipparchus.dfp.Dfp;
26  import org.hipparchus.dfp.DfpField;
27  import org.hipparchus.dfp.DfpMath;
28  import org.hipparchus.exception.LocalizedCoreFormats;
29  import org.hipparchus.exception.MathRuntimeException;
30  import org.hipparchus.random.MersenneTwister;
31  import org.hipparchus.random.RandomGenerator;
32  import org.hipparchus.random.Well1024a;
33  import org.hipparchus.random.Well19937a;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.Timeout;
38  
39  import java.lang.reflect.Method;
40  import java.lang.reflect.Modifier;
41  import java.lang.reflect.Type;
42  import java.math.BigDecimal;
43  import java.math.BigInteger;
44  import java.math.RoundingMode;
45  import java.util.concurrent.TimeUnit;
46  
47  import static org.junit.jupiter.api.Assertions.assertEquals;
48  import static org.junit.jupiter.api.Assertions.assertThrows;
49  import static org.junit.jupiter.api.Assertions.assertTrue;
50  import static org.junit.jupiter.api.Assertions.fail;
51  
52  class FastMathTest {
53  
54      private static final double MAX_ERROR_ULP = 0.51;
55      private static final int NUMBER_OF_TRIALS = 1000;
56  
57      private DfpField field;
58      private RandomGenerator generator;
59  
60      @BeforeEach
61      void setUp() {
62          field = new DfpField(40);
63          generator = new MersenneTwister(6176597458463500194L);
64      }
65  
66      @Test
67      void testMinMaxDouble() {
68          double[][] pairs = {
69              { -50.0, 50.0 },
70              {  Double.POSITIVE_INFINITY, 1.0 },
71              {  Double.NEGATIVE_INFINITY, 1.0 },
72              {  Double.NaN, 1.0 },
73              {  Double.POSITIVE_INFINITY, 0.0 },
74              {  Double.NEGATIVE_INFINITY, 0.0 },
75              {  Double.NaN, 0.0 },
76              {  Double.NaN, Double.NEGATIVE_INFINITY },
77              {  Double.NaN, Double.POSITIVE_INFINITY },
78              { Precision.SAFE_MIN, Precision.EPSILON }
79          };
80          for (double[] pair : pairs) {
81              assertEquals(Math.min(pair[0], pair[1]),
82                           FastMath.min(pair[0], pair[1]),
83                           Precision.EPSILON,
84                           "min(" + pair[0] + ", " + pair[1] + ")");
85              assertEquals(Math.min(pair[1], pair[0]),
86                           FastMath.min(pair[1], pair[0]),
87                           Precision.EPSILON,
88                           "min(" + pair[1] + ", " + pair[0] + ")");
89              assertEquals(Math.max(pair[0], pair[1]),
90                           FastMath.max(pair[0], pair[1]),
91                           Precision.EPSILON,
92                           "max(" + pair[0] + ", " + pair[1] + ")");
93              assertEquals(Math.max(pair[1], pair[0]),
94                           FastMath.max(pair[1], pair[0]),
95                           Precision.EPSILON,
96                           "max(" + pair[1] + ", " + pair[0] + ")");
97          }
98      }
99  
100     @Test
101     void testMinMaxField() {
102         double[][] pairs = {
103             { -50.0, 50.0 },
104             {  Double.POSITIVE_INFINITY, 1.0 },
105             {  Double.NEGATIVE_INFINITY, 1.0 },
106             {  Double.NaN, 1.0 },
107             {  Double.POSITIVE_INFINITY, 0.0 },
108             {  Double.NEGATIVE_INFINITY, 0.0 },
109             {  Double.NaN, 0.0 },
110             {  Double.NaN, Double.NEGATIVE_INFINITY },
111             {  Double.NaN, Double.POSITIVE_INFINITY },
112             { Precision.SAFE_MIN, Precision.EPSILON }
113         };
114         for (double[] pair : pairs) {
115             assertEquals(Math.min(pair[0], pair[1]),
116                          FastMath.min(new Binary64(pair[0]), new Binary64(pair[1])).getReal(),
117                          Precision.EPSILON,
118                          "min(" + pair[0] + ", " + pair[1] + ")");
119             assertEquals(Math.min(pair[1], pair[0]),
120                          FastMath.min(new Binary64(pair[0]), new Binary64(pair[1])).getReal(),
121                          Precision.EPSILON,
122                          "min(" + pair[1] + ", " + pair[0] + ")");
123             assertEquals(Math.max(pair[0], pair[1]),
124                          FastMath.max(new Binary64(pair[0]), new Binary64(pair[1])).getReal(),
125                          Precision.EPSILON,
126                          "max(" + pair[0] + ", " + pair[1] + ")");
127             assertEquals(Math.max(pair[1], pair[0]),
128                          FastMath.max(new Binary64(pair[0]), new Binary64(pair[1])).getReal(),
129                          Precision.EPSILON,
130                          "max(" + pair[1] + ", " + pair[0] + ")");
131         }
132     }
133 
134     @Test
135     void testMinMaxFloat() {
136         float[][] pairs = {
137             { -50.0f, 50.0f },
138             {  Float.POSITIVE_INFINITY, 1.0f },
139             {  Float.NEGATIVE_INFINITY, 1.0f },
140             {  Float.NaN, 1.0f },
141             {  Float.POSITIVE_INFINITY, 0.0f },
142             {  Float.NEGATIVE_INFINITY, 0.0f },
143             {  Float.NaN, 0.0f },
144             {  Float.NaN, Float.NEGATIVE_INFINITY },
145             {  Float.NaN, Float.POSITIVE_INFINITY }
146         };
147         for (float[] pair : pairs) {
148             assertEquals(Math.min(pair[0], pair[1]),
149                          FastMath.min(pair[0], pair[1]),
150                          Precision.EPSILON,
151                          "min(" + pair[0] + ", " + pair[1] + ")");
152             assertEquals(Math.min(pair[1], pair[0]),
153                          FastMath.min(pair[1], pair[0]),
154                          Precision.EPSILON,
155                          "min(" + pair[1] + ", " + pair[0] + ")");
156             assertEquals(Math.max(pair[0], pair[1]),
157                          FastMath.max(pair[0], pair[1]),
158                          Precision.EPSILON,
159                          "max(" + pair[0] + ", " + pair[1] + ")");
160             assertEquals(Math.max(pair[1], pair[0]),
161                          FastMath.max(pair[1], pair[0]),
162                          Precision.EPSILON,
163                          "max(" + pair[1] + ", " + pair[0] + ")");
164         }
165     }
166 
167     @Test
168     void testConstants() {
169         assertEquals(0x400921fb54442d18L, Double.doubleToRawLongBits(FastMath.PI));
170         assertEquals(0x401921fb54442d18L, Double.doubleToRawLongBits(FastMath.TAU));
171         assertEquals(0x4005bf0a8b145769L, Double.doubleToRawLongBits(FastMath.E));
172     }
173 
174     @Test
175     void testAtan2() {
176         double y1 = 1.2713504628280707e10;
177         double x1 = -5.674940885228782e-10;
178         assertEquals(Math.atan2(y1, x1), FastMath.atan2(y1, x1), 2 * Precision.EPSILON);
179         double y2 = 0.0;
180         double x2P = Double.POSITIVE_INFINITY;
181         assertEquals(Math.atan2(y2, x2P), FastMath.atan2(y2, x2P), Precision.SAFE_MIN);
182         double x2M = Double.NEGATIVE_INFINITY;
183         assertEquals(Math.atan2(y2, x2M), FastMath.atan2(y2, x2M), Precision.SAFE_MIN);
184         assertEquals(+0.5 * FastMath.PI, FastMath.atan2(+1e20, +Precision.SAFE_MIN), Precision.SAFE_MIN);
185         assertEquals(+0.5 * FastMath.PI, FastMath.atan2(+1e20, -Precision.SAFE_MIN), Precision.SAFE_MIN);
186         assertEquals(-0.5 * FastMath.PI, FastMath.atan2(-1e20, +Precision.SAFE_MIN), Precision.SAFE_MIN);
187         assertEquals(-0.5 * FastMath.PI, FastMath.atan2(-1e20, -Precision.SAFE_MIN), Precision.SAFE_MIN);
188         assertEquals( 0.0,               FastMath.atan2(+Precision.SAFE_MIN, +1e20), Precision.SAFE_MIN);
189         assertEquals(+1.0,               FastMath.copySign(1.0, FastMath.atan2(+Precision.SAFE_MIN, +1e20)), Precision.SAFE_MIN);
190         assertEquals( 0.0,               FastMath.atan2(-Precision.SAFE_MIN, +1e20), Precision.SAFE_MIN);
191         assertEquals(-1.0,               FastMath.copySign(1.0, FastMath.atan2(-Precision.SAFE_MIN, +1e20)), Precision.SAFE_MIN);
192         assertEquals(+FastMath.PI,       FastMath.atan2(+Precision.SAFE_MIN, -1e20), Precision.SAFE_MIN);
193         assertEquals(-FastMath.PI,       FastMath.atan2(-Precision.SAFE_MIN, -1e20), Precision.SAFE_MIN);
194     }
195 
196     @Test
197     void testHyperbolic() {
198         double maxErr = 0;
199         for (double x = -30; x < 30; x += 0.001) {
200             double tst = FastMath.sinh(x);
201             double ref = Math.sinh(x);
202             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
203         }
204         assertEquals(0, maxErr, 2);
205 
206         maxErr = 0;
207         for (double x = -30; x < 30; x += 0.001) {
208             double tst = FastMath.cosh(x);
209             double ref = Math.cosh(x);
210             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
211         }
212         assertEquals(0, maxErr, 2);
213 
214         maxErr = 0;
215         for (double x = -0.5; x < 0.5; x += 0.001) {
216             double tst = FastMath.tanh(x);
217             double ref = Math.tanh(x);
218             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
219         }
220         assertEquals(0, maxErr, 4);
221 
222     }
223 
224     @Test
225     void testMath904() {
226         final double x = -1;
227         final double y = (5 + 1e-15) * 1e15;
228         assertEquals(Math.pow(x, y),
229                      FastMath.pow(x, y), 0);
230         assertEquals(Math.pow(x, -y),
231                      FastMath.pow(x, -y), 0);
232     }
233 
234     @Test
235     void testMath905LargePositive() {
236         final double start = StrictMath.log(Double.MAX_VALUE);
237         final double endT = StrictMath.sqrt(2) * StrictMath.sqrt(Double.MAX_VALUE);
238         final double end = 2 * StrictMath.log(endT);
239 
240         double maxErr = 0;
241         for (double x = start; x < end; x += 1e-3) {
242             final double tst = FastMath.cosh(x);
243             final double ref = Math.cosh(x);
244             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
245         }
246         assertEquals(0, maxErr, 3);
247 
248         for (double x = start; x < end; x += 1e-3) {
249             final double tst = FastMath.sinh(x);
250             final double ref = Math.sinh(x);
251             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
252         }
253         assertEquals(0, maxErr, 3);
254     }
255 
256     @Test
257     void testMath905LargeNegative() {
258         final double start = -StrictMath.log(Double.MAX_VALUE);
259         final double endT = StrictMath.sqrt(2) * StrictMath.sqrt(Double.MAX_VALUE);
260         final double end = -2 * StrictMath.log(endT);
261 
262         double maxErr = 0;
263         for (double x = start; x > end; x -= 1e-3) {
264             final double tst = FastMath.cosh(x);
265             final double ref = Math.cosh(x);
266             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
267         }
268         assertEquals(0, maxErr, 3);
269 
270         for (double x = start; x > end; x -= 1e-3) {
271             final double tst = FastMath.sinh(x);
272             final double ref = Math.sinh(x);
273             maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref));
274         }
275         assertEquals(0, maxErr, 3);
276     }
277 
278     @Test
279     void testMath1269() {
280         final double arg = 709.8125;
281         final double vM = Math.exp(arg);
282         final double vFM = FastMath.exp(arg);
283         assertTrue(Precision.equalsIncludingNaN(vM, vFM),
284                    "exp(" + arg + ") is " + vFM + " instead of " + vM);
285     }
286 
287     @Test
288     void testHyperbolicInverses() {
289         double maxErr = 0;
290         for (double x = -30; x < 30; x += 0.01) {
291             maxErr = FastMath.max(maxErr, FastMath.abs(x - FastMath.sinh(FastMath.asinh(x))) / (2 * FastMath.ulp(x)));
292         }
293         assertEquals(0, maxErr, 3);
294 
295         maxErr = 0;
296         for (double x = 1; x < 30; x += 0.01) {
297             maxErr = FastMath.max(maxErr, FastMath.abs(x - FastMath.cosh(FastMath.acosh(x))) / (2 * FastMath.ulp(x)));
298         }
299         assertEquals(0, maxErr, 2);
300 
301         maxErr = 0;
302         for (double x = -1 + Precision.EPSILON; x < 1 - Precision.EPSILON; x += 0.0001) {
303             maxErr = FastMath.max(maxErr, FastMath.abs(x - FastMath.tanh(FastMath.atanh(x))) / (2 * FastMath.ulp(x)));
304         }
305         assertEquals(0, maxErr, 2);
306     }
307 
308     @Test
309     void testLogAccuracy() {
310         double maxerrulp = 0.0;
311 
312         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
313             double x = Math.exp(generator.nextDouble() * 1416.0 - 708.0) * generator.nextDouble();
314             // double x = generator.nextDouble()*2.0;
315             double tst = FastMath.log(x);
316             double ref = DfpMath.log(field.newDfp(x)).toDouble();
317             double err = (tst - ref) / ref;
318 
319             if (err != 0.0) {
320                 double ulp = Math.abs(ref -
321                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
322                 double errulp = field.newDfp(tst).subtract(DfpMath.log(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
323 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
324 
325                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
326             }
327         }
328 
329         assertTrue(maxerrulp < MAX_ERROR_ULP, "log() had errors in excess of " + MAX_ERROR_ULP + " ULP");
330     }
331 
332     @Test
333     void testLog10Accuracy() {
334         double maxerrulp = 0.0;
335 
336         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
337             double x = Math.exp(generator.nextDouble() * 1416.0 - 708.0) * generator.nextDouble();
338             // double x = generator.nextDouble()*2.0;
339             double tst = FastMath.log10(x);
340             double ref = DfpMath.log(field.newDfp(x)).divide(DfpMath.log(field.newDfp("10"))).toDouble();
341             double err = (tst - ref) / ref;
342 
343             if (err != 0.0) {
344                 double ulp = Math.abs(ref -
345                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
346                 double errulp = field.newDfp(tst).subtract(DfpMath.log(field.newDfp(x)).divide(DfpMath.log(field.newDfp("10")))).divide(field.newDfp(ulp)).toDouble();
347 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
348 
349                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
350             }
351         }
352 
353         assertTrue(maxerrulp < MAX_ERROR_ULP, "log10() had errors in excess of " + MAX_ERROR_ULP + " ULP");
354     }
355 
356     @Test
357     void testLog1pAccuracy() {
358         double maxerrulp = 0.0;
359 
360         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
361             double x = Math.exp(generator.nextDouble() * 10.0 - 5.0) * generator.nextDouble();
362             // double x = generator.nextDouble()*2.0;
363             double tst = FastMath.log1p(x);
364             double ref = DfpMath.log(field.newDfp(x).add(field.getOne())).toDouble();
365             double err = (tst - ref) / ref;
366 
367             if (err != 0.0) {
368                 double ulp = Math.abs(ref -
369                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
370                 double errulp = field.newDfp(tst).subtract(DfpMath.log(field.newDfp(x).add(field.getOne()))).divide(field.newDfp(ulp)).toDouble();
371 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
372 
373                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
374             }
375         }
376 
377         assertTrue(maxerrulp < MAX_ERROR_ULP, "log1p() had errors in excess of " + MAX_ERROR_ULP + " ULP");
378     }
379 
380     @Test
381     void testLog1pSpecialCases() {
382         assertTrue(Double.isInfinite(FastMath.log1p(-1.0)), "Logp of -1.0 should be -Inf");
383     }
384 
385     @Test
386     void testLogSpecialCases() {
387         assertEquals(Double.NEGATIVE_INFINITY, FastMath.log(0.0), 1.0, "Log of zero should be -Inf");
388         assertEquals(Double.NEGATIVE_INFINITY, FastMath.log(-0.0), 1.0, "Log of -zero should be -Inf");
389         assertTrue(Double.isNaN(FastMath.log(Double.NaN)), "Log of NaN should be NaN");
390         assertTrue(Double.isNaN(FastMath.log(-1.0)), "Log of negative number should be NaN");
391         assertEquals(-744.4400719213812, FastMath.log(Double.MIN_VALUE), Precision.EPSILON, "Log of Double.MIN_VALUE should be -744.4400719213812");
392         assertEquals(Double.POSITIVE_INFINITY, FastMath.log(Double.POSITIVE_INFINITY), 1.0, "Log of infinity should be infinity");
393     }
394 
395     @Test
396     void testExpSpecialCases() {
397         // Smallest value that will round up to Double.MIN_VALUE
398         assertEquals(Double.MIN_VALUE, FastMath.exp(-745.1332191019411), Precision.EPSILON);
399         assertEquals(0.0, FastMath.exp(-745.1332191019412), Precision.EPSILON, "exp(-745.1332191019412) should be 0.0");
400         assertTrue(Double.isNaN(FastMath.exp(Double.NaN)), "exp of NaN should be NaN");
401         assertEquals(Double.POSITIVE_INFINITY, FastMath.exp(Double.POSITIVE_INFINITY), 1.0, "exp of infinity should be infinity");
402         assertEquals(0.0, FastMath.exp(Double.NEGATIVE_INFINITY), Precision.EPSILON, "exp of -infinity should be 0.0");
403         assertEquals(Math.E, FastMath.exp(1.0), Precision.EPSILON, "exp(1) should be Math.E");
404     }
405 
406     @Test
407     void testPowSpecialCases() {
408         final double EXACT = 1.0;
409 
410         assertEquals(1.0, FastMath.pow(-1.0, 0.0), Precision.EPSILON, "pow(-1, 0) should be 1.0");
411         assertEquals(1.0, FastMath.pow(-1.0, -0.0), Precision.EPSILON, "pow(-1, -0) should be 1.0");
412         assertEquals(FastMath.PI, FastMath.pow(FastMath.PI, 1.0), Precision.EPSILON, "pow(PI, 1.0) should be PI");
413         assertEquals(-FastMath.PI, FastMath.pow(-FastMath.PI, 1.0), Precision.EPSILON, "pow(-PI, 1.0) should be -PI");
414         assertTrue(Double.isNaN(FastMath.pow(Math.PI, Double.NaN)), "pow(PI, NaN) should be NaN");
415         assertTrue(Double.isNaN(FastMath.pow(Double.NaN, Math.PI)), "pow(NaN, PI) should be NaN");
416         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(2.0, Double.POSITIVE_INFINITY), 1.0, "pow(2.0, Infinity) should be Infinity");
417         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(0.5, Double.NEGATIVE_INFINITY), 1.0, "pow(0.5, -Infinity) should be Infinity");
418         assertEquals(0.0, FastMath.pow(0.5, Double.POSITIVE_INFINITY), Precision.EPSILON, "pow(0.5, Infinity) should be 0.0");
419         assertEquals(0.0, FastMath.pow(2.0, Double.NEGATIVE_INFINITY), Precision.EPSILON, "pow(2.0, -Infinity) should be 0.0");
420         assertEquals(0.0, FastMath.pow(0.0, 0.5), Precision.EPSILON, "pow(0.0, 0.5) should be 0.0");
421         assertEquals(0.0, FastMath.pow(Double.POSITIVE_INFINITY, -0.5), Precision.EPSILON, "pow(Infinity, -0.5) should be 0.0");
422         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(0.0, -0.5), 1.0, "pow(0.0, -0.5) should be Inf");
423         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.POSITIVE_INFINITY, 0.5), 1.0, "pow(Inf, 0.5) should be Inf");
424         assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(-0.0, -3.0), 1.0, "pow(-0.0, -3.0) should be -Inf");
425         assertEquals(0.0, FastMath.pow(-0.0, Double.POSITIVE_INFINITY), Precision.EPSILON, "pow(-0.0, Infinity) should be 0.0");
426         assertTrue(Double.isNaN(FastMath.pow(-0.0, Double.NaN)), "pow(-0.0, NaN) should be NaN");
427         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-0.0, -Double.MIN_VALUE), 1.0, "pow(-0.0, -tiny) should be Infinity");
428         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-0.0, -Double.MAX_VALUE), 1.0, "pow(-0.0, -huge) should be Infinity");
429         assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, 3.0), 1.0, "pow(-Inf, 3.0) should be -Inf");
430         assertEquals(-0.0, FastMath.pow(Double.NEGATIVE_INFINITY, -3.0), EXACT, "pow(-Inf, -3.0) should be -0.0");
431         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-0.0, -3.5), 1.0, "pow(-0.0, -3.5) should be Inf");
432         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.POSITIVE_INFINITY, 3.5), 1.0, "pow(Inf, 3.5) should be Inf");
433         assertEquals(-8.0, FastMath.pow(-2.0, 3.0), Precision.EPSILON, "pow(-2.0, 3.0) should be -8.0");
434         assertTrue(Double.isNaN(FastMath.pow(-2.0, 3.5)), "pow(-2.0, 3.5) should be NaN");
435         assertTrue(Double.isNaN(FastMath.pow(Double.NaN, Double.NEGATIVE_INFINITY)), "pow(NaN, -Infinity) should be NaN");
436         assertEquals(1.0, FastMath.pow(Double.NaN, 0.0), Precision.EPSILON, "pow(NaN, 0.0) should be 1.0");
437         assertEquals(0.0, FastMath.pow(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY), Precision.EPSILON, "pow(-Infinity, -Infinity) should be 0.0");
438         assertEquals(0.0, FastMath.pow(-Double.MAX_VALUE, -Double.MAX_VALUE), Precision.EPSILON, "pow(-huge, -huge) should be 0.0");
439         assertTrue(Double.isInfinite(FastMath.pow(-Double.MAX_VALUE, Double.MAX_VALUE)), "pow(-huge,  huge) should be +Inf");
440         assertTrue(Double.isNaN(FastMath.pow(Double.NaN, Double.NEGATIVE_INFINITY)), "pow(NaN, -Infinity) should be NaN");
441         assertEquals(1.0, FastMath.pow(Double.NaN, -0.0), Precision.EPSILON, "pow(NaN, -0.0) should be 1.0");
442         assertEquals(0.0, FastMath.pow(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY), Precision.EPSILON, "pow(-Infinity, -Infinity) should be 0.0");
443         assertEquals(0.0, FastMath.pow(-Double.MAX_VALUE, -Double.MAX_VALUE), Precision.EPSILON, "pow(-huge, -huge) should be 0.0");
444         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-Double.MAX_VALUE, Double.MAX_VALUE), 1.0, "pow(-huge,  huge) should be +Inf");
445 
446         // Added tests for a 100% coverage
447 
448         assertTrue(Double.isNaN(FastMath.pow(Double.POSITIVE_INFINITY, Double.NaN)), "pow(+Inf, NaN) should be NaN");
449         assertTrue(Double.isNaN(FastMath.pow(1.0, Double.POSITIVE_INFINITY)), "pow(1.0, +Inf) should be NaN");
450         assertTrue(Double.isNaN(FastMath.pow(Double.NEGATIVE_INFINITY, Double.NaN)), "pow(-Inf, NaN) should be NaN");
451         assertEquals(-0.0, FastMath.pow(Double.NEGATIVE_INFINITY, -1.0), EXACT, "pow(-Inf, -1.0) should be -0.0");
452         assertEquals(0.0, FastMath.pow(Double.NEGATIVE_INFINITY, -2.0), EXACT, "pow(-Inf, -2.0) should be 0.0");
453         assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, 1.0), 1.0, "pow(-Inf, 1.0) should be -Inf");
454         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, 2.0), 1.0, "pow(-Inf, 2.0) should be +Inf");
455         assertTrue(Double.isNaN(FastMath.pow(1.0, Double.NEGATIVE_INFINITY)), "pow(1.0, -Inf) should be NaN");
456         assertEquals(-0.0, FastMath.pow(-0.0, 1.0), EXACT, "pow(-0.0, 1.0) should be -0.0");
457         assertEquals(0.0, FastMath.pow(0.0, 1.0), EXACT, "pow(0.0, 1.0) should be 0.0");
458         assertEquals(0.0, FastMath.pow(0.0, Double.POSITIVE_INFINITY), EXACT, "pow(0.0, +Inf) should be 0.0");
459         assertEquals(0.0, FastMath.pow(-0.0, 6.0), EXACT, "pow(-0.0, even) should be 0.0");
460         assertEquals(-0.0, FastMath.pow(-0.0, 13.0), EXACT, "pow(-0.0, odd) should be -0.0");
461         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-0.0, -6.0), EXACT, "pow(-0.0, -even) should be +Inf");
462         assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(-0.0, -13.0), EXACT, "pow(-0.0, -odd) should be -Inf");
463         assertEquals(16.0, FastMath.pow(-2.0, 4.0), EXACT, "pow(-2.0, 4.0) should be 16.0");
464         assertEquals(Double.NaN, FastMath.pow(-2.0, 4.5), EXACT, "pow(-2.0, 4.5) should be NaN");
465         assertEquals(1.0, FastMath.pow(-0.0, -0.0), EXACT, "pow(-0.0, -0.0) should be 1.0");
466         assertEquals(1.0, FastMath.pow(-0.0, 0.0), EXACT, "pow(-0.0, 0.0) should be 1.0");
467         assertEquals(1.0, FastMath.pow(0.0, -0.0), EXACT, "pow(0.0, -0.0) should be 1.0");
468         assertEquals(1.0, FastMath.pow(0.0, 0.0), EXACT, "pow(0.0, 0.0) should be 1.0");
469     }
470 
471     @Test
472     @Timeout(value = 20000L, unit = TimeUnit.MILLISECONDS)
473     void testPowAllSpecialCases() {
474         final double EXACT = 0;
475         final double[] DOUBLES = new double[]
476             {
477                 Double.NEGATIVE_INFINITY, -0.0, Double.NaN, 0.0, Double.POSITIVE_INFINITY,
478                 Long.MIN_VALUE, Integer.MIN_VALUE, Short.MIN_VALUE, Byte.MIN_VALUE,
479                 -(double)Long.MIN_VALUE, -(double)Integer.MIN_VALUE, -(double)Short.MIN_VALUE, -(double)Byte.MIN_VALUE,
480                 Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE, Long.MAX_VALUE,
481                 -Byte.MAX_VALUE, -Short.MAX_VALUE, -Integer.MAX_VALUE, -Long.MAX_VALUE,
482                 Float.MAX_VALUE, Double.MAX_VALUE, Double.MIN_VALUE, Float.MIN_VALUE,
483                 -Float.MAX_VALUE, -Double.MAX_VALUE, -Double.MIN_VALUE, -Float.MIN_VALUE,
484                 0.5, 0.1, 0.2, 0.8, 1.1, 1.2, 1.5, 1.8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.3, 2.2, 2.5, 2.8, 33.0, 33.1, 33.5, 33.8, 10.0, 300.0, 400.0, 500.0,
485                 -0.5, -0.1, -0.2, -0.8, -1.1, -1.2, -1.5, -1.8, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0, -1.3, -2.2, -2.5, -2.8, -33.0, -33.1, -33.5, -33.8, -10.0, -300.0, -400.0, -500.0
486             };
487 
488         // Special cases from Math.pow javadoc:
489         // If the second argument is positive or negative zero, then the result is 1.0.
490         for (double d : DOUBLES) {
491             assertEquals(1.0, FastMath.pow(d, 0.0), EXACT);
492         }
493         for (double d : DOUBLES) {
494             assertEquals(1.0, FastMath.pow(d, -0.0), EXACT);
495         }
496         // If the second argument is 1.0, then the result is the same as the first argument.
497         for (double d : DOUBLES) {
498             assertEquals(d, FastMath.pow(d, 1.0), EXACT);
499         }
500         // If the second argument is NaN, then the result is NaN.
501         for (double d : DOUBLES) {
502             assertEquals(Double.NaN, FastMath.pow(d, Double.NaN), EXACT);
503         }
504         // If the first argument is NaN and the second argument is nonzero, then the result is NaN.
505         for (double i : DOUBLES) {
506             if (i != 0.0) {
507                 assertEquals(Double.NaN, FastMath.pow(Double.NaN, i), EXACT);
508             }
509         }
510         // If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or
511         // the absolute value of the first argument is less than 1 and the second argument is negative infinity, then the result is positive infinity.
512         for (double d : DOUBLES) {
513             if (Math.abs(d) > 1.0) {
514                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(d, Double.POSITIVE_INFINITY), EXACT);
515             }
516         }
517         for (double d : DOUBLES) {
518             if (Math.abs(d) < 1.0) {
519                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(d, Double.NEGATIVE_INFINITY), EXACT);
520             }
521         }
522         // If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or
523         // the absolute value of the first argument is less than 1 and the second argument is positive infinity, then the result is positive zero.
524         for (double d : DOUBLES) {
525             if (Math.abs(d) > 1.0) {
526                 assertEquals(0.0, FastMath.pow(d, Double.NEGATIVE_INFINITY), EXACT);
527             }
528         }
529         for (double d : DOUBLES) {
530             if (Math.abs(d) < 1.0) {
531                 assertEquals(0.0, FastMath.pow(d, Double.POSITIVE_INFINITY), EXACT);
532             }
533         }
534         // If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.
535         assertEquals(Double.NaN, FastMath.pow(1.0, Double.POSITIVE_INFINITY), EXACT);
536         assertEquals(Double.NaN, FastMath.pow(1.0, Double.NEGATIVE_INFINITY), EXACT);
537         assertEquals(Double.NaN, FastMath.pow(-1.0, Double.POSITIVE_INFINITY), EXACT);
538         assertEquals(Double.NaN, FastMath.pow(-1.0, Double.NEGATIVE_INFINITY), EXACT);
539         // If the first argument is positive zero and the second argument is greater than zero, or
540         // the first argument is positive infinity and the second argument is less than zero, then the result is positive zero.
541         for (double i : DOUBLES) {
542             if (i > 0.0) {
543                 assertEquals(0.0, FastMath.pow(0.0, i), EXACT);
544             }
545         }
546         for (double i : DOUBLES) {
547             if (i < 0.0) {
548                 assertEquals(0.0, FastMath.pow(Double.POSITIVE_INFINITY, i), EXACT);
549             }
550         }
551         // If the first argument is positive zero and the second argument is less than zero, or
552         // the first argument is positive infinity and the second argument is greater than zero, then the result is positive infinity.
553         for (double i : DOUBLES) {
554             if (i < 0.0) {
555                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(0.0, i), EXACT);
556             }
557         }
558         for (double i : DOUBLES) {
559             if (i > 0.0) {
560                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.POSITIVE_INFINITY, i), EXACT);
561             }
562         }
563         // If the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or
564         // the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, then the result is positive zero.
565         for (double i : DOUBLES) {
566             if (i > 0.0 && (Double.isInfinite(i) || i % 2.0 == 0.0)) {
567                 assertEquals(0.0, FastMath.pow(-0.0, i), EXACT);
568             }
569         }
570         for (double i : DOUBLES) {
571             if (i < 0.0 && (Double.isInfinite(i) || i % 2.0 == 0.0)) {
572                 assertEquals(0.0, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
573             }
574         }
575         // If the first argument is negative zero and the second argument is a positive finite odd integer, or
576         // the first argument is negative infinity and the second argument is a negative finite odd integer, then the result is negative zero.
577         for (double i : DOUBLES) {
578             if (i > 0.0 && i % 2.0 == 1.0) {
579                 assertEquals(-0.0, FastMath.pow(-0.0, i), EXACT);
580             }
581         }
582         for (double i : DOUBLES) {
583             if (i < 0.0 && i % 2.0 == -1.0) {
584                 assertEquals(-0.0, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
585             }
586         }
587         // If the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or
588         // the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, then the result is positive infinity.
589         for (double i : DOUBLES) {
590             if (i > 0.0 && (Double.isInfinite(i) || i % 2.0 == 0.0)) {
591                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
592             }
593         }
594         for (double i : DOUBLES) {
595             if (i < 0.0 && (Double.isInfinite(i) || i % 2.0 == 0.0)) {
596                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-0.0, i), EXACT);
597             }
598         }
599         // If the first argument is negative zero and the second argument is a negative finite odd integer, or
600         // the first argument is negative infinity and the second argument is a positive finite odd integer, then the result is negative infinity.
601         for (double i : DOUBLES) {
602             if (i > 0.0 && i % 2.0 == 1.0) {
603                 assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
604             }
605         }
606         for (double i : DOUBLES) {
607             if (i < 0.0 && i % 2.0 == -1.0) {
608                 assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(-0.0, i), EXACT);
609             }
610         }
611         for (double d : DOUBLES) {
612             // If the first argument is finite and less than zero
613             if (d < 0.0 && Math.abs(d) <= Double.MAX_VALUE) {
614                 for (double i : DOUBLES) {
615                     if (Math.abs(i) <= Double.MAX_VALUE) {
616                         // if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
617                         if (i % 2.0 == 0.0) assertEquals(FastMath.pow(-d, i), FastMath.pow(d, i), EXACT);
618                         // if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
619                         else if (Math.abs(i) % 2.0 == 1.0) assertEquals(-FastMath.pow(-d, i), FastMath.pow(d, i), EXACT);
620                         // if the second argument is finite and not an integer, then the result is NaN.
621                         else assertEquals(Double.NaN, FastMath.pow(d, i), EXACT);
622                     }
623                 }
624             }
625         }
626         // If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first argument to the power
627         // of the second argument if that result can in fact be represented exactly as a double value.
628         final int TOO_BIG_TO_CALCULATE = 18; // This value is empirical: 2^18 > 200.000 resulting bits after raising d to power i.
629         for (double d : DOUBLES) {
630             if (d % 1.0 == 0.0) {
631                 boolean dNegative = Double.doubleToRawLongBits( d ) < 0L;
632                 for (double i : DOUBLES) {
633                     if (i % 1.0 == 0.0) {
634                         BigInteger bd = BigDecimal.valueOf(d).toBigInteger().abs();
635                         BigInteger bi = BigDecimal.valueOf(i).toBigInteger().abs();
636                         double expected;
637                         if (bd.bitLength() > 1 && bi.bitLength() > 1 && 32 - Integer.numberOfLeadingZeros(bd.bitLength()) + bi.bitLength() > TOO_BIG_TO_CALCULATE) {
638                             // Result would be too big.
639                             expected = i < 0.0 ? 0.0 : Double.POSITIVE_INFINITY;
640                         } else {
641                             BigInteger res = ArithmeticUtils.pow(bd, bi);
642                             if (i >= 0.0) {
643                                 expected = res.doubleValue();
644                             } else if (res.signum() == 0) {
645                                 expected = Double.POSITIVE_INFINITY;
646                             } else {
647                                 expected = BigDecimal.ONE.divide( new BigDecimal( res ), 1024, RoundingMode.HALF_UP ).doubleValue();
648                             }
649                         }
650                         if (dNegative && bi.testBit( 0 )) {
651                             expected = -expected;
652                         }
653                         assertEquals(expected, FastMath.pow(d, i), expected == 0.0 || Double.isInfinite(expected) || Double.isNaN(expected) ? EXACT : 2.0 * Math.ulp(expected), d + "^" + i + "=" + expected + ", Math.pow=" + Math.pow(d, i));
654                     }
655                 }
656             }
657         }
658     }
659 
660     @Test
661     void testPowLargeIntegralDouble() {
662         double y = FastMath.scalb(1.0, 65);
663         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(FastMath.nextUp(1.0), y),    1.0);
664         assertEquals(1.0,                      FastMath.pow(1.0, y),                     1.0);
665         assertEquals(0.0,                      FastMath.pow(FastMath.nextDown(1.0), y),  1.0);
666         assertEquals(0.0,                      FastMath.pow(FastMath.nextUp(-1.0), y),   1.0);
667         assertEquals(1.0,                      FastMath.pow(-1.0, y),                    1.0);
668         assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(FastMath.nextDown(-1.0), y), 1.0);
669     }
670 
671     @Test
672     void testAtan2SpecialCases() {
673 
674         assertTrue(Double.isNaN(FastMath.atan2(Double.NaN, 0.0)), "atan2(NaN, 0.0) should be NaN");
675         assertTrue(Double.isNaN(FastMath.atan2(0.0, Double.NaN)), "atan2(0.0, NaN) should be NaN");
676         assertEquals(0.0, FastMath.atan2(0.0, 0.0), Precision.EPSILON, "atan2(0.0, 0.0) should be 0.0");
677         assertEquals(0.0, FastMath.atan2(0.0, 0.001), Precision.EPSILON, "atan2(0.0, 0.001) should be 0.0");
678         assertEquals(0.0, FastMath.atan2(0.1, Double.POSITIVE_INFINITY), Precision.EPSILON, "atan2(0.1, +Inf) should be 0.0");
679         assertEquals(-0.0, FastMath.atan2(-0.0, 0.0), Precision.EPSILON, "atan2(-0.0, 0.0) should be -0.0");
680         assertEquals(-0.0, FastMath.atan2(-0.0, 0.001), Precision.EPSILON, "atan2(-0.0, 0.001) should be -0.0");
681         assertEquals(-0.0, FastMath.atan2(-0.1, Double.POSITIVE_INFINITY), Precision.EPSILON, "atan2(-0.0, +Inf) should be -0.0");
682         assertEquals(FastMath.PI, FastMath.atan2(0.0, -0.0), Precision.EPSILON, "atan2(0.0, -0.0) should be PI");
683         assertEquals(FastMath.PI, FastMath.atan2(0.1, Double.NEGATIVE_INFINITY), Precision.EPSILON, "atan2(0.1, -Inf) should be PI");
684         assertEquals(-FastMath.PI, FastMath.atan2(-0.0, -0.0), Precision.EPSILON, "atan2(-0.0, -0.0) should be -PI");
685         assertEquals(-FastMath.PI, FastMath.atan2(-0.1, Double.NEGATIVE_INFINITY), Precision.EPSILON, "atan2(0.1, -Inf) should be -PI");
686         assertEquals(FastMath.PI / 2.0, FastMath.atan2(0.1, 0.0), Precision.EPSILON, "atan2(0.1, 0.0) should be PI/2");
687         assertEquals(FastMath.PI / 2.0, FastMath.atan2(0.1, -0.0), Precision.EPSILON, "atan2(0.1, -0.0) should be PI/2");
688         assertEquals(FastMath.PI / 2.0, FastMath.atan2(Double.POSITIVE_INFINITY, 0.1), Precision.EPSILON, "atan2(Inf, 0.1) should be PI/2");
689         assertEquals(FastMath.PI / 2.0, FastMath.atan2(Double.POSITIVE_INFINITY, -0.1), Precision.EPSILON, "atan2(Inf, -0.1) should be PI/2");
690         assertEquals(-FastMath.PI / 2.0, FastMath.atan2(-0.1, 0.0), Precision.EPSILON, "atan2(-0.1, 0.0) should be -PI/2");
691         assertEquals(-FastMath.PI / 2.0, FastMath.atan2(-0.1, -0.0), Precision.EPSILON, "atan2(-0.1, -0.0) should be -PI/2");
692         assertEquals(-FastMath.PI / 2.0, FastMath.atan2(Double.NEGATIVE_INFINITY, 0.1), Precision.EPSILON, "atan2(-Inf, 0.1) should be -PI/2");
693         assertEquals(-FastMath.PI / 2.0, FastMath.atan2(Double.NEGATIVE_INFINITY, -0.1), Precision.EPSILON, "atan2(-Inf, -0.1) should be -PI/2");
694         assertEquals(FastMath.PI / 4.0, FastMath.atan2(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY),
695                      Precision.EPSILON,
696                      "atan2(Inf, Inf) should be PI/4");
697         assertEquals(FastMath.PI * 3.0 / 4.0,
698                      FastMath.atan2(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY), Precision.EPSILON, "atan2(Inf, -Inf) should be PI * 3/4");
699         assertEquals(-FastMath.PI / 4.0, FastMath.atan2(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY),
700                      Precision.EPSILON,
701                      "atan2(-Inf, Inf) should be -PI/4");
702         assertEquals(- FastMath.PI * 3.0 / 4.0,
703                      FastMath.atan2(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY), Precision.EPSILON, "atan2(-Inf, -Inf) should be -PI * 3/4");
704     }
705 
706     @Test
707     void testPowAccuracy() {
708         double maxerrulp = 0.0;
709 
710         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
711             double x = (generator.nextDouble() * 2.0 + 0.25);
712             double y = (generator.nextDouble() * 1200.0 - 600.0) * generator.nextDouble();
713             /*
714              * double x = FastMath.floor(generator.nextDouble()*1024.0 - 512.0); double
715              * y; if (x != 0) y = FastMath.floor(512.0 / FastMath.abs(x)); else
716              * y = generator.nextDouble()*1200.0; y = y - y/2; x = FastMath.pow(2.0, x) *
717              * generator.nextDouble(); y = y * generator.nextDouble();
718              */
719 
720             // double x = generator.nextDouble()*2.0;
721             double tst = FastMath.pow(x, y);
722             double ref = DfpMath.pow(field.newDfp(x), field.newDfp(y)).toDouble();
723             double err = (tst - ref) / ref;
724 
725             if (err != 0) {
726                 double ulp = Math.abs(ref -
727                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
728                 double errulp = field.newDfp(tst).subtract(DfpMath.pow(field.newDfp(x), field.newDfp(y))).divide(field.newDfp(ulp)).toDouble();
729 //                System.out.println(x + "\t" + y + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
730 
731                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
732             }
733         }
734 
735         assertTrue(maxerrulp < MAX_ERROR_ULP, "pow() had errors in excess of " + MAX_ERROR_ULP + " ULP");
736     }
737 
738     @Test
739     void testExpAccuracy() {
740         double maxerrulp = 0.0;
741 
742         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
743             /* double x = 1.0 + i/1024.0/2.0; */
744             double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble();
745             // double x = (generator.nextDouble() * 20.0) - 10.0;
746             // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
747             /* double x = 3.0 / 512.0 * i - 3.0; */
748             double tst = FastMath.exp(x);
749             double ref = DfpMath.exp(field.newDfp(x)).toDouble();
750             double err = (tst - ref) / ref;
751 
752             if (err != 0) {
753                 double ulp = Math.abs(ref -
754                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
755                 double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
756 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
757 
758                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
759             }
760         }
761 
762         assertTrue(maxerrulp < MAX_ERROR_ULP, "exp() had errors in excess of " + MAX_ERROR_ULP + " ULP");
763     }
764 
765     @Test
766     void testSinCosSpecialCases() {
767         for (double x : new double[] {
768             -0.0, +0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
769             Double.NaN, Precision.EPSILON, -Precision.EPSILON,
770             Precision.SAFE_MIN, -Precision.SAFE_MIN,
771             FastMath.PI, MathUtils.TWO_PI
772         }) {
773             doTestSinCos(x);
774         }
775     }
776 
777     @Test
778     void testSinCosRandom() {
779         final RandomGenerator random = new Well19937a(0xf67ff538323a55eaL);
780         for (int i = 0; i < 1000000; ++i) {
781             doTestSinCos(1000000.0 * (2.0 * random.nextDouble() - 1.0));
782         }
783     }
784 
785     private void doTestSinCos(final double x) {
786         final SinCos sinCos = FastMath.sinCos(x);
787         UnitTestUtils.customAssertSame(FastMath.sin(x), sinCos.sin());
788         UnitTestUtils.customAssertSame(FastMath.cos(x), sinCos.cos());
789     }
790 
791     @Test
792     void testSinCosSum() {
793         final RandomGenerator random = new Well19937a(0x4aab62a42c9eb940L);
794         for (int i = 0; i < 1000000; ++i) {
795             final double alpha = 10.0 * (2.0 * random.nextDouble() - 1.0);
796             final double beta  = 10.0 * (2.0 * random.nextDouble() - 1.0);
797             final SinCos scAlpha         = FastMath.sinCos(alpha);
798             final SinCos scBeta          = FastMath.sinCos(beta);
799             final SinCos scAlphaPlusBeta = FastMath.sinCos(alpha + beta);
800             final SinCos scSum           = SinCos.sum(scAlpha, scBeta);
801             assertEquals(scAlphaPlusBeta.sin(), scSum.sin(), 2.0e-15);
802             assertEquals(scAlphaPlusBeta.cos(), scSum.cos(), 2.0e-15);
803         }
804     }
805 
806     @Test
807     void testSinCosdifference() {
808         final RandomGenerator random = new Well19937a(0x589aaf49471b03d5L);
809         for (int i = 0; i < 1000000; ++i) {
810             final double alpha = 10.0 * (2.0 * random.nextDouble() - 1.0);
811             final double beta  = 10.0 * (2.0 * random.nextDouble() - 1.0);
812             final SinCos scAlpha          = FastMath.sinCos(alpha);
813             final SinCos scBeta           = FastMath.sinCos(beta);
814             final SinCos scAlphaMinusBeta = FastMath.sinCos(alpha - beta);
815             final SinCos scdifference     = SinCos.difference(scAlpha, scBeta);
816             assertEquals(scAlphaMinusBeta.sin(), scdifference.sin(), 2.0e-15);
817             assertEquals(scAlphaMinusBeta.cos(), scdifference.cos(), 2.0e-15);
818         }
819     }
820 
821     @Test
822     void testSinAccuracy() {
823         double maxerrulp = 0.0;
824 
825         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
826             /* double x = 1.0 + i/1024.0/2.0; */
827             // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble();
828             double x = ((generator.nextDouble() * Math.PI) - Math.PI / 2.0) *
829                        Math.pow(2, 21) * generator.nextDouble();
830             // double x = (generator.nextDouble() * 20.0) - 10.0;
831             // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
832             /* double x = 3.0 / 512.0 * i - 3.0; */
833             double tst = FastMath.sin(x);
834             double ref = DfpMath.sin(field.newDfp(x)).toDouble();
835             double err = (tst - ref) / ref;
836 
837             if (err != 0) {
838                 double ulp = Math.abs(ref -
839                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
840                 double errulp = field.newDfp(tst).subtract(DfpMath.sin(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
841 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
842 
843                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
844             }
845         }
846 
847         assertTrue(maxerrulp < MAX_ERROR_ULP, "sin() had errors in excess of " + MAX_ERROR_ULP + " ULP");
848     }
849 
850     @Test
851     void testCosAccuracy() {
852         double maxerrulp = 0.0;
853 
854         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
855             /* double x = 1.0 + i/1024.0/2.0; */
856             // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble();
857             double x = ((generator.nextDouble() * Math.PI) - Math.PI / 2.0) *
858                        Math.pow(2, 21) * generator.nextDouble();
859             // double x = (generator.nextDouble() * 20.0) - 10.0;
860             // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
861             /* double x = 3.0 / 512.0 * i - 3.0; */
862             double tst = FastMath.cos(x);
863             double ref = DfpMath.cos(field.newDfp(x)).toDouble();
864             double err = (tst - ref) / ref;
865 
866             if (err != 0) {
867                 double ulp = Math.abs(ref -
868                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
869                 double errulp = field.newDfp(tst).subtract(DfpMath.cos(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
870 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
871 
872                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
873             }
874         }
875 
876         assertTrue(maxerrulp < MAX_ERROR_ULP, "cos() had errors in excess of " + MAX_ERROR_ULP + " ULP");
877     }
878 
879     @Test
880     void testTanAccuracy() {
881         double maxerrulp = 0.0;
882 
883         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
884             /* double x = 1.0 + i/1024.0/2.0; */
885             // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble();
886             double x = ((generator.nextDouble() * Math.PI) - Math.PI / 2.0) *
887                        Math.pow(2, 12) * generator.nextDouble();
888             // double x = (generator.nextDouble() * 20.0) - 10.0;
889             // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
890             /* double x = 3.0 / 512.0 * i - 3.0; */
891             double tst = FastMath.tan(x);
892             double ref = DfpMath.tan(field.newDfp(x)).toDouble();
893             double err = (tst - ref) / ref;
894 
895             if (err != 0) {
896                 double ulp = Math.abs(ref -
897                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
898                 double errulp = field.newDfp(tst).subtract(DfpMath.tan(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
899 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
900 
901                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
902             }
903         }
904 
905         assertTrue(maxerrulp < MAX_ERROR_ULP, "tan() had errors in excess of " + MAX_ERROR_ULP + " ULP");
906     }
907 
908     @Test
909     void testAtanAccuracy() {
910         double maxerrulp = 0.0;
911 
912         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
913             /* double x = 1.0 + i/1024.0/2.0; */
914             // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble();
915             // double x = ((generator.nextDouble() * Math.PI) - Math.PI/2.0) *
916             // generator.nextDouble();
917             double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble();
918 
919             // double x = (generator.nextDouble() * 20.0) - 10.0;
920             // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
921             /* double x = 3.0 / 512.0 * i - 3.0; */
922             double tst = FastMath.atan(x);
923             double ref = DfpMath.atan(field.newDfp(x)).toDouble();
924             double err = (tst - ref) / ref;
925 
926             if (err != 0) {
927                 double ulp = Math.abs(ref -
928                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
929                 double errulp = field.newDfp(tst).subtract(DfpMath.atan(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
930 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
931 
932                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
933             }
934         }
935 
936         assertTrue(maxerrulp < MAX_ERROR_ULP, "atan() had errors in excess of " + MAX_ERROR_ULP + " ULP");
937     }
938 
939     @Test
940     void testAtan2Accuracy() {
941         double maxerrulp = 0.0;
942 
943         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
944             /* double x = 1.0 + i/1024.0/2.0; */
945             // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble();
946             double x = generator.nextDouble() - 0.5;
947             double y = generator.nextDouble() - 0.5;
948             // double x = (generator.nextDouble() * 20.0) - 10.0;
949             // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
950             /* double x = 3.0 / 512.0 * i - 3.0; */
951             double tst = FastMath.atan2(y, x);
952             Dfp refdfp = DfpMath.atan(field.newDfp(y).divide(field.newDfp(x)));
953             /* Make adjustments for sign */
954             if (x < 0.0) {
955                 if (y > 0.0)
956                     refdfp = field.getPi().add(refdfp);
957                 else
958                     refdfp = refdfp.subtract(field.getPi());
959             }
960 
961             double ref = refdfp.toDouble();
962             double err = (tst - ref) / ref;
963 
964             if (err != 0) {
965                 double ulp = Math.abs(ref -
966                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
967                 double errulp = field.newDfp(tst).subtract(refdfp).divide(field.newDfp(ulp)).toDouble();
968 //                System.out.println(x + "\t" + y + "\t" + tst + "\t" + ref + "\t" + errulp);
969 
970                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
971             }
972         }
973 
974         assertTrue(maxerrulp < MAX_ERROR_ULP, "atan2() had errors in excess of " + MAX_ERROR_ULP + " ULP");
975     }
976 
977     @Test
978     void testExpm1Huge() {
979         assertTrue(Double.isInfinite(FastMath.expm1(709.85)));
980     }
981 
982     @Test
983     void testExpm1Accuracy() {
984         double maxerrulp = 0.0;
985 
986         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
987             /* double x = 1.0 + i/1024.0/2.0; */
988             // double x = (generator.nextDouble() * 20.0) - 10.0;
989             double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble();
990             /* double x = 3.0 / 512.0 * i - 3.0; */
991             double tst = FastMath.expm1(x);
992             double ref = DfpMath.exp(field.newDfp(x)).subtract(field.getOne()).toDouble();
993             double err = (tst - ref) / ref;
994 
995             if (err != 0) {
996                 double ulp = Math.abs(ref -
997                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
998                 double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x)).subtract(field.getOne())).divide(field.newDfp(ulp)).toDouble();
999 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
1000 
1001                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1002             }
1003         }
1004 
1005         assertTrue(maxerrulp < MAX_ERROR_ULP, "expm1() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1006     }
1007 
1008     @Test
1009     void testAsinAccuracy() {
1010         double maxerrulp = 0.0;
1011 
1012         for (int i=0; i<10000; i++) {
1013             double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
1014 
1015             double tst = FastMath.asin(x);
1016             double ref = DfpMath.asin(field.newDfp(x)).toDouble();
1017             double err = (tst - ref) / ref;
1018 
1019             if (err != 0) {
1020                 double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1021                 double errulp = field.newDfp(tst).subtract(DfpMath.asin(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
1022                 //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp);
1023 
1024                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1025             }
1026         }
1027 
1028         assertTrue(maxerrulp < MAX_ERROR_ULP, "asin() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1029     }
1030 
1031     @Test
1032     void testAcosAccuracy() {
1033         double maxerrulp = 0.0;
1034 
1035         for (int i=0; i<10000; i++) {
1036             double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble();
1037 
1038             double tst = FastMath.acos(x);
1039             double ref = DfpMath.acos(field.newDfp(x)).toDouble();
1040             double err = (tst - ref) / ref;
1041 
1042             if (err != 0) {
1043                 double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1044                 double errulp = field.newDfp(tst).subtract(DfpMath.acos(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
1045                 //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp);
1046 
1047                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1048             }
1049         }
1050 
1051         assertTrue(maxerrulp < MAX_ERROR_ULP, "acos() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1052     }
1053 
1054     /**
1055      * Added tests for a 100% coverage of acos().
1056      */
1057     @Test
1058     void testAcosSpecialCases() {
1059 
1060         assertTrue(Double.isNaN(FastMath.acos(Double.NaN)), "acos(NaN) should be NaN");
1061         assertTrue(Double.isNaN(FastMath.acos(-1.1)), "acos(-1.1) should be NaN");
1062         assertTrue(Double.isNaN(FastMath.acos(1.1)), "acos(-1.1) should be NaN");
1063         assertEquals(FastMath.PI, FastMath.acos(-1.0), Precision.EPSILON, "acos(-1.0) should be PI");
1064         assertEquals(0.0, FastMath.acos(1.0), Precision.EPSILON, "acos(1.0) should be 0.0");
1065         assertEquals(FastMath.acos(0.0), FastMath.PI / 2.0, Precision.EPSILON, "acos(0.0) should be PI/2");
1066     }
1067 
1068     /**
1069      * Added tests for a 100% coverage of asin().
1070      */
1071     @Test
1072     void testAsinSpecialCases() {
1073 
1074         assertTrue(Double.isNaN(FastMath.asin(Double.NaN)), "asin(NaN) should be NaN");
1075         assertTrue(Double.isNaN(FastMath.asin(1.1)), "asin(1.1) should be NaN");
1076         assertTrue(Double.isNaN(FastMath.asin(-1.1)), "asin(-1.1) should be NaN");
1077         assertEquals(FastMath.asin(1.0), FastMath.PI / 2.0, Precision.EPSILON, "asin(1.0) should be PI/2");
1078         assertEquals(FastMath.asin(-1.0), -FastMath.PI / 2.0, Precision.EPSILON, "asin(-1.0) should be -PI/2");
1079         assertEquals(0.0, FastMath.asin(0.0), Precision.EPSILON, "asin(0.0) should be 0.0");
1080     }
1081 
1082     private Dfp cosh(Dfp x) {
1083       return DfpMath.exp(x).add(DfpMath.exp(x.negate())).half();
1084     }
1085 
1086     private Dfp sinh(Dfp x) {
1087       return DfpMath.exp(x).subtract(DfpMath.exp(x.negate())).half();
1088     }
1089 
1090     private Dfp tanh(Dfp x) {
1091       return sinh(x).divide(cosh(x));
1092     }
1093 
1094     @Test
1095     void testSinhCoshSpecialCases() {
1096         for (double x : new double[] {
1097             -0.0, +0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
1098             Double.NaN, Precision.EPSILON, -Precision.EPSILON,
1099             Precision.SAFE_MIN, -Precision.SAFE_MIN,
1100             FastMath.PI, MathUtils.TWO_PI
1101         }) {
1102             doTestSinhCosh(x);
1103         }
1104     }
1105 
1106     @Test
1107     void testSinhCoshRandom() {
1108         final RandomGenerator random = new Well19937a(0xa7babe18705d756fL);
1109         for (int i = 0; i < 1000000; ++i) {
1110             doTestSinhCosh(1000.0 * (2.0 * random.nextDouble() - 1.0));
1111         }
1112     }
1113 
1114     private void doTestSinhCosh(final double x) {
1115         final SinhCosh sinhCosh = FastMath.sinhCosh(x);
1116         UnitTestUtils.customAssertSame(FastMath.sinh(x), sinhCosh.sinh());
1117         UnitTestUtils.customAssertSame(FastMath.cosh(x), sinhCosh.cosh());
1118     }
1119 
1120     @Test
1121     void testSinhCoshSum() {
1122         final RandomGenerator random = new Well19937a(0x11cf5123446bc9ffL);
1123         for (int i = 0; i < 1000000; ++i) {
1124             final double alpha = 2.0 * (2.0 * random.nextDouble() - 1.0);
1125             final double beta  = 2.0 * (2.0 * random.nextDouble() - 1.0);
1126             final SinhCosh schAlpha         = FastMath.sinhCosh(alpha);
1127             final SinhCosh schBeta          = FastMath.sinhCosh(beta);
1128             final SinhCosh schAlphaPlusBeta = FastMath.sinhCosh(alpha + beta);
1129             final SinhCosh schSum           = SinhCosh.sum(schAlpha, schBeta);
1130             final double tol = 8 * FastMath.max(FastMath.max(FastMath.ulp(schAlpha.sinh()), FastMath.ulp(schAlpha.cosh())),
1131                                                 FastMath.max(FastMath.ulp(schBeta.sinh()), FastMath.ulp(schBeta.cosh())));
1132             assertEquals(schAlphaPlusBeta.sinh(), schSum.sinh(), tol);
1133             assertEquals(schAlphaPlusBeta.cosh(), schSum.cosh(), tol);
1134         }
1135     }
1136 
1137     @Test
1138     void testSinhCoshdifference() {
1139         final RandomGenerator random = new Well19937a(0x219fd680c53974bbL);
1140         for (int i = 0; i < 1000000; ++i) {
1141             final double alpha = 2.0 * (2.0 * random.nextDouble() - 1.0);
1142             final double beta  = 2.0 * (2.0 * random.nextDouble() - 1.0);
1143             final SinhCosh schAlpha          = FastMath.sinhCosh(alpha);
1144             final SinhCosh schBeta           = FastMath.sinhCosh(beta);
1145             final SinhCosh schAlphaMinusBeta = FastMath.sinhCosh(alpha - beta);
1146             final SinhCosh schDifference     = SinhCosh.difference(schAlpha, schBeta);
1147             final double tol = 8 * FastMath.max(FastMath.max(FastMath.ulp(schAlpha.sinh()), FastMath.ulp(schAlpha.cosh())),
1148                                                 FastMath.max(FastMath.ulp(schBeta.sinh()), FastMath.ulp(schBeta.cosh())));
1149             assertEquals(schAlphaMinusBeta.sinh(), schDifference.sinh(), tol);
1150             assertEquals(schAlphaMinusBeta.cosh(), schDifference.cosh(), tol);
1151         }
1152     }
1153 
1154     @Test
1155     void testSinhAccuracy() {
1156         double maxerrulp = 0.0;
1157 
1158         for (int i=0; i<10000; i++) {
1159             double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble();
1160 
1161             double tst = FastMath.sinh(x);
1162             double ref = sinh(field.newDfp(x)).toDouble();
1163             double err = (tst - ref) / ref;
1164 
1165             if (err != 0) {
1166                 double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1167                 double errulp = field.newDfp(tst).subtract(sinh(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
1168                 //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp);
1169                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1170             }
1171         }
1172 
1173         assertTrue(maxerrulp < MAX_ERROR_ULP, "sinh() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1174     }
1175 
1176     @Test
1177     void testCoshAccuracy() {
1178         double maxerrulp = 0.0;
1179 
1180         for (int i=0; i<10000; i++) {
1181             double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble();
1182 
1183             double tst = FastMath.cosh(x);
1184             double ref = cosh(field.newDfp(x)).toDouble();
1185             double err = (tst - ref) / ref;
1186 
1187             if (err != 0) {
1188                 double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1189                 double errulp = field.newDfp(tst).subtract(cosh(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
1190                 //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp);
1191                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1192             }
1193         }
1194 
1195         assertTrue(maxerrulp < MAX_ERROR_ULP, "cosh() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1196     }
1197 
1198     @Test
1199     void testTanhAccuracy() {
1200         double maxerrulp = 0.0;
1201 
1202         for (int i=0; i<10000; i++) {
1203             double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble();
1204 
1205             double tst = FastMath.tanh(x);
1206             double ref = tanh(field.newDfp(x)).toDouble();
1207             double err = (tst - ref) / ref;
1208 
1209             if (err != 0) {
1210                 double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1211                 double errulp = field.newDfp(tst).subtract(tanh(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
1212                 //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp);
1213                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1214             }
1215         }
1216 
1217         assertTrue(maxerrulp < MAX_ERROR_ULP, "tanh() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1218     }
1219 
1220     @Test
1221     void testCbrtAccuracy() {
1222         double maxerrulp = 0.0;
1223 
1224         for (int i=0; i<10000; i++) {
1225             double x = ((generator.nextDouble() * 200.0) - 100.0) * generator.nextDouble();
1226 
1227             double tst = FastMath.cbrt(x);
1228             double ref = cbrt(field.newDfp(x)).toDouble();
1229             double err = (tst - ref) / ref;
1230 
1231             if (err != 0) {
1232                 double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1233                 double errulp = field.newDfp(tst).subtract(cbrt(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble();
1234                 //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp);
1235                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1236             }
1237         }
1238 
1239         assertTrue(maxerrulp < MAX_ERROR_ULP, "cbrt() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1240     }
1241 
1242     private Dfp cbrt(Dfp x) {
1243         boolean negative=false;
1244 
1245         if (x.lessThan(field.getZero())) {
1246             negative = true;
1247             x = x.negate();
1248         }
1249 
1250         Dfp y = DfpMath.pow(x, field.getOne().divide(3));
1251 
1252         if (negative) {
1253             y = y.negate();
1254         }
1255 
1256         return y;
1257     }
1258 
1259     @Test
1260     void testToDegrees() {
1261         double maxerrulp = 0.0;
1262         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
1263             double x = generator.nextDouble();
1264             double tst = field.newDfp(x).multiply(180).divide(field.getPi()).toDouble();
1265             double ref = FastMath.toDegrees(x);
1266             double err = (tst - ref) / ref;
1267 
1268             if (err != 0) {
1269                 double ulp = Math.abs(ref -
1270                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1271                 double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x)).subtract(field.getOne())).divide(field.newDfp(ulp)).toDouble();
1272 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
1273 
1274                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1275             }
1276         }
1277         assertTrue(maxerrulp < MAX_ERROR_ULP, "toDegrees() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1278     }
1279 
1280     @Test
1281     void testToRadians() {
1282         double maxerrulp = 0.0;
1283         for (int i = 0; i < NUMBER_OF_TRIALS; i++) {
1284             double x = generator.nextDouble();
1285             double tst = field.newDfp(x).multiply(field.getPi()).divide(180).toDouble();
1286             double ref = FastMath.toRadians(x);
1287             double err = (tst - ref) / ref;
1288 
1289             if (err != 0) {
1290                 double ulp = Math.abs(ref -
1291                                       Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1)));
1292                 double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x)).subtract(field.getOne())).divide(field.newDfp(ulp)).toDouble();
1293 //                System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp);
1294 
1295                 maxerrulp = Math.max(maxerrulp, Math.abs(errulp));
1296             }
1297         }
1298 
1299         assertTrue(maxerrulp < MAX_ERROR_ULP, "toRadians() had errors in excess of " + MAX_ERROR_ULP + " ULP");
1300     }
1301 
1302     @Test
1303     void testNextAfter() {
1304         // 0x402fffffffffffff 0x404123456789abcd -> 4030000000000000
1305         assertEquals(16.0, FastMath.nextUp(15.999999999999998), 0.0);
1306 
1307         // 0xc02fffffffffffff 0x404123456789abcd -> c02ffffffffffffe
1308         assertEquals(-15.999999999999996, FastMath.nextAfter(-15.999999999999998, 34.27555555555555), 0.0);
1309 
1310         // 0x402fffffffffffff 0x400123456789abcd -> 402ffffffffffffe
1311         assertEquals(15.999999999999996, FastMath.nextDown(15.999999999999998), 0.0);
1312 
1313         // 0xc02fffffffffffff 0x400123456789abcd -> c02ffffffffffffe
1314         assertEquals(-15.999999999999996, FastMath.nextAfter(-15.999999999999998, 2.142222222222222), 0.0);
1315 
1316         // 0x4020000000000000 0x404123456789abcd -> 4020000000000001
1317         assertEquals(8.000000000000002, FastMath.nextAfter(8.0, 34.27555555555555), 0.0);
1318 
1319         // 0xc020000000000000 0x404123456789abcd -> c01fffffffffffff
1320         assertEquals(-7.999999999999999, FastMath.nextAfter(-8.0, 34.27555555555555), 0.0);
1321 
1322         // 0x4020000000000000 0x400123456789abcd -> 401fffffffffffff
1323         assertEquals(7.999999999999999, FastMath.nextAfter(8.0, 2.142222222222222), 0.0);
1324 
1325         // 0xc020000000000000 0x400123456789abcd -> c01fffffffffffff
1326         assertEquals(-7.999999999999999, FastMath.nextAfter(-8.0, 2.142222222222222), 0.0);
1327 
1328         // 0x3f2e43753d36a223 0x3f2e43753d36a224 -> 3f2e43753d36a224
1329         assertEquals(2.308922399667661E-4, FastMath.nextAfter(2.3089223996676606E-4, 2.308922399667661E-4), 0.0);
1330 
1331         // 0x3f2e43753d36a223 0x3f2e43753d36a223 -> 3f2e43753d36a223
1332         assertEquals(2.3089223996676606E-4, FastMath.nextAfter(2.3089223996676606E-4, 2.3089223996676606E-4), 0.0);
1333 
1334         // 0x3f2e43753d36a223 0x3f2e43753d36a222 -> 3f2e43753d36a222
1335         assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, 2.3089223996676603E-4), 0.0);
1336 
1337         // 0x3f2e43753d36a223 0xbf2e43753d36a224 -> 3f2e43753d36a222
1338         assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, -2.308922399667661E-4), 0.0);
1339 
1340         // 0x3f2e43753d36a223 0xbf2e43753d36a223 -> 3f2e43753d36a222
1341         assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, -2.3089223996676606E-4), 0.0);
1342 
1343         // 0x3f2e43753d36a223 0xbf2e43753d36a222 -> 3f2e43753d36a222
1344         assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, -2.3089223996676603E-4), 0.0);
1345 
1346         // 0xbf2e43753d36a223 0x3f2e43753d36a224 -> bf2e43753d36a222
1347         assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, 2.308922399667661E-4), 0.0);
1348 
1349         // 0xbf2e43753d36a223 0x3f2e43753d36a223 -> bf2e43753d36a222
1350         assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, 2.3089223996676606E-4), 0.0);
1351 
1352         // 0xbf2e43753d36a223 0x3f2e43753d36a222 -> bf2e43753d36a222
1353         assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, 2.3089223996676603E-4), 0.0);
1354 
1355         // 0xbf2e43753d36a223 0xbf2e43753d36a224 -> bf2e43753d36a224
1356         assertEquals(-2.308922399667661E-4, FastMath.nextAfter(-2.3089223996676606E-4, -2.308922399667661E-4), 0.0);
1357 
1358         // 0xbf2e43753d36a223 0xbf2e43753d36a223 -> bf2e43753d36a223
1359         assertEquals(-2.3089223996676606E-4, FastMath.nextAfter(-2.3089223996676606E-4, -2.3089223996676606E-4), 0.0);
1360 
1361         // 0xbf2e43753d36a223 0xbf2e43753d36a222 -> bf2e43753d36a222
1362         assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, -2.3089223996676603E-4), 0.0);
1363 
1364     }
1365 
1366     @Test
1367     void testDoubleNextAfterSpecialCases() {
1368         assertEquals(-Double.MAX_VALUE,FastMath.nextAfter(Double.NEGATIVE_INFINITY, 0D), 0D);
1369         assertEquals(Double.MAX_VALUE,FastMath.nextAfter(Double.POSITIVE_INFINITY, 0D), 0D);
1370         assertEquals(Double.NaN,FastMath.nextAfter(Double.NaN, 0D), 0D);
1371         assertEquals(Double.POSITIVE_INFINITY,FastMath.nextAfter(Double.MAX_VALUE, Double.POSITIVE_INFINITY), 0D);
1372         assertEquals(Double.NEGATIVE_INFINITY,FastMath.nextAfter(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY), 0D);
1373         assertEquals(Double.MIN_VALUE, FastMath.nextAfter(0D, 1D), 0D);
1374         assertEquals(-Double.MIN_VALUE, FastMath.nextAfter(0D, -1D), 0D);
1375         assertEquals(0D, FastMath.nextAfter(Double.MIN_VALUE, -1), 0D);
1376         assertEquals(0D, FastMath.nextAfter(-Double.MIN_VALUE, 1), 0D);
1377     }
1378 
1379     @Test
1380     void testFloatNextAfterSpecialCases() {
1381         assertEquals(-Float.MAX_VALUE,FastMath.nextAfter(Float.NEGATIVE_INFINITY, 0F), 0F);
1382         assertEquals(Float.MAX_VALUE,FastMath.nextAfter(Float.POSITIVE_INFINITY, 0F), 0F);
1383         assertEquals(Float.NaN,FastMath.nextAfter(Float.NaN, 0F), 0F);
1384         assertEquals(Float.POSITIVE_INFINITY,FastMath.nextUp(Float.MAX_VALUE), 0F);
1385         assertEquals(Float.NEGATIVE_INFINITY,FastMath.nextDown(-Float.MAX_VALUE), 0F);
1386         assertEquals(Float.MIN_VALUE, FastMath.nextAfter(0F, 1F), 0F);
1387         assertEquals(-Float.MIN_VALUE, FastMath.nextAfter(0F, -1F), 0F);
1388         assertEquals(0F, FastMath.nextAfter(Float.MIN_VALUE, -1F), 0F);
1389         assertEquals(0F, FastMath.nextAfter(-Float.MIN_VALUE, 1F), 0F);
1390     }
1391 
1392     @Test
1393     void testClampInt() {
1394         assertEquals( 3, FastMath.clamp(-17,  3, 4));
1395         assertEquals( 4, FastMath.clamp(+17,  3, 4));
1396         assertEquals( 0, FastMath.clamp(-17,  0, 4));
1397         assertEquals( 0, FastMath.clamp(+17, -3, 0));
1398     }
1399 
1400     @Test
1401     void testClampLong() {
1402         assertEquals(3L, FastMath.clamp(-17L, 3L, 4L));
1403         assertEquals(4L, FastMath.clamp(+17L, 3L, 4L));
1404         assertEquals(0L, FastMath.clamp(-17L, 0L, 4L));
1405         assertEquals(0L, FastMath.clamp(+17L, -3L, 0L));
1406     }
1407 
1408     @Test
1409     void testClampLongInt() {
1410         assertEquals( 3, FastMath.clamp(-17L, 3, 4));
1411         assertEquals( 4, FastMath.clamp(+17L, 3, 4));
1412         assertEquals( 0, FastMath.clamp(-17L, 0, 4));
1413         assertEquals( 0, FastMath.clamp(+17L, -3, 0));
1414     }
1415 
1416     @Test
1417     void testClampFloat() {
1418         assertEquals( 3.0f, FastMath.clamp(-17.0f, 3.0f, 4.0f), 1.0e-15f);
1419         assertEquals( 4.0f, FastMath.clamp(+17.0f, 3.0f, 4.0f), 1.0e-15f);
1420         assertEquals( 0.0f, FastMath.clamp(-17.0f, -0.0f, 4.0f), 1.0e-15f);
1421         assertEquals(-1.0f, FastMath.copySign(1.0f, FastMath.clamp(-17.0f, -0.0f, 4.0f)), 1.0e-15f);
1422         assertEquals( 0.0f, FastMath.clamp(-17.0f, +0.0f, 4.0f), 1.0e-15f);
1423         assertEquals(+1.0f, FastMath.copySign(1.0f, FastMath.clamp(-17.0f, +0.0f, 4.0f)), 1.0e-15f);
1424         assertEquals( 0.0f, FastMath.clamp(+17.0f, -3.0f, -0.0f), 1.0e-15f);
1425         assertEquals(-1.0f, FastMath.copySign(1.0f, FastMath.clamp(+17.0f, -3.0f, -0.0f)), 1.0e-15f);
1426         assertEquals( 0.0f, FastMath.clamp(+17.0f, -3.0f, +0.0f), 1.0e-15f);
1427         assertEquals(+1.0f, FastMath.copySign(1.0f, FastMath.clamp(+17.0f, -3.0f, +0.0f)), 1.0e-15f);
1428     }
1429 
1430     @Test
1431     void testClampDouble() {
1432         assertEquals( 3.0, FastMath.clamp(-17.0, 3.0, 4.0), 1.0e-15);
1433         assertEquals( 4.0, FastMath.clamp(+17.0, 3.0, 4.0), 1.0e-15);
1434         assertEquals( 0.0, FastMath.clamp(-17.0, -0.0, 4.0), 1.0e-15);
1435         assertEquals(-1.0, FastMath.copySign(1.0, FastMath.clamp(-17.0, -0.0, 4.0)), 1.0e-15);
1436         assertEquals( 0.0, FastMath.clamp(-17.0, +0.0, 4.0), 1.0e-15);
1437         assertEquals(+1.0, FastMath.copySign(1.0, FastMath.clamp(-17.0, +0.0, 4.0)), 1.0e-15);
1438         assertEquals( 0.0, FastMath.clamp(+17.0, -3.0, -0.0), 1.0e-15);
1439         assertEquals(-1.0, FastMath.copySign(1.0, FastMath.clamp(+17.0, -3.0, -0.0)), 1.0e-15);
1440         assertEquals( 0.0, FastMath.clamp(+17.0, -3.0, +0.0), 1.0e-15);
1441         assertEquals(+1.0, FastMath.copySign(1.0, FastMath.clamp(+17.0, -3.0, +0.0)), 1.0e-15);
1442     }
1443 
1444     @Test
1445     void testDoubleScalbSpecialCases() {
1446         assertEquals(0d,                       FastMath.scalb(0d, 1100),                0d);
1447         assertEquals(Double.POSITIVE_INFINITY,  FastMath.scalb(Double.POSITIVE_INFINITY, 1100), 0);
1448         assertEquals(Double.NEGATIVE_INFINITY,  FastMath.scalb(Double.NEGATIVE_INFINITY, 1100), 0);
1449         assertTrue(Double.isNaN(FastMath.scalb(Double.NaN, 1100)));
1450         assertEquals(2.5269841324701218E-175,  FastMath.scalb(2.2250738585072014E-308, 442), 0D);
1451         assertEquals(1.307993905256674E297,    FastMath.scalb(1.1102230246251565E-16, 1040), 0D);
1452         assertEquals(7.2520887996488946E-217,  FastMath.scalb(Double.MIN_VALUE,        356), 0D);
1453         assertEquals(8.98846567431158E307,     FastMath.scalb(Double.MIN_VALUE,       2097), 0D);
1454         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb(Double.MIN_VALUE,       2098), 0D);
1455         assertEquals(1.1125369292536007E-308,  FastMath.scalb(2.225073858507201E-308,   -1), 0D);
1456         assertEquals(1.0E-323,                 FastMath.scalb(Double.MAX_VALUE,      -2097), 0D);
1457         assertEquals(Double.MIN_VALUE,         FastMath.scalb(Double.MAX_VALUE,      -2098), 0D);
1458         assertEquals(0,                        FastMath.scalb(Double.MAX_VALUE,      -2099), 0D);
1459         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb(Double.POSITIVE_INFINITY, -1000000), 0D);
1460         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.1102230246251565E-16, 1078), 0D);
1461         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16, 1078), 0D);
1462         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16,  1079), 0D);
1463         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2047), 0D);
1464         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2048), 0D);
1465         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.7976931348623157E308,  2147483647), 0D);
1466         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.7976931348623157E308,  2147483647), 0D);
1467         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16,  2147483647), 0D);
1468         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.1102230246251565E-16,  2147483647), 0D);
1469         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2147483647), 0D);
1470         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 2.2250738585072014E-308, 2147483647), 0D);
1471         assertEquals(0L, Double.doubleToRawLongBits(FastMath.scalb(4.0, -2099)));
1472         assertEquals(1L << 63, Double.doubleToRawLongBits(FastMath.scalb(-4.0, -2099)));
1473         assertEquals(0L, Double.doubleToRawLongBits(FastMath.scalb(0.0, 12)));
1474         assertEquals(1L << 63, Double.doubleToRawLongBits(FastMath.scalb(-0.0, 12)));
1475         assertTrue(Double.isNaN(FastMath.scalb(Double.NaN, 12)));
1476         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb(Double.POSITIVE_INFINITY, 12), 1.0);
1477         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(Double.NEGATIVE_INFINITY, 12), 1.0);
1478         assertEquals(0x1.2345p-1022, FastMath.scalb(0x1.2345p28, -1050), 0x1.0p-1070);
1479         assertEquals(0L, Double.doubleToRawLongBits(FastMath.scalb(0x1.2345p28, -1104)));
1480         assertEquals(1L << 63, Double.doubleToRawLongBits(FastMath.scalb(-0x1.2345p28, -1104)));
1481         assertEquals(0x1.2345p27, FastMath.scalb(0x1.2345p-1023, 1050), 0x1.0p-25);
1482         assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 0x1.2345p-1023, 2047), 1.0);
1483         assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-0x1.2345p-1023, 2047), 1.0);
1484         assertEquals(0x1p-1057, FastMath.scalb(0x1.0p23, -1080), 0x1.0p-1073);
1485     }
1486 
1487     @Test
1488     void testFloatScalbSpecialCases() {
1489         assertEquals(0f,                       FastMath.scalb(0f, 130),                0F);
1490         assertEquals(Float.POSITIVE_INFINITY,  FastMath.scalb(Float.POSITIVE_INFINITY, 130), 0F);
1491         assertEquals(Float.NEGATIVE_INFINITY,  FastMath.scalb(Float.NEGATIVE_INFINITY, 130), 0F);
1492         assertTrue(Float.isNaN(FastMath.scalb(Float.NaN, 130)));
1493         assertEquals(0f,                       FastMath.scalb(Float.MIN_VALUE,  -30), 0F);
1494         assertEquals(2 * Float.MIN_VALUE,      FastMath.scalb(Float.MIN_VALUE,    1), 0F);
1495         assertEquals(7.555786e22f,             FastMath.scalb(Float.MAX_VALUE,  -52), 0F);
1496         assertEquals(1.7014118e38f,            FastMath.scalb(Float.MIN_VALUE,  276), 0F);
1497         assertEquals(Float.POSITIVE_INFINITY,  FastMath.scalb(Float.MIN_VALUE,  277), 0F);
1498         assertEquals(5.8774718e-39f,           FastMath.scalb(1.1754944e-38f,    -1), 0F);
1499         assertEquals(2 * Float.MIN_VALUE,      FastMath.scalb(Float.MAX_VALUE, -276), 0F);
1500         assertEquals(Float.MIN_VALUE,          FastMath.scalb(Float.MAX_VALUE, -277), 0F);
1501         assertEquals(0,                        FastMath.scalb(Float.MAX_VALUE, -278), 0F);
1502         assertEquals(Float.POSITIVE_INFINITY,  FastMath.scalb(Float.POSITIVE_INFINITY, -1000000), 0F);
1503         assertEquals(-3.13994498e38f,          FastMath.scalb(-1.1e-7f,         151), 0F);
1504         assertEquals(Float.NEGATIVE_INFINITY,  FastMath.scalb(-1.1e-7f,         152), 0F);
1505         assertEquals(Float.POSITIVE_INFINITY,  FastMath.scalb(3.4028235E38f,  2147483647), 0F);
1506         assertEquals(Float.NEGATIVE_INFINITY,  FastMath.scalb(-3.4028235E38f, 2147483647), 0F);
1507         assertEquals(0, Float.floatToRawIntBits(FastMath.scalb(4.0f, -278)));
1508         assertEquals(1 << 31, Float.floatToRawIntBits(FastMath.scalb(-4.0f, -278)));
1509         assertEquals(0, Float.floatToRawIntBits(FastMath.scalb(0.0f, 12)));
1510         assertEquals(1 << 31, Float.floatToRawIntBits(FastMath.scalb(-0.0f, 12)));
1511         assertTrue(Float.isNaN(FastMath.scalb(Float.NaN, 12)));
1512         assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb(Float.POSITIVE_INFINITY, 12), 1.0);
1513         assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(Float.NEGATIVE_INFINITY, 12), 1.0);
1514         assertEquals(0x1.2345p-106f, FastMath.scalb(0x1.2345p28f, -134), 0x1.0p-130f);
1515         assertEquals(0,       Float.floatToRawIntBits(FastMath.scalb( 0x1.2345p28f, -179)));
1516         assertEquals(1 << 31, Float.floatToRawIntBits(FastMath.scalb(-0x1.2345p28f, -179)));
1517         assertEquals(0x1.2345p127f, FastMath.scalb(0x1.2345p-123f, 250), 0x1.0p3f);
1518         assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb( 0x1.2345p-127f, 255), 1.0f);
1519         assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-0x1.2345p-127f, 255), 1.0f);
1520         assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb( 0x1.2345p-123f, 252), 1.0f);
1521         assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-0x1.2345p-123f, 252), 1.0f);
1522         assertEquals(0x1p-130f, FastMath.scalb(0x1.0p23f, -153), 0x1.0p-148f);
1523     }
1524 
1525     private boolean compareClassMethods(Class<?> class1, Class<?> class2){
1526         boolean allfound = true;
1527         for(Method method1 : class1.getDeclaredMethods()){
1528             if (Modifier.isPublic(method1.getModifiers())){
1529                 Type []params = method1.getGenericParameterTypes();
1530                 try {
1531                     class2.getDeclaredMethod(method1.getName(), (Class[]) params);
1532                 } catch (NoSuchMethodException e) {
1533                     allfound = false;
1534                     System.out.println(class2.getSimpleName()+" does not implement: "+method1);
1535                 }
1536             }
1537         }
1538         return allfound;
1539     }
1540 
1541     @Test
1542     void checkMissingFastMathClasses() {
1543         boolean ok = compareClassMethods(StrictMath.class, FastMath.class);
1544         assertTrue(ok, "FastMath should implement all StrictMath methods");
1545     }
1546 
1547     @Test
1548     void testUlpDouble() {
1549         assertTrue(Double.isNaN(FastMath.ulp(Double.NaN)));
1550         assertEquals(Double.POSITIVE_INFINITY, FastMath.ulp(Double.POSITIVE_INFINITY), 1.0);
1551         assertEquals(Double.POSITIVE_INFINITY, FastMath.ulp(Double.NEGATIVE_INFINITY), 1.0);
1552         assertEquals(0.0, FastMath.ulp(+0.0), Precision.SAFE_MIN);
1553         assertEquals(0.0, FastMath.ulp(-0.0), Precision.SAFE_MIN);
1554         assertEquals(0x1.0p-53, FastMath.ulp(0x1.fffffffffffffp-1), 0x1.0p-100);
1555         assertEquals(0x1.0p-52, FastMath.ulp(+1.0), 0x1.0p-100);
1556         assertEquals(0x1.0p-52, FastMath.ulp(-1.0), 0x1.0p-100);
1557     }
1558 
1559     @Test
1560     void testUlpFloat() {
1561         assertTrue(Float.isNaN(FastMath.ulp(Float.NaN)));
1562         assertEquals(Float.POSITIVE_INFINITY, FastMath.ulp(Float.POSITIVE_INFINITY), 1.0f);
1563         assertEquals(Float.POSITIVE_INFINITY, FastMath.ulp(Float.NEGATIVE_INFINITY), 1.0f);
1564         assertEquals(0.0f, FastMath.ulp(+0.0f), 1.0e-8f);
1565         assertEquals(0.0f, FastMath.ulp(-0.0f), 1.0e-8f);
1566         assertEquals(0x1.0p-24f, FastMath.ulp(0x1.fffffcp-1f), 0x1.0p-50f);
1567         assertEquals(0x1.0p-23f, FastMath.ulp(+1.0f), 0x1.0p-50f);
1568         assertEquals(0x1.0p-23f, FastMath.ulp(-1.0f), 0x1.0p-50f);
1569     }
1570 
1571     @Test
1572     void testCopySignDouble() {
1573 
1574         assertEquals(-2.0, FastMath.copySign(-2.0, -5.0),                     1.0e-10);
1575         assertEquals(-2.0, FastMath.copySign(+2.0, -5.0),                     1.0e-10);
1576         assertEquals(+2.0, FastMath.copySign(-2.0, +5.0),                     1.0e-10);
1577         assertEquals(+2.0, FastMath.copySign(+2.0, +5.0),                     1.0e-10);
1578         assertEquals(-2.0, FastMath.copySign(-2.0, Double.NEGATIVE_INFINITY), 1.0e-10);
1579         assertEquals(-2.0, FastMath.copySign(+2.0, Double.NEGATIVE_INFINITY), 1.0e-10);
1580         assertEquals(+2.0, FastMath.copySign(-2.0, Double.POSITIVE_INFINITY), 1.0e-10);
1581         assertEquals(+2.0, FastMath.copySign(+2.0, Double.POSITIVE_INFINITY), 1.0e-10);
1582         assertEquals(+2.0, FastMath.copySign(-2.0, Double.NaN),               1.0e-10);
1583         assertEquals(+2.0, FastMath.copySign(-2.0, Double.NaN),               1.0e-10);
1584         assertEquals(+2.0, FastMath.copySign(-2.0, -Double.NaN),              1.0e-10);
1585         assertEquals(+2.0, FastMath.copySign(-2.0, -Double.NaN),              1.0e-10);
1586         assertEquals(-2.0, FastMath.copySign(-2.0, -0.0),                     1.0e-10);
1587         assertEquals(-2.0, FastMath.copySign(+2.0, -0.0),                     1.0e-10);
1588         assertEquals(+2.0, FastMath.copySign(-2.0, +0.0),                     1.0e-10);
1589         assertEquals(+2.0, FastMath.copySign(+2.0, +0.0),                     1.0e-10);
1590 
1591         assertEquals(-3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, -5.0)),                     1.0e-10);
1592         assertEquals(-3.0, FastMath.copySign(+3.0, FastMath.copySign(+0.0, -5.0)),                     1.0e-10);
1593         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, +5.0)),                     1.0e-10);
1594         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(+0.0, +5.0)),                     1.0e-10);
1595         assertEquals(-3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, Double.NEGATIVE_INFINITY)), 1.0e-10);
1596         assertEquals(-3.0, FastMath.copySign(+3.0, FastMath.copySign(+0.0, Double.NEGATIVE_INFINITY)), 1.0e-10);
1597         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, Double.POSITIVE_INFINITY)), 1.0e-10);
1598         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(+0.0, Double.POSITIVE_INFINITY)), 1.0e-10);
1599         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, Double.NaN)),               1.0e-10);
1600         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, Double.NaN)),               1.0e-10);
1601         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, -Double.NaN)),              1.0e-10);
1602         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, -Double.NaN)),              1.0e-10);
1603         assertEquals(-3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, -0.0)),                     1.0e-10);
1604         assertEquals(-3.0, FastMath.copySign(+3.0, FastMath.copySign(+0.0, -0.0)),                     1.0e-10);
1605         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(-0.0, +0.0)),                     1.0e-10);
1606         assertEquals(+3.0, FastMath.copySign(+3.0, FastMath.copySign(+0.0, +0.0)),                     1.0e-10);
1607 
1608     }
1609 
1610     @Test
1611     void testCopySignFloat() {
1612 
1613         assertEquals(-2.0f, FastMath.copySign(-2.0f, -5.0f),                   1.0e-10f);
1614         assertEquals(-2.0f, FastMath.copySign(+2.0f, -5.0f),                   1.0e-10f);
1615         assertEquals(+2.0f, FastMath.copySign(-2.0f, +5.0f),                   1.0e-10f);
1616         assertEquals(+2.0f, FastMath.copySign(+2.0f, +5.0f),                   1.0e-10f);
1617         assertEquals(-2.0f, FastMath.copySign(-2.0f, Float.NEGATIVE_INFINITY), 1.0e-10f);
1618         assertEquals(-2.0f, FastMath.copySign(+2.0f, Float.NEGATIVE_INFINITY), 1.0e-10f);
1619         assertEquals(+2.0f, FastMath.copySign(-2.0f, Float.POSITIVE_INFINITY), 1.0e-10f);
1620         assertEquals(+2.0f, FastMath.copySign(+2.0f, Float.POSITIVE_INFINITY), 1.0e-10f);
1621         assertEquals(+2.0f, FastMath.copySign(-2.0f, Float.NaN),               1.0e-10f);
1622         assertEquals(+2.0f, FastMath.copySign(-2.0f, Float.NaN),               1.0e-10f);
1623         assertEquals(+2.0f, FastMath.copySign(-2.0f, -Float.NaN),              1.0e-10f);
1624         assertEquals(+2.0f, FastMath.copySign(-2.0f, -Float.NaN),              1.0e-10f);
1625         assertEquals(-2.0f, FastMath.copySign(-2.0f, -0.0f),                   1.0e-10f);
1626         assertEquals(-2.0f, FastMath.copySign(+2.0f, -0.0f),                   1.0e-10f);
1627         assertEquals(+2.0f, FastMath.copySign(-2.0f, +0.0f),                   1.0e-10f);
1628         assertEquals(+2.0f, FastMath.copySign(+2.0f, +0.0f),                   1.0e-10f);
1629 
1630         assertEquals(-3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, -5.0f)),                   1.0e-10f);
1631         assertEquals(-3.0f, FastMath.copySign(+3.0f, FastMath.copySign(+0.0f, -5.0f)),                   1.0e-10f);
1632         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, +5.0f)),                   1.0e-10f);
1633         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(+0.0f, +5.0f)),                   1.0e-10f);
1634         assertEquals(-3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, Float.NEGATIVE_INFINITY)), 1.0e-10f);
1635         assertEquals(-3.0f, FastMath.copySign(+3.0f, FastMath.copySign(+0.0f, Float.NEGATIVE_INFINITY)), 1.0e-10f);
1636         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, Float.POSITIVE_INFINITY)), 1.0e-10f);
1637         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(+0.0f, Float.POSITIVE_INFINITY)), 1.0e-10f);
1638         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, Float.NaN)),               1.0e-10f);
1639         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, Float.NaN)),               1.0e-10f);
1640         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, -Float.NaN)),              1.0e-10f);
1641         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, -Float.NaN)),              1.0e-10f);
1642         assertEquals(-3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, -0.0f)),                   1.0e-10f);
1643         assertEquals(-3.0f, FastMath.copySign(+3.0f, FastMath.copySign(+0.0f, -0.0f)),                   1.0e-10f);
1644         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(-0.0f, +0.0f)),                   1.0e-10f);
1645         assertEquals(+3.0f, FastMath.copySign(+3.0f, FastMath.copySign(+0.0f, +0.0f)),                   1.0e-10f);
1646 
1647     }
1648 
1649     @Test
1650     void testSignumDouble() {
1651         final double delta = 0.0;
1652         assertEquals(1.0, FastMath.signum(2.0), delta);
1653         assertEquals(0.0, FastMath.signum(0.0), delta);
1654         assertEquals(-1.0, FastMath.signum(-2.0), delta);
1655         UnitTestUtils.customAssertSame(-0. / 0., FastMath.signum(Double.NaN));
1656     }
1657 
1658     @Test
1659     void testSignumFloat() {
1660         final float delta = 0.0F;
1661         assertEquals(1.0F, FastMath.signum(2.0F), delta);
1662         assertEquals(0.0F, FastMath.signum(0.0F), delta);
1663         assertEquals(-1.0F, FastMath.signum(-2.0F), delta);
1664         UnitTestUtils.customAssertSame(Float.NaN, FastMath.signum(Float.NaN));
1665     }
1666 
1667     @Test
1668     void testLogWithBase() {
1669         assertEquals(2.0, FastMath.log(2, 4), 0);
1670         assertEquals(3.0, FastMath.log(2, 8), 0);
1671         assertTrue(Double.isNaN(FastMath.log(-1, 1)));
1672         assertTrue(Double.isNaN(FastMath.log(1, -1)));
1673         assertTrue(Double.isNaN(FastMath.log(0, 0)));
1674         assertEquals(0, FastMath.log(0, 10), 0);
1675         assertEquals(Double.NEGATIVE_INFINITY, FastMath.log(10, 0), 0);
1676     }
1677 
1678     @Test
1679     void testIndicatorDouble() {
1680         double delta = 0.0;
1681         assertEquals(1.0, FastMath.copySign(1d, 2.0), delta);
1682         assertEquals(1.0, FastMath.copySign(1d, 0.0), delta);
1683         assertEquals(-1.0, FastMath.copySign(1d, -0.0), delta);
1684         assertEquals(1.0, FastMath.copySign(1d, Double.POSITIVE_INFINITY), delta);
1685         assertEquals(-1.0, FastMath.copySign(1d, Double.NEGATIVE_INFINITY), delta);
1686         assertEquals(1.0, FastMath.copySign(1d, Double.NaN), delta);
1687         assertEquals(-1.0, FastMath.copySign(1d, -2.0), delta);
1688     }
1689 
1690     @Test
1691     void testIndicatorFloat() {
1692         float delta = 0.0F;
1693         assertEquals(1.0F, FastMath.copySign(1d, 2.0F), delta);
1694         assertEquals(1.0F, FastMath.copySign(1d, 0.0F), delta);
1695         assertEquals(-1.0F, FastMath.copySign(1d, -0.0F), delta);
1696         assertEquals(1.0F, FastMath.copySign(1d, Float.POSITIVE_INFINITY), delta);
1697         assertEquals(-1.0F, FastMath.copySign(1d, Float.NEGATIVE_INFINITY), delta);
1698         assertEquals(1.0F, FastMath.copySign(1d, Float.NaN), delta);
1699         assertEquals(-1.0F, FastMath.copySign(1d, -2.0F), delta);
1700     }
1701 
1702     @Test
1703     void testIntPow() {
1704         final int maxExp = 300;
1705         DfpField field = new DfpField(40);
1706         final double base = 1.23456789;
1707         Dfp baseDfp = field.newDfp(base);
1708         Dfp dfpPower = field.getOne();
1709         for (int i = 0; i < maxExp; i++) {
1710             assertEquals(dfpPower.toDouble(), FastMath.pow(base, i),
1711                          0.6 * FastMath.ulp(dfpPower.toDouble()),
1712                          "exp=" + i);
1713             dfpPower = dfpPower.multiply(baseDfp);
1714         }
1715     }
1716 
1717     @Test
1718     void testIntPowHuge() {
1719         assertTrue(Double.isInfinite(FastMath.pow(FastMath.scalb(1.0, 500), 4)));
1720     }
1721 
1722     // This test must finish in finite time.
1723     @Test
1724     @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
1725     void testIntPowLongMinValue() {
1726         assertEquals(1.0, FastMath.pow(1.0, Long.MIN_VALUE), 1.0);
1727     }
1728 
1729     @Test
1730     @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
1731     void testIntPowSpecialCases() {
1732         final double EXACT = 1.0;
1733         final double[] DOUBLES = new double[]
1734             {
1735                 Double.NEGATIVE_INFINITY, -0.0, Double.NaN, 0.0, Double.POSITIVE_INFINITY,
1736                 Long.MIN_VALUE, Integer.MIN_VALUE, Short.MIN_VALUE, Byte.MIN_VALUE,
1737                 -(double)Long.MIN_VALUE, -(double)Integer.MIN_VALUE, -(double)Short.MIN_VALUE, -(double)Byte.MIN_VALUE,
1738                 Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE, Long.MAX_VALUE,
1739                 -Byte.MAX_VALUE, -Short.MAX_VALUE, -Integer.MAX_VALUE, -Long.MAX_VALUE,
1740                 Float.MAX_VALUE, Double.MAX_VALUE, Double.MIN_VALUE, Float.MIN_VALUE,
1741                 -Float.MAX_VALUE, -Double.MAX_VALUE, -Double.MIN_VALUE, -Float.MIN_VALUE,
1742                 0.5, 0.1, 0.2, 0.8, 1.1, 1.2, 1.5, 1.8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.3, 2.2, 2.5, 2.8, 33.0, 33.1, 33.5, 33.8, 10.0, 300.0, 400.0, 500.0,
1743                 -0.5, -0.1, -0.2, -0.8, -1.1, -1.2, -1.5, -1.8, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0, -1.3, -2.2, -2.5, -2.8, -33.0, -33.1, -33.5, -33.8, -10.0, -300.0, -400.0, -500.0
1744             };
1745 
1746         final long[]
1747                         INTS = new long[]{Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2, Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2, 0, 1, 2, 3, 5, 8, 10, 20, 100, 300, 500, -1, -2, -3, -5, -8, -10, -20, -100, -300, -500};
1748         // Special cases from Math.pow javadoc:
1749         // If the second argument is positive or negative zero, then the result is 1.0.
1750         for (double d : DOUBLES) {
1751             assertEquals(1.0, FastMath.pow(d, 0L), EXACT);
1752         }
1753         // If the second argument is 1.0, then the result is the same as the first argument.
1754         for (double d : DOUBLES) {
1755             assertEquals(d, FastMath.pow(d, 1L), EXACT);
1756         }
1757         // If the second argument is NaN, then the result is NaN. <- Impossible with int.
1758         // If the first argument is NaN and the second argument is nonzero, then the result is NaN.
1759         for (long i : INTS) {
1760             if (i != 0L) {
1761                 assertEquals(Double.NaN, FastMath.pow(Double.NaN, i), EXACT);
1762             }
1763         }
1764         // If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or
1765         // the absolute value of the first argument is less than 1 and the second argument is negative infinity, then the result is positive infinity.
1766         for (double d : DOUBLES) {
1767             if (Math.abs(d) > 1.0) {
1768                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(d, Long.MAX_VALUE - 1L), EXACT);
1769             }
1770         }
1771         for (double d : DOUBLES) {
1772             if (Math.abs(d) < 1.0) {
1773                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(d, Long.MIN_VALUE), EXACT);
1774             }
1775         }
1776         // Note: Long.MAX_VALUE isn't actually an infinity, so its parity affects the sign of resulting infinity.
1777         for (double d : DOUBLES) {
1778             if (Math.abs(d) > 1.0) {
1779                 assertTrue(Double.isInfinite(FastMath.pow(d, Long.MAX_VALUE)));
1780             }
1781         }
1782         for (double d : DOUBLES) {
1783             if (Math.abs(d) < 1.0) {
1784                 assertTrue(Double.isInfinite(FastMath.pow(d, Long.MIN_VALUE + 1L)));
1785             }
1786         }
1787         // If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or
1788         // the absolute value of the first argument is less than 1 and the second argument is positive infinity, then the result is positive zero.
1789         for (double d : DOUBLES) {
1790             if (Math.abs(d) > 1.0) {
1791                 assertEquals(0.0, FastMath.pow(d, Long.MIN_VALUE), EXACT);
1792             }
1793         }
1794         for (double d : DOUBLES) {
1795             if (Math.abs(d) < 1.0) {
1796                 assertEquals(0.0, FastMath.pow(d, Long.MAX_VALUE - 1L), EXACT);
1797             }
1798         }
1799         // Note: Long.MAX_VALUE isn't actually an infinity, so its parity affects the sign of resulting zero.
1800         for (double d : DOUBLES) {
1801             if (Math.abs(d) > 1.0) {
1802                 assertEquals(0.0, FastMath.pow(d, Long.MIN_VALUE + 1L), 0.0);
1803             }
1804         }
1805         for (double d : DOUBLES) {
1806             if (Math.abs(d) < 1.0) {
1807                 assertEquals(0.0, FastMath.pow(d, Long.MAX_VALUE), 0.0);
1808             }
1809         }
1810         // If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN. <- Impossible with int.
1811         // If the first argument is positive zero and the second argument is greater than zero, or
1812         // the first argument is positive infinity and the second argument is less than zero, then the result is positive zero.
1813         for (long i : INTS) {
1814             if (i > 0L) {
1815                 assertEquals(0.0, FastMath.pow(0.0, i), EXACT);
1816             }
1817         }
1818         for (long i : INTS) {
1819             if (i < 0L) {
1820                 assertEquals(0.0, FastMath.pow(Double.POSITIVE_INFINITY, i), EXACT);
1821             }
1822         }
1823         // If the first argument is positive zero and the second argument is less than zero, or
1824         // the first argument is positive infinity and the second argument is greater than zero, then the result is positive infinity.
1825         for (long i : INTS) {
1826             if (i < 0L) {
1827                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(0.0, i), EXACT);
1828             }
1829         }
1830         for (long i : INTS) {
1831             if (i > 0L) {
1832                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.POSITIVE_INFINITY, i), EXACT);
1833             }
1834         }
1835         // If the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or
1836         // the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, then the result is positive zero.
1837         for (long i : INTS) {
1838             if (i > 0L && (i & 1L) == 0L) {
1839                 assertEquals(0.0, FastMath.pow(-0.0, i), EXACT);
1840             }
1841         }
1842         for (long i : INTS) {
1843             if (i < 0L && (i & 1L) == 0L) {
1844                 assertEquals(0.0, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
1845             }
1846         }
1847         // If the first argument is negative zero and the second argument is a positive finite odd integer, or
1848         // the first argument is negative infinity and the second argument is a negative finite odd integer, then the result is negative zero.
1849         for (long i : INTS) {
1850             if (i > 0L && (i & 1L) == 1L) {
1851                 assertEquals(-0.0, FastMath.pow(-0.0, i), EXACT);
1852             }
1853         }
1854         for (long i : INTS) {
1855             if (i < 0L && (i & 1L) == 1L) {
1856                 assertEquals(-0.0, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
1857             }
1858         }
1859         // If the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or
1860         // the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, then the result is positive infinity.
1861         for (long i : INTS) {
1862             if (i > 0L && (i & 1L) == 0L) {
1863                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
1864             }
1865         }
1866         for (long i : INTS) {
1867             if (i < 0L && (i & 1L) == 0L) {
1868                 assertEquals(Double.POSITIVE_INFINITY, FastMath.pow(-0.0, i), EXACT);
1869             }
1870         }
1871         // If the first argument is negative zero and the second argument is a negative finite odd integer, or
1872         // the first argument is negative infinity and the second argument is a positive finite odd integer, then the result is negative infinity.
1873         for (long i : INTS) {
1874             if (i > 0L && (i & 1L) == 1L) {
1875                 assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(Double.NEGATIVE_INFINITY, i), EXACT);
1876             }
1877         }
1878         for (long i : INTS) {
1879             if (i < 0L && (i & 1L) == 1L) {
1880                 assertEquals(Double.NEGATIVE_INFINITY, FastMath.pow(-0.0, i), EXACT);
1881             }
1882         }
1883         for (double d : DOUBLES) {
1884             // If the first argument is finite and less than zero
1885             if (d < 0.0 && Math.abs(d) <= Double.MAX_VALUE) {
1886                 for (long i : INTS) {
1887                     // if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
1888                     if ((i & 1L) == 0L) assertEquals(FastMath.pow(-d, i), FastMath.pow(d, i), EXACT);
1889                     // if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
1890                     else assertEquals(-FastMath.pow(-d, i), FastMath.pow(d, i), EXACT);
1891                     // if the second argument is finite and not an integer, then the result is NaN. <- Impossible with int.
1892                 }
1893             }
1894         }
1895         // If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first argument to the power
1896         // of the second argument if that result can in fact be represented exactly as a double value. <- Other tests.
1897     }
1898 
1899     @Test
1900     void testIncrementExactInt() {
1901         int[] specialValues = new int[] {
1902             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
1903             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
1904             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1905             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
1906             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
1907         };
1908         for (int a : specialValues) {
1909             BigInteger bdA   = BigInteger.valueOf(a);
1910             BigInteger bdSum = bdA.add(BigInteger.ONE);
1911             if (bdSum.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
1912                 bdSum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
1913                 try {
1914                     FastMath.incrementExact(a);
1915                     fail("an exception should have been thrown");
1916                 } catch (MathRuntimeException mae) {
1917                     // expected
1918                 }
1919             } else {
1920                 assertEquals(bdSum, BigInteger.valueOf(FastMath.incrementExact(a)));
1921             }
1922         }
1923     }
1924 
1925     @Test
1926     void testIncrementExactLong() {
1927         long[] specialValues = new long[] {
1928             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
1929             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
1930             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1931             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
1932             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
1933         };
1934         for (long a : specialValues) {
1935             BigInteger bdA   = BigInteger.valueOf(a);
1936             BigInteger bdSum = bdA.add(BigInteger.ONE);
1937             if (bdSum.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
1938                 bdSum.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
1939                 try {
1940                     FastMath.incrementExact(a);
1941                     fail("an exception should have been thrown");
1942                 } catch (MathRuntimeException mae) {
1943                     // expected
1944                 }
1945             } else {
1946                 assertEquals(bdSum, BigInteger.valueOf(FastMath.incrementExact(a)));
1947             }
1948         }
1949     }
1950 
1951     @Test
1952     void testDecrementExactInt() {
1953         int[] specialValues = new int[] {
1954             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
1955             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
1956             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1957             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
1958             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
1959         };
1960         for (int a : specialValues) {
1961             BigInteger bdA   = BigInteger.valueOf(a);
1962             BigInteger bdSub = bdA.subtract(BigInteger.ONE);
1963             if (bdSub.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
1964                 bdSub.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
1965                 try {
1966                     FastMath.decrementExact(a);
1967                     fail("an exception should have been thrown");
1968                 } catch (MathRuntimeException mae) {
1969                     // expected
1970                 }
1971             } else {
1972                 assertEquals(bdSub, BigInteger.valueOf(FastMath.decrementExact(a)));
1973             }
1974         }
1975     }
1976 
1977     @Test
1978     void testDecrementExactLong() {
1979         long[] specialValues = new long[] {
1980             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
1981             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
1982             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1983             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
1984             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
1985         };
1986         for (long a : specialValues) {
1987             BigInteger bdA   = BigInteger.valueOf(a);
1988             BigInteger bdSub = bdA.subtract(BigInteger.ONE);
1989             if (bdSub.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
1990                 bdSub.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
1991                 try {
1992                     FastMath.decrementExact(a);
1993                     fail("an exception should have been thrown");
1994                 } catch (MathRuntimeException mae) {
1995                     // expected
1996                 }
1997             } else {
1998                 assertEquals(bdSub, BigInteger.valueOf(FastMath.decrementExact(a)));
1999             }
2000         }
2001     }
2002 
2003     @Test
2004     void testAddExactInt() {
2005         int[] specialValues = new int[] {
2006             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
2007             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
2008             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2009             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
2010             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
2011         };
2012         for (int a : specialValues) {
2013             for (int b : specialValues) {
2014                 BigInteger bdA   = BigInteger.valueOf(a);
2015                 BigInteger bdB   = BigInteger.valueOf(b);
2016                 BigInteger bdSum = bdA.add(bdB);
2017                 if (bdSum.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
2018                         bdSum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
2019                     try {
2020                         FastMath.addExact(a, b);
2021                         fail("an exception should have been thrown");
2022                     } catch (MathRuntimeException mae) {
2023                         // expected
2024                     }
2025                 } else {
2026                     assertEquals(bdSum, BigInteger.valueOf(FastMath.addExact(a, b)));
2027                 }
2028             }
2029         }
2030     }
2031 
2032     @Test
2033     void testAddExactLong() {
2034         long[] specialValues = new long[] {
2035             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
2036             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
2037             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2038             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
2039             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
2040         };
2041         for (long a : specialValues) {
2042             for (long b : specialValues) {
2043                 BigInteger bdA   = BigInteger.valueOf(a);
2044                 BigInteger bdB   = BigInteger.valueOf(b);
2045                 BigInteger bdSum = bdA.add(bdB);
2046                 if (bdSum.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
2047                         bdSum.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
2048                     try {
2049                         FastMath.addExact(a, b);
2050                         fail("an exception should have been thrown");
2051                     } catch (MathRuntimeException mae) {
2052                         // expected
2053                     }
2054                 } else {
2055                     assertEquals(bdSum, BigInteger.valueOf(FastMath.addExact(a, b)));
2056                 }
2057             }
2058         }
2059     }
2060 
2061     @Test
2062     void testSubtractExactInt() {
2063         int[] specialValues = new int[] {
2064             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
2065             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
2066             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2067             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
2068             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
2069         };
2070         for (int a : specialValues) {
2071             for (int b : specialValues) {
2072                 BigInteger bdA   = BigInteger.valueOf(a);
2073                 BigInteger bdB   = BigInteger.valueOf(b);
2074                 BigInteger bdSub = bdA.subtract(bdB);
2075                 if (bdSub.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
2076                         bdSub.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
2077                     try {
2078                         FastMath.subtractExact(a, b);
2079                         fail("an exception should have been thrown");
2080                     } catch (MathRuntimeException mae) {
2081                         // expected
2082                     }
2083                 } else {
2084                     assertEquals(bdSub, BigInteger.valueOf(FastMath.subtractExact(a, b)));
2085                 }
2086             }
2087         }
2088     }
2089 
2090     @Test
2091     void testSubtractExactLong() {
2092         long[] specialValues = new long[] {
2093             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
2094             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
2095             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2096             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
2097             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
2098         };
2099         for (long a : specialValues) {
2100             for (long b : specialValues) {
2101                 BigInteger bdA   = BigInteger.valueOf(a);
2102                 BigInteger bdB   = BigInteger.valueOf(b);
2103                 BigInteger bdSub = bdA.subtract(bdB);
2104                 if (bdSub.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
2105                         bdSub.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
2106                     try {
2107                         FastMath.subtractExact(a, b);
2108                         fail("an exception should have been thrown");
2109                     } catch (MathRuntimeException mae) {
2110                         // expected
2111                     }
2112                 } else {
2113                     assertEquals(bdSub, BigInteger.valueOf(FastMath.subtractExact(a, b)));
2114                 }
2115             }
2116         }
2117     }
2118 
2119     @Test
2120     void testMultiplyExactInt() {
2121         int[] specialValues = new int[] {
2122             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
2123             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
2124             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2125             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
2126             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
2127         };
2128         for (int a : specialValues) {
2129             for (int b : specialValues) {
2130                 BigInteger bdA   = BigInteger.valueOf(a);
2131                 BigInteger bdB   = BigInteger.valueOf(b);
2132                 BigInteger bdMul = bdA.multiply(bdB);
2133                 if (bdMul.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
2134                     bdMul.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
2135                     try {
2136                         FastMath.multiplyExact(a, b);
2137                         fail("an exception should have been thrown " + a + b);
2138                     } catch (MathRuntimeException mae) {
2139                         // expected
2140                     }
2141                 } else {
2142                     assertEquals(bdMul, BigInteger.valueOf(FastMath.multiplyExact(a, b)));
2143                 }
2144                 assertEquals(bdMul.longValue(), FastMath.multiplyFull(a, b));
2145             }
2146         }
2147     }
2148 
2149     @Test
2150     void testMultiplyExactLongInt() {
2151         long[] specialValuesL = new long[] {
2152             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
2153             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
2154             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2155             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
2156             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
2157         };
2158         int[] specialValuesI = new int[] {
2159             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
2160             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
2161             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2162             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
2163             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
2164         };
2165         for (long a : specialValuesL) {
2166             for (int b : specialValuesI) {
2167                 BigInteger bdA   = BigInteger.valueOf(a);
2168                 BigInteger bdB   = BigInteger.valueOf(b);
2169                 BigInteger bdMul = bdA.multiply(bdB);
2170                 if (bdMul.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
2171                     bdMul.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
2172                     try {
2173                         FastMath.multiplyExact(a, b);
2174                         fail("an exception should have been thrown " + a + b);
2175                     } catch (MathRuntimeException mae) {
2176                         // expected
2177                     }
2178                 } else {
2179                     assertEquals(bdMul, BigInteger.valueOf(FastMath.multiplyExact(a, b)));
2180                 }
2181             }
2182         }
2183     }
2184 
2185     @Test
2186     void testMultiplyExactLong() {
2187         long[] specialValues = new long[] {
2188             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
2189             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
2190             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2191             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
2192             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
2193         };
2194         for (long a : specialValues) {
2195             for (long b : specialValues) {
2196                 BigInteger bdA   = BigInteger.valueOf(a);
2197                 BigInteger bdB   = BigInteger.valueOf(b);
2198                 BigInteger bdMul = bdA.multiply(bdB);
2199                 if (bdMul.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
2200                     bdMul.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
2201                     try {
2202                         FastMath.multiplyExact(a, b);
2203                         fail("an exception should have been thrown " + a + b);
2204                     } catch (MathRuntimeException mae) {
2205                         // expected
2206                     }
2207                 } else {
2208                     assertEquals(bdMul, BigInteger.valueOf(FastMath.multiplyExact(a, b)));
2209                 }
2210             }
2211         }
2212     }
2213 
2214     @Test
2215     void testDivideExactInt() {
2216         int[] specialValues = new int[] {
2217             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2,
2218             Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2,
2219             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2220             -1 - (Integer.MIN_VALUE / 2), -(Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2),
2221             -1 + (Integer.MAX_VALUE / 2), (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2),
2222         };
2223         for (int a : specialValues) {
2224             for (int b : specialValues) {
2225                 if (b == 0) {
2226                     try {
2227                         FastMath.divideExact(a, b);
2228                         fail("an exception should have been thrown " + a + b);
2229                     } catch (MathRuntimeException mae) {
2230                         assertEquals(LocalizedCoreFormats.ZERO_DENOMINATOR, mae.getSpecifier());
2231                     }
2232                 } else {
2233                     BigInteger bdA   = BigInteger.valueOf(a);
2234                     BigInteger bdB   = BigInteger.valueOf(b);
2235                     BigInteger bdDiv = bdA.divide(bdB);
2236                     if (bdDiv.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
2237                                     bdDiv.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
2238                         try {
2239                             FastMath.divideExact(a, b);
2240                             fail("an exception should have been thrown " + a + b);
2241                         } catch (MathRuntimeException mae) {
2242                             // expected
2243                         }
2244                     } else {
2245                         assertEquals(bdDiv, BigInteger.valueOf(FastMath.divideExact(a, b)));
2246                     }
2247                 }
2248             }
2249         }
2250     }
2251 
2252     @Test
2253     void testDivideExactLong() {
2254         long[] specialValues = new long[] {
2255             Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2,
2256             Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 2,
2257             -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2258             -1 - (Long.MIN_VALUE / 2), -(Long.MIN_VALUE / 2), 1 - (Long.MIN_VALUE / 2),
2259             -1 + (Long.MAX_VALUE / 2), (Long.MAX_VALUE / 2), 1 + (Long.MAX_VALUE / 2),
2260         };
2261         for (long a : specialValues) {
2262             for (long b : specialValues) {
2263                 if (b == 0L) {
2264                     try {
2265                         FastMath.divideExact(a, b);
2266                         fail("an exception should have been thrown " + a + b);
2267                     } catch (MathRuntimeException mae) {
2268                         assertEquals(LocalizedCoreFormats.ZERO_DENOMINATOR, mae.getSpecifier());
2269                     }
2270                 } else {
2271                     BigInteger bdA   = BigInteger.valueOf(a);
2272                     BigInteger bdB   = BigInteger.valueOf(b);
2273                     BigInteger bdDiv = bdA.divide(bdB);
2274                     if (bdDiv.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 ||
2275                                     bdDiv.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
2276                         try {
2277                             FastMath.divideExact(a, b);
2278                             fail("an exception should have been thrown " + a + b);
2279                         } catch (MathRuntimeException mae) {
2280                             // expected
2281                         }
2282                     } else {
2283                         assertEquals(bdDiv, BigInteger.valueOf(FastMath.divideExact(a, b)));
2284                     }
2285                 }
2286             }
2287         }
2288     }
2289 
2290     @Test
2291     void testToIntExactTooLow() {
2292         assertThrows(MathRuntimeException.class, () -> FastMath.toIntExact(-1L + Integer.MIN_VALUE));
2293     }
2294 
2295     @Test
2296     void testToIntExactTooHigh() {
2297         assertThrows(MathRuntimeException.class, () -> FastMath.toIntExact(+1L + Integer.MAX_VALUE));
2298     }
2299 
2300     @Test
2301     void testAbsExactInt() {
2302         assertEquals(12, FastMath.absExact(+12));
2303         assertEquals(12, FastMath.absExact(-12));
2304         assertEquals(Integer.MAX_VALUE, FastMath.absExact(Integer.MAX_VALUE));
2305         assertEquals(Integer.MAX_VALUE, FastMath.absExact(-Integer.MAX_VALUE));
2306         try {
2307             FastMath.absExact(Integer.MIN_VALUE);
2308             fail("an exception should have been thrown");
2309         } catch (ArithmeticException ae) {
2310             // expected
2311         }
2312     }
2313 
2314     @Test
2315     void testAbsExactLong() {
2316         assertEquals(12L, FastMath.absExact(+12L));
2317         assertEquals(12L, FastMath.absExact(-12L));
2318         assertEquals(Long.MAX_VALUE, FastMath.absExact(Long.MAX_VALUE));
2319         assertEquals(Long.MAX_VALUE, FastMath.absExact(-Long.MAX_VALUE));
2320         try {
2321             FastMath.absExact(Long.MIN_VALUE);
2322             fail("an exception should have been thrown");
2323         } catch (ArithmeticException ae) {
2324             // expected
2325         }
2326     }
2327 
2328     @Test
2329     void testNegateExactInt() {
2330         assertEquals(-12, FastMath.negateExact(+12));
2331         assertEquals(12, FastMath.negateExact(-12));
2332         assertEquals(-Integer.MAX_VALUE, FastMath.negateExact(Integer.MAX_VALUE));
2333         assertEquals(Integer.MAX_VALUE, FastMath.negateExact(-Integer.MAX_VALUE));
2334         try {
2335             FastMath.negateExact(Integer.MIN_VALUE);
2336             fail("an exception should have been thrown");
2337         } catch (ArithmeticException ae) {
2338             // expected
2339         }
2340     }
2341 
2342     @Test
2343     void testNegateExactLong() {
2344         assertEquals(-12L, FastMath.negateExact(+12L));
2345         assertEquals(12L, FastMath.negateExact(-12L));
2346         assertEquals(-Long.MAX_VALUE, FastMath.negateExact(Long.MAX_VALUE));
2347         assertEquals(Long.MAX_VALUE, FastMath.negateExact(-Long.MAX_VALUE));
2348         try {
2349             FastMath.negateExact(Long.MIN_VALUE);
2350             fail("an exception should have been thrown");
2351         } catch (ArithmeticException ae) {
2352             // expected
2353         }
2354     }
2355 
2356     @Test
2357     void testToIntExact() {
2358         for (int n = -1000; n < 1000; ++n) {
2359             assertEquals(n, FastMath.toIntExact((long) n));
2360         }
2361         assertEquals(Integer.MIN_VALUE, FastMath.toIntExact(
2362                         (long) Integer.MIN_VALUE));
2363         assertEquals(Integer.MAX_VALUE, FastMath.toIntExact(
2364                         (long) Integer.MAX_VALUE));
2365     }
2366 
2367     @Test
2368     void testCeilDivInt() {
2369         assertEquals(+2, FastMath.ceilDiv(+4, +3));
2370         assertEquals(-1, FastMath.ceilDiv(-4, +3));
2371         assertEquals(-1, FastMath.ceilDiv(+4, -3));
2372         assertEquals(+2, FastMath.ceilDiv(-4, -3));
2373         try {
2374             FastMath.ceilDiv(1, 0);
2375             fail("an exception should have been thrown");
2376         } catch (MathRuntimeException mae) {
2377             // expected
2378         }
2379         for (int a = -100; a <= 100; ++a) {
2380             for (int b = -100; b <= 100; ++b) {
2381                 if (b != 0) {
2382                     assertEquals(poorManCeilDiv(a, b), FastMath.ceilDiv(a, b));
2383                     assertEquals(poorManCeilDiv(a, b), FastMath.ceilDivExact(a, b));
2384                 }
2385             }
2386         }
2387         assertEquals(Integer.MIN_VALUE, FastMath.ceilDiv(Integer.MIN_VALUE, -1));
2388         try {
2389             FastMath.ceilDivExact(Integer.MIN_VALUE, -1);
2390             fail("an exception should have been thrown");
2391         } catch (MathRuntimeException mre) {
2392             assertEquals(LocalizedCoreFormats.OVERFLOW_IN_FRACTION, mre.getSpecifier());
2393         }
2394     }
2395 
2396     @Test
2397     void testCeilDivLong() {
2398         assertEquals(+2L, FastMath.ceilDiv(+4L, +3L));
2399         assertEquals(-1L, FastMath.ceilDiv(-4L, +3L));
2400         assertEquals(-1L, FastMath.ceilDiv(+4L, -3L));
2401         assertEquals(+2L, FastMath.ceilDiv(-4L, -3L));
2402         try {
2403             FastMath.ceilDiv(1L, 0L);
2404             fail("an exception should have been thrown");
2405         } catch (MathRuntimeException mae) {
2406             // expected
2407         }
2408         for (long a = -100L; a <= 100L; ++a) {
2409             for (long b = -100L; b <= 100L; ++b) {
2410                 if (b != 0L) {
2411                     assertEquals(poorManCeilDiv(a, b), FastMath.ceilDiv(a, b));
2412                     assertEquals(poorManCeilDiv(a, b), FastMath.ceilDivExact(a, b));
2413                 }
2414             }
2415         }
2416         assertEquals(Long.MIN_VALUE, FastMath.ceilDiv(Long.MIN_VALUE, -1L));
2417         try {
2418             FastMath.ceilDivExact(Long.MIN_VALUE, -1L);
2419             fail("an exception should have been thrown");
2420         } catch (MathRuntimeException mre) {
2421             assertEquals(LocalizedCoreFormats.OVERFLOW_IN_FRACTION, mre.getSpecifier());
2422         }
2423     }
2424 
2425     @Test
2426     void testCeilDivLongInt() {
2427         assertEquals(+2L, FastMath.ceilDiv(+4L, +3));
2428         assertEquals(-1L, FastMath.ceilDiv(-4L, +3));
2429         assertEquals(-1L, FastMath.ceilDiv(+4L, -3));
2430         assertEquals(+2L, FastMath.ceilDiv(-4L, -3));
2431         try {
2432             FastMath.ceilDiv(1L, 0);
2433             fail("an exception should have been thrown");
2434         } catch (MathRuntimeException mae) {
2435             // expected
2436         }
2437         for (long a = -100L; a <= 100L; ++a) {
2438             for (int b = -100; b <= 100; ++b) {
2439                 if (b != 0) {
2440                     assertEquals(poorManCeilDiv(a, b), FastMath.ceilDiv(a, b));
2441                     assertEquals(poorManCeilDiv(a, b), FastMath.ceilDivExact(a, b));
2442                 }
2443             }
2444         }
2445         assertEquals(Long.MIN_VALUE, FastMath.ceilDiv(Long.MIN_VALUE, -1));
2446         try {
2447             FastMath.ceilDivExact(Long.MIN_VALUE, -1);
2448             fail("an exception should have been thrown");
2449         } catch (MathRuntimeException mre) {
2450             assertEquals(LocalizedCoreFormats.OVERFLOW_IN_FRACTION, mre.getSpecifier());
2451         }
2452     }
2453 
2454     @Test
2455     void testCeilDivModInt() {
2456         RandomGenerator generator = new Well1024a(0x66c371cc6f7ebea9L);
2457         for (int i = 0; i < 10000; ++i) {
2458             int a = generator.nextInt();
2459             int b = generator.nextInt();
2460             if (b == 0) {
2461                 try {
2462                     FastMath.floorMod(a, b);
2463                     fail("an exception should have been thrown");
2464                 } catch (MathRuntimeException mae) {
2465                     // expected
2466                 }
2467             } else {
2468                 int d = FastMath.ceilDiv(a, b);
2469                 int m = FastMath.ceilMod(a, b);
2470                 assertEquals(poorManCeilDiv(a, b), d);
2471                 assertEquals(poorManCeilMod(a, b), m);
2472                 assertEquals(a, (long) d * b + m);
2473                 if (b < 0) {
2474                     assertTrue(m >= 0);
2475                     assertTrue(-m > b);
2476                 } else {
2477                     assertTrue(m <= 0);
2478                     assertTrue(m < b);
2479                 }
2480             }
2481         }
2482     }
2483 
2484     @Test
2485     void testCeilDivModLong() {
2486         RandomGenerator generator = new Well1024a(0x769c9ab4e4a9129eL);
2487         for (int i = 0; i < 10000; ++i) {
2488             long a = generator.nextLong();
2489             long b = generator.nextLong();
2490             if (b == 0L) {
2491                 try {
2492                     FastMath.floorMod(a, b);
2493                     fail("an exception should have been thrown");
2494                 } catch (MathRuntimeException mae) {
2495                     // expected
2496                 }
2497             } else {
2498                 long d = FastMath.ceilDiv(a, b);
2499                 long m = FastMath.ceilMod(a, b);
2500                 assertEquals(poorManCeilDiv(a, b), d);
2501                 assertEquals(poorManCeilMod(a, b), m);
2502                 assertEquals(a, d * b + m);
2503                 if (b < 0L) {
2504                     assertTrue(m >= 0L);
2505                     assertTrue(-m > b);
2506                 } else {
2507                     assertTrue(m <= 0L);
2508                     assertTrue(m < b);
2509                 }
2510             }
2511         }
2512     }
2513 
2514     @Test
2515     void testCeilDivModLongInt() {
2516         RandomGenerator generator = new Well1024a(0xd4c67ab9cd4af669L);
2517         for (int i = 0; i < 10000; ++i) {
2518             long a = generator.nextLong();
2519             int b = generator.nextInt();
2520             if (b == 0) {
2521                 try {
2522                     FastMath.floorMod(a, b);
2523                     fail("an exception should have been thrown");
2524                 } catch (MathRuntimeException mae) {
2525                     // expected
2526                 }
2527             } else {
2528                 long d = FastMath.ceilDiv(a, b);
2529                 int  m = FastMath.ceilMod(a, b);
2530                 assertEquals(poorManCeilDiv(a, b), d);
2531                 assertEquals(poorManCeilMod(a, b), m);
2532                 assertEquals(a, d * b + m);
2533                 if (b < 0) {
2534                     assertTrue(m >= 0);
2535                     assertTrue(-m > b);
2536                 } else {
2537                     assertTrue(m <= 0);
2538                     assertTrue(m < b);
2539                 }
2540             }
2541         }
2542     }
2543 
2544     @Test
2545     void testFloorDivInt() {
2546         assertEquals(+1, FastMath.floorDiv(+4, +3));
2547         assertEquals(-2, FastMath.floorDiv(-4, +3));
2548         assertEquals(-2, FastMath.floorDiv(+4, -3));
2549         assertEquals(+1, FastMath.floorDiv(-4, -3));
2550         try {
2551             FastMath.floorDiv(1, 0);
2552             fail("an exception should have been thrown");
2553         } catch (MathRuntimeException mae) {
2554             // expected
2555         }
2556         for (int a = -100; a <= 100; ++a) {
2557             for (int b = -100; b <= 100; ++b) {
2558                 if (b != 0) {
2559                     assertEquals(poorManFloorDiv(a, b), FastMath.floorDiv(a, b));
2560                     assertEquals(poorManFloorDiv(a, b), FastMath.floorDivExact(a, b));
2561                 }
2562             }
2563         }
2564         assertEquals(Integer.MIN_VALUE, FastMath.floorDiv(Integer.MIN_VALUE, -1));
2565         try {
2566             FastMath.floorDivExact(Integer.MIN_VALUE, -1);
2567             fail("an exception should have been thrown");
2568         } catch (MathRuntimeException mre) {
2569             assertEquals(LocalizedCoreFormats.OVERFLOW_IN_FRACTION, mre.getSpecifier());
2570         }
2571     }
2572 
2573     @Test
2574     void testFloorModInt() {
2575         assertEquals(+1, FastMath.floorMod(+4, +3));
2576         assertEquals(+2, FastMath.floorMod(-4, +3));
2577         assertEquals(-2, FastMath.floorMod(+4, -3));
2578         assertEquals(-1, FastMath.floorMod(-4, -3));
2579         try {
2580             FastMath.floorMod(1, 0);
2581             fail("an exception should have been thrown");
2582         } catch (MathRuntimeException mae) {
2583             // expected
2584         }
2585         for (int a = -100; a <= 100; ++a) {
2586             for (int b = -100; b <= 100; ++b) {
2587                 if (b != 0) {
2588                     assertEquals(poorManFloorMod(a, b), FastMath.floorMod(a, b));
2589                 }
2590             }
2591         }
2592     }
2593 
2594     @Test
2595     void testFloorDivModInt() {
2596         RandomGenerator generator = new Well1024a(0x7ccab45edeaab90aL);
2597         for (int i = 0; i < 10000; ++i) {
2598             int a = generator.nextInt();
2599             int b = generator.nextInt();
2600             if (b == 0) {
2601                 try {
2602                     FastMath.floorDiv(a, b);
2603                     fail("an exception should have been thrown");
2604                 } catch (MathRuntimeException mae) {
2605                     // expected
2606                 }
2607             } else {
2608                 int d = FastMath.floorDiv(a, b);
2609                 int m = FastMath.floorMod(a, b);
2610                 assertEquals(poorManFloorDiv(a, b), d);
2611                 assertEquals(poorManFloorMod(a, b), m);
2612                 assertEquals(a, (long) d * b + m);
2613                 if (b < 0) {
2614                     assertTrue(m <= 0);
2615                     assertTrue(-m < -b);
2616                 } else {
2617                     assertTrue(m >= 0);
2618                     assertTrue(m < b);
2619                 }
2620             }
2621         }
2622     }
2623 
2624     @Test
2625     void testFloorDivLong() {
2626         assertEquals(+1L, FastMath.floorDiv(+4L, +3L));
2627         assertEquals(-2L, FastMath.floorDiv(-4L, +3L));
2628         assertEquals(-2L, FastMath.floorDiv(+4L, -3L));
2629         assertEquals(+1L, FastMath.floorDiv(-4L, -3L));
2630         try {
2631             FastMath.floorDiv(1L, 0L);
2632             fail("an exception should have been thrown");
2633         } catch (MathRuntimeException mae) {
2634             // expected
2635         }
2636         for (long a = -100L; a <= 100L; ++a) {
2637             for (long b = -100L; b <= 100L; ++b) {
2638                 if (b != 0) {
2639                     assertEquals(poorManFloorDiv(a, b), FastMath.floorDiv(a, b));
2640                     assertEquals(poorManFloorDiv(a, b), FastMath.floorDivExact(a, b));
2641                 }
2642             }
2643         }
2644         assertEquals(Long.MIN_VALUE, FastMath.floorDiv(Long.MIN_VALUE, -1L));
2645         try {
2646             FastMath.floorDivExact(Long.MIN_VALUE, -1L);
2647             fail("an exception should have been thrown");
2648         } catch (MathRuntimeException mre) {
2649             assertEquals(LocalizedCoreFormats.OVERFLOW_IN_FRACTION, mre.getSpecifier());
2650         }
2651     }
2652 
2653     @Test
2654     void testFloorModLong() {
2655         assertEquals(+1L, FastMath.floorMod(+4L, +3L));
2656         assertEquals(+2L, FastMath.floorMod(-4L, +3L));
2657         assertEquals(-2L, FastMath.floorMod(+4L, -3L));
2658         assertEquals(-1L, FastMath.floorMod(-4L, -3L));
2659         try {
2660             FastMath.floorMod(1L, 0L);
2661             fail("an exception should have been thrown");
2662         } catch (MathRuntimeException mae) {
2663             // expected
2664         }
2665         for (long a = -100L; a <= 100L; ++a) {
2666             for (long b = -100L; b <= 100L; ++b) {
2667                 if (b != 0) {
2668                     assertEquals(poorManFloorMod(a, b), FastMath.floorMod(a, b));
2669                 }
2670             }
2671         }
2672     }
2673 
2674     @Test
2675     void testFloorDivModLong() {
2676         RandomGenerator generator = new Well1024a(0xb87b9bc14c96ccd5L);
2677         for (int i = 0; i < 10000; ++i) {
2678             long a = generator.nextLong();
2679             long b = generator.nextLong();
2680             if (b == 0) {
2681                 try {
2682                     FastMath.floorDiv(a, b);
2683                     fail("an exception should have been thrown");
2684                 } catch (MathRuntimeException mae) {
2685                     // expected
2686                 }
2687             } else {
2688                 long d = FastMath.floorDiv(a, b);
2689                 long m = FastMath.floorMod(a, b);
2690                 assertEquals(poorManFloorDiv(a, b), d);
2691                 assertEquals(poorManFloorMod(a, b), m);
2692                 assertEquals(a, d * b + m);
2693                 if (b < 0) {
2694                     assertTrue(m <= 0);
2695                     assertTrue(-m < -b);
2696                 } else {
2697                     assertTrue(m >= 0);
2698                     assertTrue(m < b);
2699                 }
2700             }
2701         }
2702     }
2703 
2704     @Test
2705     void testFloorDivModLongInt() {
2706         RandomGenerator generator = new Well1024a(0xe03b6a1800d92fa7L);
2707         for (int i = 0; i < 10000; ++i) {
2708             long a = generator.nextInt();
2709             int b = generator.nextInt();
2710             if (b == 0) {
2711                 try {
2712                     FastMath.floorDiv(a, b);
2713                     fail("an exception should have been thrown");
2714                 } catch (MathRuntimeException mae) {
2715                     // expected
2716                 }
2717             } else {
2718                 long d = FastMath.floorDiv(a, b);
2719                 long m = FastMath.floorMod(a, b);
2720                 assertEquals(poorManFloorDiv(a, b), d);
2721                 assertEquals(poorManFloorMod(a, b), m);
2722                 assertEquals(a, d * b + m);
2723                 if (b < 0) {
2724                     assertTrue(m <= 0);
2725                     assertTrue(-m < -b);
2726                 } else {
2727                     assertTrue(m >= 0);
2728                     assertTrue(m < b);
2729                 }
2730             }
2731         }
2732     }
2733 
2734     private int poorManCeilDiv(int a, int b) {
2735 
2736         // find q0, r0 such that a = q0 b + r0
2737         BigInteger q0   = BigInteger.valueOf(a / b);
2738         BigInteger r0   = BigInteger.valueOf(a % b);
2739         BigInteger fd   = BigInteger.valueOf(Integer.MIN_VALUE);
2740         BigInteger bigB = BigInteger.valueOf(b);
2741 
2742         for (int k = -2; k < 2; ++k) {
2743             // find another pair q, r such that a = q b + r
2744             BigInteger bigK = BigInteger.valueOf(k);
2745             BigInteger q    = q0.subtract(bigK);
2746             BigInteger r    = r0.add(bigK.multiply(bigB));
2747             if (r.abs().compareTo(bigB.abs()) < 0 &&
2748                 (r.intValue() == 0 || ((r.intValue() ^ b) & 0x80000000) != 0)) {
2749                 if (fd.compareTo(q) < 0) {
2750                     fd = q;
2751                 }
2752             }
2753         }
2754 
2755         return fd.intValue();
2756 
2757     }
2758 
2759     private long poorManCeilDiv(long a, long b) {
2760 
2761         // find q0, r0 such that a = q0 b + r0
2762         BigInteger q0   = BigInteger.valueOf(a / b);
2763         BigInteger r0   = BigInteger.valueOf(a % b);
2764         BigInteger fd   = BigInteger.valueOf(Long.MIN_VALUE);
2765         BigInteger bigB = BigInteger.valueOf(b);
2766 
2767         for (int k = -2; k < 2; ++k) {
2768             // find another pair q, r such that a = q b + r
2769             BigInteger bigK = BigInteger.valueOf(k);
2770             BigInteger q    = q0.subtract(bigK);
2771             BigInteger r    = r0.add(bigK.multiply(bigB));
2772             if (r.abs().compareTo(bigB.abs()) < 0 &&
2773                 (r.longValue() == 0L || ((r.longValue() ^ b) & 0x8000000000000000L) != 0)) {
2774                 if (fd.compareTo(q) < 0) {
2775                     fd = q;
2776                 }
2777             }
2778         }
2779 
2780         return fd.longValue();
2781 
2782     }
2783 
2784     private long poorManCeilMod(long a, long b) {
2785         return a - b * poorManCeilDiv(a, b);
2786     }
2787 
2788     private int poorManFloorDiv(int a, int b) {
2789 
2790         // find q0, r0 such that a = q0 b + r0
2791         BigInteger q0   = BigInteger.valueOf(a / b);
2792         BigInteger r0   = BigInteger.valueOf(a % b);
2793         BigInteger fd   = BigInteger.valueOf(Integer.MIN_VALUE);
2794         BigInteger bigB = BigInteger.valueOf(b);
2795 
2796         for (int k = -2; k < 2; ++k) {
2797             // find another pair q, r such that a = q b + r
2798             BigInteger bigK = BigInteger.valueOf(k);
2799             BigInteger q    = q0.subtract(bigK);
2800             BigInteger r    = r0.add(bigK.multiply(bigB));
2801             if (r.abs().compareTo(bigB.abs()) < 0 &&
2802                 (r.intValue() == 0 || ((r.intValue() ^ b) & 0x80000000) == 0)) {
2803                 if (fd.compareTo(q) < 0) {
2804                     fd = q;
2805                 }
2806             }
2807         }
2808 
2809         return fd.intValue();
2810 
2811     }
2812 
2813     private int poorManFloorMod(int a, int b) {
2814         return a - b * poorManFloorDiv(a, b);
2815     }
2816 
2817     private long poorManFloorDiv(long a, long b) {
2818 
2819         // find q0, r0 such that a = q0 b + r0
2820         BigInteger q0   = BigInteger.valueOf(a / b);
2821         BigInteger r0   = BigInteger.valueOf(a % b);
2822         BigInteger fd   = BigInteger.valueOf(Long.MIN_VALUE);
2823         BigInteger bigB = BigInteger.valueOf(b);
2824 
2825         for (int k = -2; k < 2; ++k) {
2826             // find another pair q, r such that a = q b + r
2827             BigInteger bigK = BigInteger.valueOf(k);
2828             BigInteger q    = q0.subtract(bigK);
2829             BigInteger r    = r0.add(bigK.multiply(bigB));
2830             if (r.abs().compareTo(bigB.abs()) < 0 &&
2831                 (r.longValue() == 0L || ((r.longValue() ^ b) & 0x8000000000000000L) == 0)) {
2832                 if (fd.compareTo(q) < 0) {
2833                     fd = q;
2834                 }
2835             }
2836         }
2837 
2838         return fd.longValue();
2839 
2840     }
2841 
2842     private long poorManFloorMod(long a, long b) {
2843         return a - b * poorManFloorDiv(a, b);
2844     }
2845 
2846     /**
2847      * <a href="http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6430675">bug JDK-6430675</a>
2848      */
2849     @Test
2850     void testRoundDown() {
2851         double x = 0x1.fffffffffffffp-2;
2852         assertTrue(x < 0.5d);
2853         assertEquals(0, FastMath.round(x));
2854 
2855         x = 4503599627370497.0; // x = Math.pow(2, 52) + 1;
2856         assertEquals("4503599627370497", new BigDecimal(x).toString());
2857         assertEquals(x, Math.rint(x), 0.0);
2858         assertEquals(x, FastMath.round(x), 0.0);
2859         assertEquals(x, Math.round(x), 0.0);
2860     }
2861 
2862     @Test
2863     void testHypot() {
2864         for (double x = -20; x < 20; x += 0.01) {
2865             for (double y = -20; y < 20; y += 0.01) {
2866                 assertEquals(FastMath.sqrt(x * x + y * y), FastMath.hypot(x, y), 1.0e-15);
2867             }
2868         }
2869     }
2870 
2871     @Test
2872     void testHypotNoOverflow() {
2873         final double x = +3.0e250;
2874         final double y = -4.0e250;
2875         final double h = +5.0e250;
2876         assertEquals(h, FastMath.hypot(x, y), 1.0e-15 * h);
2877         assertTrue(Double.isInfinite(FastMath.sqrt(x * x + y * y)));
2878     }
2879 
2880     @Test
2881     void testHypotSpecialCases() {
2882         assertTrue(Double.isNaN(FastMath.hypot(Double.NaN, 0)));
2883         assertTrue(Double.isNaN(FastMath.hypot(0, Double.NaN)));
2884         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(Double.POSITIVE_INFINITY, 0), 1.0);
2885         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(Double.NEGATIVE_INFINITY, 0), 1.0);
2886         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(Double.POSITIVE_INFINITY, Double.NaN), 1.0);
2887         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(Double.NEGATIVE_INFINITY, Double.NaN), 1.0);
2888         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(0, Double.POSITIVE_INFINITY), 1.0);
2889         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(0, Double.NEGATIVE_INFINITY), 1.0);
2890         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(Double.NaN, Double.POSITIVE_INFINITY), 1.0);
2891         assertEquals(Double.POSITIVE_INFINITY, FastMath.hypot(Double.NaN, Double.NEGATIVE_INFINITY), 1.0);
2892     }
2893 
2894     @Test
2895     void testFMADouble() {
2896         // examples from official javadoc
2897         assertEquals(Double.doubleToRawLongBits(+0.0),
2898                             Double.doubleToRawLongBits(FastMath.fma(-0.0, +0.0, +0.0)));
2899         assertEquals(Double.doubleToRawLongBits(-0.0),
2900                             Double.doubleToRawLongBits(-0.0 * +0.0));
2901 
2902         // computed using Emacs calculator with 50 digits
2903         double a   =  0x1.123456789abcdp-04;
2904         double b   =  0x1.dcba987654321p+01;
2905         double c   = -0x1.fea12e1ce4000p-03;
2906         double fma =  0x1.fb864494872dap-44;
2907         assertEquals(fma, FastMath.fma(a, b, c), 1.0e-50);
2908         assertTrue(FastMath.fma(a, b, c) - (a * b + c) > 5.0e-18);
2909 
2910     }
2911 
2912     @Test
2913     void testFMAFloat() {
2914         // examples from official javadoc
2915         assertEquals(Float.floatToRawIntBits(+0.0f),
2916                             Float.floatToRawIntBits(FastMath.fma(-0.0f, +0.0f, +0.0f)));
2917         assertEquals(Float.floatToRawIntBits(-0.0f),
2918                             Float.floatToRawIntBits(-0.0f * +0.0f));
2919 
2920         // computed using Emacs calculator with 50 digits
2921         float a   =  0x1.123456p-04f;
2922         float b   =  0x1.654322p+01f;
2923         float c   = -0x1.7eaa00p-03f;
2924         float fma =  0x1.c816eap-20f;
2925         assertEquals(fma, FastMath.fma(a, b, c), 1.0e-20f);
2926         assertTrue(FastMath.fma(a, b, c) - (a * b + c) > 3.0e-10f);
2927 
2928     }
2929 
2930     @Test
2931     void testMultiplyHigh() {
2932 
2933         // a * b = Long.MAX_VALUE (exactly), multiplication just fits in a 64 bits primitive long
2934         final long a = 153092023L;
2935         final long b = 60247241209L;
2936         assertEquals(Long.MAX_VALUE, a * b);
2937         assertEquals(0, FastMath.multiplyHigh(a, b));
2938 
2939         // as we just slightly exceeds Long.MAX_VALUE, there are no extra bits,
2940         // but sign is nevertheless wrong because the most significant bit is set to 1
2941         final long c1 = 1L << 31;
2942         final long c2 = 1L << 32;
2943         assertEquals(0, FastMath.multiplyHigh(c1, c2)); // no extra bits
2944         assertEquals(Long.MIN_VALUE, c1 * c2);          // but result is negative despite c1 and c2 are both positive
2945 
2946         // some small and large integers
2947         final long[] values = new long[] {
2948             -1L, 0L, 1L, 10L, 0x100000000L, 0x200000000L, 0x400000000L, -0x100000000L, -0x200000000L, -0x400000000L,
2949             ((long) Integer.MIN_VALUE) -1, ((long) Integer.MIN_VALUE), ((long) Integer.MIN_VALUE) +1,
2950             ((long) Integer.MAX_VALUE) -1, ((long) Integer.MAX_VALUE), ((long) Integer.MAX_VALUE) +1,
2951             Long.MIN_VALUE, Long.MAX_VALUE
2952         };
2953         for (final long p : values) {
2954             for (long q : values) {
2955                 assertEquals(poorManMultiplyHigh(p, q), FastMath.multiplyHigh(p, q));
2956             }
2957         }
2958 
2959         // random values
2960         RandomGenerator random = new Well1024a(0x082a2316178e5e9eL);
2961         for (int i = 0; i < 10000000; ++i) {
2962             long m = random.nextLong();
2963             long n = random.nextLong();
2964             assertEquals(poorManMultiplyHigh(m, n), FastMath.multiplyHigh(m, n));
2965         }
2966     }
2967 
2968     @Test
2969     void testUnsignedMultiplyHigh() {
2970 
2971         // a * b = Long.MAX_VALUE (exactly), multiplication just fits in a 64 bits primitive long
2972         final long a = 153092023L;
2973         final long b = 60247241209L;
2974         assertEquals(Long.MAX_VALUE, a * b);
2975         assertEquals(0, FastMath.unsignedMultiplyHigh(a, b));
2976 
2977         // as we just slightly exceeds Long.MAX_VALUE, there are no extra bits,
2978         // but sign is nevertheless wrong because the most significant bit is set to 1
2979         final long c1 = 1L << 31;
2980         final long c2 = 1L << 32;
2981         assertEquals(0, FastMath.unsignedMultiplyHigh(c1, c2)); // no extra bits
2982         assertEquals(Long.MIN_VALUE, c1 * c2);          // but result is negative despite c1 and c2 are both positive
2983 
2984         // some small and large integers
2985         final long[] values = new long[] {
2986             -1L, 0L, 1L, 10L, 0x100000000L, 0x200000000L, 0x400000000L, -0x100000000L, -0x200000000L, -0x400000000L,
2987             ((long) Integer.MIN_VALUE) -1, ((long) Integer.MIN_VALUE), ((long) Integer.MIN_VALUE) +1,
2988             ((long) Integer.MAX_VALUE) -1, ((long) Integer.MAX_VALUE), ((long) Integer.MAX_VALUE) +1,
2989             Long.MIN_VALUE, Long.MAX_VALUE
2990         };
2991         for (final long p : values) {
2992             for (long q : values) {
2993                 assertEquals(poorManUnsignedMultiplyHigh(p, q), FastMath.unsignedMultiplyHigh(p, q));
2994             }
2995         }
2996 
2997         // random values
2998         RandomGenerator random = new Well1024a(0xcf5736c8f8adf962L);
2999         for (int i = 0; i < 10000000; ++i) {
3000             long m = random.nextLong();
3001             long n = random.nextLong();
3002             assertEquals(poorManUnsignedMultiplyHigh(m, n), FastMath.unsignedMultiplyHigh(m, n));
3003         }
3004     }
3005 
3006     @Test
3007     void testGetExponentDouble() {
3008         assertEquals( 1024, FastMath.getExponent(Double.NaN));
3009         assertEquals( 1024, FastMath.getExponent(Double.POSITIVE_INFINITY));
3010         assertEquals( 1024, FastMath.getExponent(Double.NEGATIVE_INFINITY));
3011         assertEquals(-1023, FastMath.getExponent(+0.0));
3012         assertEquals(-1023, FastMath.getExponent(-0.0));
3013         assertEquals(    1, FastMath.getExponent(+2.0));
3014         assertEquals(    1, FastMath.getExponent(-2.0));
3015         for (int i = -1022; i < 1024; ++i) {
3016             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.0,   i)));
3017             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.2,   i)));
3018             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.5,   i)));
3019             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.999, i)));
3020         }
3021     }
3022 
3023     @Test
3024     void testGetExponentFloat() {
3025         assertEquals( 128, FastMath.getExponent(Float.NaN));
3026         assertEquals( 128, FastMath.getExponent(Float.POSITIVE_INFINITY));
3027         assertEquals( 128, FastMath.getExponent(Float.NEGATIVE_INFINITY));
3028         assertEquals(-127, FastMath.getExponent(+0.0f));
3029         assertEquals(-127, FastMath.getExponent(-0.0f));
3030         assertEquals(   1, FastMath.getExponent(+2.0f));
3031         assertEquals(   1, FastMath.getExponent(-2.0f));
3032         for (int i = -126; i < 128; ++i) {
3033             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.0f,   i)));
3034             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.2f,   i)));
3035             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.5f,   i)));
3036             assertEquals(i, FastMath.getExponent(FastMath.scalb(1.999f, i)));
3037         }
3038     }
3039 
3040     @Test
3041     public void testUnsignedMultiplyExactIntInt() {
3042         Assertions.assertThrows(ArithmeticException.class,
3043                                 () -> FastMath.unsignedMultiplyExact(-1, 0));
3044         Assertions.assertThrows(ArithmeticException.class,
3045                                 () -> FastMath.unsignedMultiplyExact(0, -1));
3046         Assertions.assertThrows(ArithmeticException.class,
3047                                 () -> FastMath.unsignedMultiplyExact(Integer.MAX_VALUE, 2));
3048         Assertions.assertEquals(2147483646, FastMath.unsignedMultiplyExact(41706, 51491));
3049         Assertions.assertThrows(ArithmeticException.class,
3050                                 () -> FastMath.unsignedMultiplyExact(41707, 51491));
3051         Assertions.assertEquals(0, FastMath.unsignedMultiplyExact(0, Integer.MAX_VALUE));
3052     }
3053 
3054     @Test
3055     public void testUnsignedMultiplyExactLongInt() {
3056         Assertions.assertThrows(ArithmeticException.class,
3057                                 () -> FastMath.unsignedMultiplyExact(-1L, 0));
3058         Assertions.assertThrows(ArithmeticException.class,
3059                                 () -> FastMath.unsignedMultiplyExact(0L, -1));
3060         Assertions.assertThrows(ArithmeticException.class,
3061                                 () -> FastMath.unsignedMultiplyExact(Long.MAX_VALUE, 2));
3062         Assertions.assertEquals(9223372036854775807L, FastMath.unsignedMultiplyExact(60247241209L, 153092023));
3063         Assertions.assertThrows(ArithmeticException.class,
3064                                 () -> FastMath.unsignedMultiplyExact(60247241209L, 153092024));
3065         Assertions.assertEquals(0, FastMath.unsignedMultiplyExact(0L, Integer.MAX_VALUE));
3066     }
3067 
3068     @Test
3069     public void testUnsignedMultiplyExactLongLong() {
3070         Assertions.assertThrows(ArithmeticException.class,
3071                                 () -> FastMath.unsignedMultiplyExact(Long.MAX_VALUE, 2L));
3072     }
3073 
3074     @Test
3075     public void testPowExactIntInt() {
3076         Assertions.assertThrows(ArithmeticException.class,
3077                                 () -> FastMath.powExact(21, 8));
3078         Assertions.assertEquals(1, FastMath.powExact(-2, 0));
3079         Assertions.assertEquals(1, FastMath.powExact( 2, 0));
3080         Assertions.assertEquals(0x1 << 30, FastMath.powExact(2, 30));
3081         Assertions.assertEquals( 1419857, FastMath.powExact( 17, 5));
3082         Assertions.assertEquals(-1419857, FastMath.powExact(-17, 5));
3083         Assertions.assertThrows(ArithmeticException.class,
3084                                 () -> FastMath.powExact(17, 9));
3085     }
3086 
3087     @Test
3088     public void testPowExactLongInt() {
3089         Assertions.assertThrows(ArithmeticException.class,
3090                                 () -> FastMath.powExact(21L, 15));
3091         Assertions.assertEquals(1L, FastMath.powExact(-2L, 0));
3092         Assertions.assertEquals(1L, FastMath.powExact( 2L, 0));
3093         Assertions.assertEquals(0x1L << 62, FastMath.powExact(2L, 62));
3094         Assertions.assertEquals( 118587876497L, FastMath.powExact( 17L, 9));
3095         Assertions.assertEquals(-118587876497L, FastMath.powExact(-17L, 9));
3096     }
3097 
3098     @Test
3099     public void testUnsignedPowExactIntInt() {
3100         Assertions.assertThrows(ArithmeticException.class,
3101                                 () -> FastMath.unsignedPowExact(21, 15));
3102         Assertions.assertThrows(ArithmeticException.class,
3103                                 () -> FastMath.unsignedPowExact(-1, 1));
3104         Assertions.assertThrows(ArithmeticException.class,
3105                                 () -> FastMath.unsignedPowExact(-1, 2));
3106         // when exponent is 0, we don't even check first argument is unsigned
3107         // this is for consistency with the Math class
3108         Assertions.assertEquals(1, FastMath.unsignedPowExact(-1, 0));
3109         Assertions.assertEquals( 1419857, FastMath.unsignedPowExact(17, 5));
3110         Assertions.assertEquals(24137569, FastMath.unsignedPowExact(17, 6));
3111         Assertions.assertThrows(ArithmeticException.class,
3112                                 () -> FastMath.unsignedPowExact(17, 8));
3113         Assertions.assertEquals(1, FastMath.unsignedPowExact(-1, 0));
3114     }
3115 
3116     @Test
3117     public void testUnsignedPowExactLongInt() {
3118         Assertions.assertThrows(ArithmeticException.class,
3119                                 () -> FastMath.unsignedPowExact(21L, 15));
3120         Assertions.assertThrows(ArithmeticException.class,
3121                                 () -> FastMath.unsignedPowExact(-1L, 1));
3122         Assertions.assertThrows(ArithmeticException.class,
3123                                 () -> FastMath.unsignedPowExact(-1L, 2));
3124         // when exponent is 0, we don't even check first argument is unsigned
3125         // this is for consistency with the Math class
3126         Assertions.assertEquals(1, FastMath.unsignedPowExact(-1L, 0));
3127         Assertions.assertEquals(       118587876497L, FastMath.powExact( 17L,  9));
3128         Assertions.assertEquals(      2015993900449L, FastMath.powExact( 17L, 10));
3129         Assertions.assertEquals(2862423051509815793L, FastMath.powExact( 17L, 15));
3130         Assertions.assertThrows(ArithmeticException.class,
3131                                 () -> FastMath.unsignedPowExact(17L, 16));
3132     }
3133 
3134     private static long poorManMultiplyHigh(final long p, final long q) {
3135 
3136         BigInteger bigP = BigInteger.valueOf(p);
3137         if (p < 0) {
3138             bigP = BigInteger.ONE.shiftLeft(128).add(bigP);
3139         }
3140 
3141         BigInteger bigQ = BigInteger.valueOf(q);
3142         if (q < 0) {
3143             bigQ = BigInteger.ONE.shiftLeft(128).add(bigQ);
3144         }
3145 
3146         return bigP.multiply(bigQ).shiftRight(64).longValue();
3147 
3148     }
3149 
3150     private static long poorManUnsignedMultiplyHigh(final long p, final long q) {
3151 
3152         BigInteger bigP = BigInteger.valueOf(p);
3153         if (p < 0) {
3154             bigP = BigInteger.ONE.shiftLeft(64).add(bigP);
3155         }
3156 
3157         BigInteger bigQ = BigInteger.valueOf(q);
3158         if (q < 0) {
3159             bigQ = BigInteger.ONE.shiftLeft(64).add(bigQ);
3160         }
3161 
3162         return bigP.multiply(bigQ).shiftRight(64).longValue();
3163 
3164     }
3165 
3166 }