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.CalculusFieldElementAbstractTest;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class Binary64Test extends CalculusFieldElementAbstractTest<Binary64> {
29      public static final double X = 1.2345;
30  
31      public static final Binary64 PLUS_X = new Binary64(X);
32  
33      public static final Binary64 MINUS_X = new Binary64(-X);
34  
35      public static final double Y = 6.789;
36  
37      public static final Binary64 PLUS_Y = new Binary64(Y);
38  
39      public static final Binary64 MINUS_Y = new Binary64(-Y);
40  
41      public static final Binary64 PLUS_ZERO = new Binary64(0.0);
42  
43      public static final Binary64 MINUS_ZERO = new Binary64(-0.0);
44  
45      @Override
46      protected Binary64 build(final double x) {
47          return new Binary64(x);
48      }
49  
50      @Test
51      public void testAdd() {
52          Binary64 expected, actual;
53  
54          expected = new Binary64(X + Y);
55          actual = PLUS_X.add(PLUS_Y);
56          Assert.assertEquals(expected, actual);
57          actual = PLUS_Y.add(PLUS_X);
58          Assert.assertEquals(expected, actual);
59  
60          expected = new Binary64(X + (-Y));
61          actual = PLUS_X.add(MINUS_Y);
62          Assert.assertEquals(expected, actual);
63          actual = MINUS_Y.add(PLUS_X);
64          Assert.assertEquals(expected, actual);
65  
66          expected = new Binary64((-X) + (-Y));
67          actual = MINUS_X.add(MINUS_Y);
68          Assert.assertEquals(expected, actual);
69          actual = MINUS_Y.add(MINUS_X);
70          Assert.assertEquals(expected, actual);
71  
72          expected = Binary64.POSITIVE_INFINITY;
73          actual = PLUS_X.add(Binary64.POSITIVE_INFINITY);
74          Assert.assertEquals(expected, actual);
75          actual = Binary64.POSITIVE_INFINITY.add(PLUS_X);
76          Assert.assertEquals(expected, actual);
77          actual = MINUS_X.add(Binary64.POSITIVE_INFINITY);
78          Assert.assertEquals(expected, actual);
79          actual = Binary64.POSITIVE_INFINITY.add(MINUS_X);
80          Assert.assertEquals(expected, actual);
81          actual = Binary64.POSITIVE_INFINITY.add(Binary64.POSITIVE_INFINITY);
82          Assert.assertEquals(expected, actual);
83  
84          expected = Binary64.NEGATIVE_INFINITY;
85          actual = PLUS_X.add(Binary64.NEGATIVE_INFINITY);
86          Assert.assertEquals(expected, actual);
87          actual = Binary64.NEGATIVE_INFINITY.add(PLUS_X);
88          Assert.assertEquals(expected, actual);
89          actual = Binary64.NEGATIVE_INFINITY.add(Binary64.NEGATIVE_INFINITY);
90          Assert.assertEquals(expected, actual);
91          actual = MINUS_X.add(Binary64.NEGATIVE_INFINITY);
92          Assert.assertEquals(expected, actual);
93          actual = Binary64.NEGATIVE_INFINITY.add(MINUS_X);
94          Assert.assertEquals(expected, actual);
95  
96          expected = Binary64.NAN;
97          actual = Binary64.POSITIVE_INFINITY.add(Binary64.NEGATIVE_INFINITY);
98          Assert.assertEquals(expected, actual);
99          actual = Binary64.NEGATIVE_INFINITY.add(Binary64.POSITIVE_INFINITY);
100         Assert.assertEquals(expected, actual);
101         actual = PLUS_X.add(Binary64.NAN);
102         Assert.assertEquals(expected, actual);
103         actual = Binary64.NAN.add(PLUS_X);
104         Assert.assertEquals(expected, actual);
105         actual = MINUS_X.add(Binary64.NAN);
106         Assert.assertEquals(expected, actual);
107         actual = Binary64.NAN.add(MINUS_X);
108         Assert.assertEquals(expected, actual);
109         actual = Binary64.POSITIVE_INFINITY.add(Binary64.NAN);
110         Assert.assertEquals(expected, actual);
111         actual = Binary64.NAN.add(Binary64.POSITIVE_INFINITY);
112         Assert.assertEquals(expected, actual);
113         actual = Binary64.NEGATIVE_INFINITY.add(Binary64.NAN);
114         Assert.assertEquals(expected, actual);
115         actual = Binary64.NAN.add(Binary64.NEGATIVE_INFINITY);
116         Assert.assertEquals(expected, actual);
117         actual = Binary64.NAN.add(Binary64.NAN);
118         Assert.assertEquals(expected, actual);
119     }
120 
121     @Test
122     public void testSubtract() {
123         Binary64 expected, actual;
124 
125         expected = new Binary64(X - Y);
126         actual = PLUS_X.subtract(PLUS_Y);
127         Assert.assertEquals(expected, actual);
128 
129         expected = new Binary64(X - (-Y));
130         actual = PLUS_X.subtract(MINUS_Y);
131         Assert.assertEquals(expected, actual);
132 
133         expected = new Binary64((-X) - Y);
134         actual = MINUS_X.subtract(PLUS_Y);
135         Assert.assertEquals(expected, actual);
136 
137         expected = new Binary64((-X) - (-Y));
138         actual = MINUS_X.subtract(MINUS_Y);
139         Assert.assertEquals(expected, actual);
140 
141         expected = Binary64.NEGATIVE_INFINITY;
142         actual = PLUS_X.subtract(Binary64.POSITIVE_INFINITY);
143         Assert.assertEquals(expected, actual);
144         actual = MINUS_X.subtract(Binary64.POSITIVE_INFINITY);
145         Assert.assertEquals(expected, actual);
146         actual = Binary64.NEGATIVE_INFINITY
147                 .subtract(Binary64.POSITIVE_INFINITY);
148         Assert.assertEquals(expected, actual);
149 
150         expected = Binary64.POSITIVE_INFINITY;
151         actual = PLUS_X.subtract(Binary64.NEGATIVE_INFINITY);
152         Assert.assertEquals(expected, actual);
153         actual = MINUS_X.subtract(Binary64.NEGATIVE_INFINITY);
154         Assert.assertEquals(expected, actual);
155         actual = Binary64.POSITIVE_INFINITY
156                 .subtract(Binary64.NEGATIVE_INFINITY);
157         Assert.assertEquals(expected, actual);
158 
159         expected = Binary64.NAN;
160         actual = Binary64.POSITIVE_INFINITY
161                 .subtract(Binary64.POSITIVE_INFINITY);
162         Assert.assertEquals(expected, actual);
163         actual = Binary64.NEGATIVE_INFINITY
164                 .subtract(Binary64.NEGATIVE_INFINITY);
165         Assert.assertEquals(expected, actual);
166         actual = PLUS_X.subtract(Binary64.NAN);
167         Assert.assertEquals(expected, actual);
168         actual = Binary64.NAN.subtract(PLUS_X);
169         Assert.assertEquals(expected, actual);
170         actual = MINUS_X.subtract(Binary64.NAN);
171         Assert.assertEquals(expected, actual);
172         actual = Binary64.NAN.subtract(MINUS_X);
173         Assert.assertEquals(expected, actual);
174         actual = Binary64.POSITIVE_INFINITY.subtract(Binary64.NAN);
175         Assert.assertEquals(expected, actual);
176         actual = Binary64.NAN.subtract(Binary64.POSITIVE_INFINITY);
177         Assert.assertEquals(expected, actual);
178         actual = Binary64.NEGATIVE_INFINITY.subtract(Binary64.NAN);
179         Assert.assertEquals(expected, actual);
180         actual = Binary64.NAN.subtract(Binary64.NEGATIVE_INFINITY);
181         Assert.assertEquals(expected, actual);
182         actual = Binary64.NAN.subtract(Binary64.NAN);
183         Assert.assertEquals(expected, actual);
184     }
185 
186     @Test
187     public void testNegate() {
188         Binary64 expected, actual;
189 
190         expected = MINUS_X;
191         actual = PLUS_X.negate();
192         Assert.assertEquals(expected, actual);
193 
194         expected = PLUS_X;
195         actual = MINUS_X.negate();
196         Assert.assertEquals(expected, actual);
197 
198         expected = MINUS_ZERO;
199         actual = PLUS_ZERO.negate();
200         Assert.assertEquals(expected, actual);
201 
202         expected = PLUS_ZERO;
203         actual = MINUS_ZERO.negate();
204         Assert.assertEquals(expected, actual);
205 
206         expected = Binary64.POSITIVE_INFINITY;
207         actual = Binary64.NEGATIVE_INFINITY.negate();
208         Assert.assertEquals(expected, actual);
209 
210         expected = Binary64.NEGATIVE_INFINITY;
211         actual = Binary64.POSITIVE_INFINITY.negate();
212         Assert.assertEquals(expected, actual);
213 
214         expected = Binary64.NAN;
215         actual = Binary64.NAN.negate();
216         Assert.assertEquals(expected, actual);
217     }
218 
219     @Test
220     public void testMultiply() {
221         Binary64 expected, actual;
222 
223         expected = new Binary64(X * Y);
224         actual = PLUS_X.multiply(PLUS_Y);
225         Assert.assertEquals(expected, actual);
226         actual = PLUS_Y.multiply(PLUS_X);
227         Assert.assertEquals(expected, actual);
228 
229         expected = new Binary64(X * (-Y));
230         actual = PLUS_X.multiply(MINUS_Y);
231         Assert.assertEquals(expected, actual);
232         actual = MINUS_Y.multiply(PLUS_X);
233         Assert.assertEquals(expected, actual);
234 
235         expected = new Binary64((-X) * (-Y));
236         actual = MINUS_X.multiply(MINUS_Y);
237         Assert.assertEquals(expected, actual);
238         actual = MINUS_Y.multiply(MINUS_X);
239         Assert.assertEquals(expected, actual);
240 
241         expected = Binary64.POSITIVE_INFINITY;
242         actual = PLUS_X.multiply(Binary64.POSITIVE_INFINITY);
243         Assert.assertEquals(expected, actual);
244         actual = Binary64.POSITIVE_INFINITY.multiply(PLUS_X);
245         Assert.assertEquals(expected, actual);
246         actual = MINUS_X.multiply(Binary64.NEGATIVE_INFINITY);
247         Assert.assertEquals(expected, actual);
248         actual = Binary64.NEGATIVE_INFINITY.multiply(MINUS_X);
249         Assert.assertEquals(expected, actual);
250         actual = Binary64.POSITIVE_INFINITY
251                 .multiply(Binary64.POSITIVE_INFINITY);
252         Assert.assertEquals(expected, actual);
253         actual = Binary64.NEGATIVE_INFINITY
254                 .multiply(Binary64.NEGATIVE_INFINITY);
255         Assert.assertEquals(expected, actual);
256 
257         expected = Binary64.NEGATIVE_INFINITY;
258         actual = PLUS_X.multiply(Binary64.NEGATIVE_INFINITY);
259         Assert.assertEquals(expected, actual);
260         actual = Binary64.NEGATIVE_INFINITY.multiply(PLUS_X);
261         Assert.assertEquals(expected, actual);
262         actual = MINUS_X.multiply(Binary64.POSITIVE_INFINITY);
263         Assert.assertEquals(expected, actual);
264         actual = Binary64.POSITIVE_INFINITY.multiply(MINUS_X);
265         Assert.assertEquals(expected, actual);
266         actual = Binary64.POSITIVE_INFINITY
267                 .multiply(Binary64.NEGATIVE_INFINITY);
268         Assert.assertEquals(expected, actual);
269         actual = Binary64.NEGATIVE_INFINITY
270                 .multiply(Binary64.POSITIVE_INFINITY);
271         Assert.assertEquals(expected, actual);
272 
273         expected = Binary64.NAN;
274         actual = PLUS_X.multiply(Binary64.NAN);
275         Assert.assertEquals(expected, actual);
276         actual = Binary64.NAN.multiply(PLUS_X);
277         Assert.assertEquals(expected, actual);
278         actual = MINUS_X.multiply(Binary64.NAN);
279         Assert.assertEquals(expected, actual);
280         actual = Binary64.NAN.multiply(MINUS_X);
281         Assert.assertEquals(expected, actual);
282         actual = Binary64.POSITIVE_INFINITY.multiply(Binary64.NAN);
283         Assert.assertEquals(expected, actual);
284         actual = Binary64.NAN.multiply(Binary64.POSITIVE_INFINITY);
285         Assert.assertEquals(expected, actual);
286         actual = Binary64.NEGATIVE_INFINITY.multiply(Binary64.NAN);
287         Assert.assertEquals(expected, actual);
288         actual = Binary64.NAN.multiply(Binary64.NEGATIVE_INFINITY);
289         Assert.assertEquals(expected, actual);
290         actual = Binary64.NAN.multiply(Binary64.NAN);
291         Assert.assertEquals(expected, actual);
292     }
293 
294     @Test
295     public void testDivide() {
296         Binary64 expected, actual;
297 
298         expected = new Binary64(X / Y);
299         actual = PLUS_X.divide(PLUS_Y);
300         Assert.assertEquals(expected, actual);
301 
302         expected = new Binary64(X / (-Y));
303         actual = PLUS_X.divide(MINUS_Y);
304         Assert.assertEquals(expected, actual);
305 
306         expected = new Binary64((-X) / Y);
307         actual = MINUS_X.divide(PLUS_Y);
308         Assert.assertEquals(expected, actual);
309 
310         expected = new Binary64((-X) / (-Y));
311         actual = MINUS_X.divide(MINUS_Y);
312         Assert.assertEquals(expected, actual);
313 
314         expected = PLUS_ZERO;
315         actual = PLUS_X.divide(Binary64.POSITIVE_INFINITY);
316         Assert.assertEquals(expected, actual);
317         actual = MINUS_X.divide(Binary64.NEGATIVE_INFINITY);
318         Assert.assertEquals(expected, actual);
319 
320         expected = MINUS_ZERO;
321         actual = MINUS_X.divide(Binary64.POSITIVE_INFINITY);
322         Assert.assertEquals(expected, actual);
323         actual = PLUS_X.divide(Binary64.NEGATIVE_INFINITY);
324         Assert.assertEquals(expected, actual);
325 
326         expected = Binary64.POSITIVE_INFINITY;
327         actual = Binary64.POSITIVE_INFINITY.divide(PLUS_X);
328         Assert.assertEquals(expected, actual);
329         actual = Binary64.NEGATIVE_INFINITY.divide(MINUS_X);
330         Assert.assertEquals(expected, actual);
331         actual = PLUS_X.divide(PLUS_ZERO);
332         Assert.assertEquals(expected, actual);
333         actual = MINUS_X.divide(MINUS_ZERO);
334         Assert.assertEquals(expected, actual);
335 
336         expected = Binary64.NEGATIVE_INFINITY;
337         actual = Binary64.POSITIVE_INFINITY.divide(MINUS_X);
338         Assert.assertEquals(expected, actual);
339         actual = Binary64.NEGATIVE_INFINITY.divide(PLUS_X);
340         Assert.assertEquals(expected, actual);
341         actual = PLUS_X.divide(MINUS_ZERO);
342         Assert.assertEquals(expected, actual);
343         actual = MINUS_X.divide(PLUS_ZERO);
344         Assert.assertEquals(expected, actual);
345 
346         expected = Binary64.NAN;
347         actual = Binary64.POSITIVE_INFINITY
348                 .divide(Binary64.POSITIVE_INFINITY);
349         Assert.assertEquals(expected, actual);
350         actual = Binary64.POSITIVE_INFINITY
351                 .divide(Binary64.NEGATIVE_INFINITY);
352         Assert.assertEquals(expected, actual);
353         actual = Binary64.NEGATIVE_INFINITY
354                 .divide(Binary64.POSITIVE_INFINITY);
355         Assert.assertEquals(expected, actual);
356         actual = Binary64.NEGATIVE_INFINITY
357                 .divide(Binary64.NEGATIVE_INFINITY);
358         Assert.assertEquals(expected, actual);
359         actual = PLUS_X.divide(Binary64.NAN);
360         Assert.assertEquals(expected, actual);
361         actual = Binary64.NAN.divide(PLUS_X);
362         Assert.assertEquals(expected, actual);
363         actual = MINUS_X.divide(Binary64.NAN);
364         Assert.assertEquals(expected, actual);
365         actual = Binary64.NAN.divide(MINUS_X);
366         Assert.assertEquals(expected, actual);
367         actual = Binary64.POSITIVE_INFINITY.divide(Binary64.NAN);
368         Assert.assertEquals(expected, actual);
369         actual = Binary64.NAN.divide(Binary64.POSITIVE_INFINITY);
370         Assert.assertEquals(expected, actual);
371         actual = Binary64.NEGATIVE_INFINITY.divide(Binary64.NAN);
372         Assert.assertEquals(expected, actual);
373         actual = Binary64.NAN.divide(Binary64.NEGATIVE_INFINITY);
374         Assert.assertEquals(expected, actual);
375         actual = Binary64.NAN.divide(Binary64.NAN);
376         Assert.assertEquals(expected, actual);
377         actual = PLUS_ZERO.divide(PLUS_ZERO);
378         Assert.assertEquals(expected, actual);
379         actual = PLUS_ZERO.divide(MINUS_ZERO);
380         Assert.assertEquals(expected, actual);
381         actual = MINUS_ZERO.divide(PLUS_ZERO);
382         Assert.assertEquals(expected, actual);
383         actual = MINUS_ZERO.divide(MINUS_ZERO);
384         Assert.assertEquals(expected, actual);
385     }
386 
387     @Test
388     public void testReciprocal() {
389         Binary64 expected, actual;
390 
391         expected = new Binary64(1.0 / X);
392         actual = PLUS_X.reciprocal();
393         Assert.assertEquals(expected, actual);
394 
395         expected = new Binary64(1.0 / (-X));
396         actual = MINUS_X.reciprocal();
397         Assert.assertEquals(expected, actual);
398 
399         expected = PLUS_ZERO;
400         actual = Binary64.POSITIVE_INFINITY.reciprocal();
401         Assert.assertEquals(expected, actual);
402 
403         expected = MINUS_ZERO;
404         actual = Binary64.NEGATIVE_INFINITY.reciprocal();
405         Assert.assertEquals(expected, actual);
406     }
407 
408     @Test
409     public void testIsInfinite() {
410         Assert.assertFalse(MINUS_X.isInfinite());
411         Assert.assertFalse(PLUS_X.isInfinite());
412         Assert.assertFalse(MINUS_Y.isInfinite());
413         Assert.assertFalse(PLUS_Y.isInfinite());
414         Assert.assertFalse(Binary64.NAN.isInfinite());
415 
416         Assert.assertTrue(Binary64.NEGATIVE_INFINITY.isInfinite());
417         Assert.assertTrue(Binary64.POSITIVE_INFINITY.isInfinite());
418     }
419 
420     @Test
421     public void testIsNaN() {
422         Assert.assertFalse(MINUS_X.isNaN());
423         Assert.assertFalse(PLUS_X.isNaN());
424         Assert.assertFalse(MINUS_Y.isNaN());
425         Assert.assertFalse(PLUS_Y.isNaN());
426         Assert.assertFalse(Binary64.NEGATIVE_INFINITY.isNaN());
427         Assert.assertFalse(Binary64.POSITIVE_INFINITY.isNaN());
428 
429         Assert.assertTrue(Binary64.NAN.isNaN());
430     }
431 
432     @Test
433     public void testToDegreesDefinition() {
434         double epsilon = 3.0e-16;
435         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
436             for (double x = 0.1; x < 1.2; x += 0.001) {
437                 Binary64 value = new Binary64(x);
438                 Assert.assertEquals(FastMath.toDegrees(x), value.toDegrees().getReal(), epsilon);
439             }
440         }
441     }
442 
443     @Test
444     public void testToRadiansDefinition() {
445         double epsilon = 3.0e-16;
446         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
447             for (double x = 0.1; x < 1.2; x += 0.001) {
448                 Binary64 value = new Binary64(x);
449                 Assert.assertEquals(FastMath.toRadians(x), value.toRadians().getReal(), epsilon);
450             }
451         }
452     }
453 
454     @Test
455     public void testDegRad() {
456         for (double x = 0.1; x < 1.2; x += 0.001) {
457             Binary64 value = new Binary64(x);
458             Binary64 rebuilt = value.toDegrees().toRadians();
459             Binary64 zero = rebuilt.subtract(value);
460             Assert.assertEquals(zero.getReal(), 0, 3.0e-16);
461         }
462     }
463 
464     @Test
465     public void testRootNegative() {
466         final Binary64 neg64      = new Binary64(-64);
467         final Binary64 root3Neg64 = neg64.rootN(3);
468         final Binary64 root2Neg64 = neg64.rootN(2);
469         Assert.assertEquals(-4.0, root3Neg64.getReal(), 1.0e-15);
470         Assert.assertTrue(root2Neg64.isNaN());
471     }
472 
473     @Test
474     public void testSignedZeroEquality() {
475         Assert.assertFalse(new Binary64(1.0).isZero());
476         Assert.assertTrue(new Binary64(-0.0).isZero());
477         Assert.assertTrue(new Binary64(+0.0).isZero());
478         Assert.assertFalse(new Binary64(+0.0).equals(new Binary64(-0.0)));
479     }
480 
481     @Test
482     public void testValues() {
483         Assert.assertEquals(1,    new Binary64(1.2).byteValue());
484         Assert.assertEquals(1,    new Binary64(1.2).shortValue());
485         Assert.assertEquals(1,    new Binary64(1.2).intValue());
486         Assert.assertEquals(1l,   new Binary64(1.2).longValue());
487         Assert.assertEquals(1.2f, new Binary64(1.2).floatValue(),  0.00001f);
488         Assert.assertEquals(1.2 , new Binary64(1.2).doubleValue(), 1.0e-15);
489     }
490 
491     @SuppressWarnings("unlikely-arg-type")
492     @Test
493     public void testEquals() {
494         Assert.assertTrue(new Binary64(1.25).equals(new Binary64(1.0).add(new Binary64(0.25))));
495         Assert.assertFalse(new Binary64(1.25).equals(new Binary64(1.0).add(new Binary64(1.25))));
496         Assert.assertFalse(new Binary64(1.25).equals("1.25"));
497     }
498 
499 }