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    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.numbers.core;
18  
19  import java.math.BigDecimal;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for examples contained in the user guide.
25   */
26  class UserGuideTest {
27  
28      @Test
29      void testNorm1() {
30          double x = Norm.EUCLIDEAN.of(3, -4);
31          double y = Norm.MANHATTAN.of(3, -4, 5);
32          double z = Norm.MAXIMUM.of(new double[] {3, -4, 5, -6, -7, -8});
33          Assertions.assertEquals(5, x);
34          Assertions.assertEquals(12, y);
35          Assertions.assertEquals(8, z);
36  
37          double big = Double.MAX_VALUE * 0.5;
38          double length = Norm.EUCLIDEAN.of(big, big, big);
39          double expected = Math.sqrt(0.5 * 0.5 * 3) * Double.MAX_VALUE;
40          Assertions.assertEquals(expected, length, Math.ulp(expected));
41      }
42  
43      @Test
44      void testSum1() {
45          double sum1 = Sum.create().add(1)
46                                    .addProduct(3, 4)
47                                    .getAsDouble();
48          double sum2 = Sum.of(1).addProduct(3, 4)
49                                 .getAsDouble();
50          double sum3 = Sum.ofProducts(new double[] {3, 4}, new double[] {5, 6})
51                           .getAsDouble();
52          Assertions.assertEquals(13, sum1);
53          Assertions.assertEquals(13, sum2);
54          Assertions.assertEquals(3 * 5 + 4 * 6, sum3);
55  
56          Assertions.assertEquals(Double.NaN, Sum.of(1, 2, Double.NaN).getAsDouble());
57          Assertions.assertEquals(Double.NEGATIVE_INFINITY, Sum.of(1, 2, Double.NEGATIVE_INFINITY).getAsDouble());
58      }
59  
60      @Test
61      void testSum2() {
62          double x1 = 1e100 + 1 - 2 - 1e100;
63          double x2 = Sum.of(1e100, 1, -2, -1e100).getAsDouble();
64          Assertions.assertEquals(0.0, x1);
65          Assertions.assertEquals(-1.0, x2);
66      }
67  
68      @Test
69      void testSum3() {
70          double x1 = 1e100 + 1 - 2 - 1e100;
71          Sum s1 = Sum.of(1e100, 1);
72          Sum s2 = Sum.of(2, 1e100);
73          double x2 = s1.subtract(s2).getAsDouble();
74          Assertions.assertEquals(0.0, x1);
75          Assertions.assertEquals(-1.0, x2);
76      }
77  
78      @Test
79      void testPrecision1() {
80          // Default allows no numbers between
81          Assertions.assertTrue(Precision.equals(1000.0, 1000.0));
82          Assertions.assertTrue(Precision.equals(1000.0, 1000.0 + Math.ulp(1000.0)));
83          Assertions.assertFalse(Precision.equals(1000.0, 1000.0 + 2 * Math.ulp(1000.0)));
84  
85          // Absolute - tolerance is floating-point
86          Assertions.assertFalse(Precision.equals(1000.0, 1001.0));
87          Assertions.assertTrue(Precision.equals(1000.0, 1001.0, 1.0));
88          Assertions.assertTrue(Precision.equals(1000.0, 1000.0 + Math.ulp(1000.0), 0.0));
89  
90          // ULP - tolerance is integer
91          Assertions.assertFalse(Precision.equals(1000.0, 1001.0));
92          Assertions.assertFalse(Precision.equals(1000.0, 1001.0, 1));
93          Assertions.assertFalse(Precision.equals(1000.0, 1000.0 + 2 * Math.ulp(1000.0), 1));
94          Assertions.assertTrue(Precision.equals(1000.0, 1000.0 + 2 * Math.ulp(1000.0), 2));
95          Assertions.assertFalse(Precision.equals(1000.0, 1000.0 + 3 * Math.ulp(1000.0), 2));
96  
97          // Relative
98          Assertions.assertFalse(Precision.equalsWithRelativeTolerance(1000.0, 1001.0, 1e-6));
99          Assertions.assertTrue(Precision.equalsWithRelativeTolerance(1000.0, 1001.0, 1e-3));
100     }
101 
102     @Test
103     void testPrecision2() {
104         Assertions.assertFalse(Precision.equals(Double.NaN, 1000.0));
105         Assertions.assertFalse(Precision.equals(Double.NaN, Double.NaN));
106         Assertions.assertTrue(Precision.equalsIncludingNaN(Double.NaN, Double.NaN));
107 
108         Assertions.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
109         Assertions.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
110         Assertions.assertFalse(Precision.equals(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
111     }
112 
113     @Test
114     void testPrecision3() {
115         Assertions.assertEquals(0, Precision.compareTo(100, 100, 0.0));
116         Assertions.assertEquals(0, Precision.compareTo(100, 101, 1.0));
117         Assertions.assertEquals(-1, Precision.compareTo(100, 102, 1.0));
118         Assertions.assertEquals(1, Precision.compareTo(102, 100, 1.0));
119     }
120 
121     @Test
122     void testPrecision4() {
123         Precision.DoubleEquivalence eq = Precision.doubleEquivalenceOfEpsilon(1.0);
124         Assertions.assertFalse(eq.lt(100, 100));
125         Assertions.assertTrue(eq.lte(100, 100));
126         Assertions.assertTrue(eq.eq(100, 100));
127         Assertions.assertTrue(eq.gte(100, 100));
128         Assertions.assertFalse(eq.gt(100, 100));
129     }
130 
131     @Test
132     void testPrecision5() {
133         Assertions.assertEquals(678.125, Precision.round(678.125, 4));
134         Assertions.assertEquals(678.125, Precision.round(678.125, 3));
135         Assertions.assertEquals(678.13, Precision.round(678.125, 2));
136         Assertions.assertEquals(678.1, Precision.round(678.125, 1));
137         Assertions.assertEquals(678.0, Precision.round(678.125, 0));
138         Assertions.assertEquals(680.0, Precision.round(678.125, -1));
139         Assertions.assertEquals(700.0, Precision.round(678.125, -2));
140 
141         Assertions.assertEquals(0.10000000000000009, Precision.representableDelta(1.0, 0.1));
142     }
143 
144     @Test
145     void testDD1() {
146         double x = Math.PI;
147         int    y = 42;
148         long   z = -8564728970587006436L;
149         Assertions.assertEquals(x, DD.of(x).doubleValue());
150         Assertions.assertEquals(y, DD.of(y).intValue());
151         Assertions.assertEquals(z, DD.of(z).longValue());
152         Assertions.assertNotEquals(z, (long) (double) z);
153     }
154 
155     @Test
156     void testDD2() {
157         BigDecimal pi = new BigDecimal("3.14159265358979323846264338327950288419716939937510");
158         DD x = DD.from(pi);
159         Assertions.assertEquals("(3.141592653589793,1.2246467991473532E-16)", x.toString());
160         Assertions.assertNotEquals(0, pi.compareTo(x.bigDecimalValue()));
161         Assertions.assertEquals(Math.PI, x.hi());
162         Assertions.assertEquals(pi.subtract(new BigDecimal(Math.PI)).doubleValue(), x.lo());
163 
164         DD nan = DD.of(Double.NaN);
165         Assertions.assertFalse(nan.isFinite());
166         Assertions.assertThrows(NumberFormatException.class, () -> nan.bigDecimalValue());
167     }
168 
169     @Test
170     void testDD3() {
171         long   x = -8564728970587006436L;
172         Assertions.assertNotEquals(x + 1, DD.ONE.add(x).longValue());
173         Assertions.assertEquals(x + 1, DD.ONE.add(DD.of(x)).longValue());
174     }
175 
176     @Test
177     void testDD4() {
178         double a = 1.2345678901234567;
179         double b = 123.45678901234567;
180         DD w = DD.ofProduct(a, b);
181         DD x = DD.ofSum(a, b);
182         DD y = DD.ofDifference(a, b);
183         DD z = DD.fromQuotient(1, 3);
184         Assertions.assertEquals("(152.41578753238835,-1.0325951435749745E-14)", w.toString());
185         Assertions.assertEquals("(124.69135690246912,-1.1102230246251565E-15)", x.toString());
186         Assertions.assertEquals("(-122.22222112222221,-1.1102230246251565E-15)", y.toString());
187         Assertions.assertEquals("(0.3333333333333333,1.850371707708594E-17)", z.toString());
188         Assertions.assertEquals(a * b, w.hi());
189         Assertions.assertEquals(a + b, x.hi());
190         Assertions.assertEquals(a - b, y.hi());
191         Assertions.assertEquals(1.0 / 3, z.hi());
192 
193         DD zz = DD.of(1).divide(DD.of(3));
194         Assertions.assertEquals(z, zz);
195     }
196 
197     @Test
198     void testDD5() {
199         Assertions.assertEquals(0.9999999999999999, 1.0 / 2 + 1.0 / 3 + 1.0 / 6);
200         DD z = DD.fromQuotient(1, 2)
201                  .add(DD.fromQuotient(1, 3))
202                  .add(DD.fromQuotient(1, 6));
203         Assertions.assertEquals("(1.0,-4.622231866529366E-33)", z.toString());
204         Assertions.assertEquals(1.0, z.doubleValue());
205     }
206 
207     @Test
208     void testDD6() {
209         double a = 1;
210         double b = Math.pow(2, 53);
211         double c = Math.pow(2, 106);
212         DD z = DD.of(a).add(b).add(c).subtract(c).subtract(b);
213         Assertions.assertEquals(0.0, z.doubleValue());
214     }
215 
216     @Test
217     void testDD7() {
218         double a = 1.5 * Math.pow(2, 1023);
219         double b = 4 * Math.pow(2, -1022);
220         DD x = DD.of(a);
221         DD y = DD.of(b);
222         Assertions.assertFalse(x.multiply(y).isFinite());
223 
224         // Create fractional representation as [0.5, 1) * 2^b
225         int[] xb = {0};
226         int[] yb = {0};
227         x = x.frexp(xb);       // (0.75, 0) * 2^1024
228         y = y.frexp(yb);       // (0.5, 0)  * 2^-1019
229         Assertions.assertEquals(0.75, x.doubleValue());
230         Assertions.assertEquals(0.5, y.doubleValue());
231         Assertions.assertEquals(1024, xb[0]);
232         Assertions.assertEquals(-1019, yb[0]);
233 
234         DD z = x.multiply(y);  // (0.375, 0)
235         Assertions.assertEquals(0.375, z.doubleValue());
236         // Rescale by 2^5
237         Assertions.assertEquals(a * b, z.scalb(xb[0] + yb[0]).doubleValue());
238     }
239 }