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.field;
18  
19  import java.util.List;
20  import java.util.function.BiPredicate;
21  import java.util.stream.Stream;
22  import java.util.ArrayList;
23  
24  import org.apache.commons.numbers.fraction.Fraction;
25  import org.apache.commons.numbers.core.DD;
26  import org.apache.commons.numbers.core.Precision;
27  import org.apache.commons.numbers.fraction.BigFraction;
28  
29  import org.junit.jupiter.api.Assertions;
30  
31  /**
32   * List of fields.
33   */
34  final class FieldsList {
35      /** List of all fields implemented in the library. */
36      private static final List<FieldTestData<?>> LIST =
37              new ArrayList<>();
38  
39      static {
40          try {
41              // List of fields to test.
42              add(FractionField.get(),
43                  Fraction.of(13, 4),
44                  Fraction.of(5, 29),
45                  Fraction.of(-279, 11));
46              add(BigFractionField.get(),
47                  BigFraction.of(13256093L, 43951044L),
48                  BigFraction.of(543016315L, 29L),
49                  BigFraction.of(-27930919051L, 11L));
50              add(FP64Field.get(),
51                  FP64.of(23.45678901),
52                  FP64.of(-543.2109876),
53                  FP64.of(-234.5678901),
54                  // double operations are subject to rounding so allow a tolerance
55                  (x, y) -> Precision.equals(x.doubleValue(), y.doubleValue(), 1));
56              add(DDField.get(),
57                  createDD(23.45678901, 4.5671892973),
58                  createDD(-543.2109876, 5.237848286),
59                  createDD(-234.5678901, -4.561268179),
60                  // double-double operations are subject to rounding so allow a tolerance.
61                  FieldsList::areEqual);
62              // Pass through a second DD that uses a signed negative zero
63              add(DDField.get(),
64                  createDD(2.45678901, 45.671892973),
65                  createDD(-54.32109876, 52.37848286),
66                  createDD(-0.0, -0.0),
67                  // double-double operations are subject to rounding so allow a tolerance.
68                  FieldsList::areEqual);
69          } catch (Exception e) {
70              e.printStackTrace(System.err);
71              throw new RuntimeException(e);
72          }
73      }
74  
75      private FieldsList() {}
76  
77      /**
78       * @param field Field.
79       * @param a Field element.
80       * @param b Field element.
81       * @param c Field element.
82       */
83      private static <T> void add(Field<T> field,
84                                  T a,
85                                  T b,
86                                  T c) {
87          LIST.add(new FieldTestData<>(field, a, b, c));
88      }
89  
90      /**
91       * @param field Field.
92       * @param a Field element.
93       * @param b Field element.
94       * @param c Field element.
95       * @param equals Field equality predicate.
96       */
97      private static <T> void add(Field<T> field,
98                                  T a,
99                                  T b,
100                                 T c,
101                                 BiPredicate<T, T> equals) {
102         LIST.add(new FieldTestData<>(field, a, b, c, equals));
103     }
104 
105     /**
106      * Creates the double-double number from two random values.
107      * The second value is scaled so that it does not overlap the first.
108      *
109      * @param a Value.
110      * @param b Value.
111      * @return the number
112      */
113     private static DD createDD(double a, double b) {
114         final int ea = Math.getExponent(a);
115         final int eb = Math.getExponent(b);
116         // Scale to have a non-overlapping 53-bit mantissa
117         double bb = Math.scalb(b, ea - eb - 53);
118         // If b has a larger mantissa than a (ignoring the exponent) then an overlap may occur
119         if (a != a + bb) {
120             bb *= 0.5;
121         }
122         Assertions.assertEquals(a, a + bb);
123         return DD.ofSum(a, bb);
124     }
125 
126     /**
127      * Test if the two numbers are equal.
128      *
129      * @param x Value.
130      * @param y Value.
131      * @return true if equal
132      */
133     private static boolean areEqual(DD x, DD y) {
134         // A simple binary equality is fine for most cases.
135         if (x.equals(y)) {
136             return true;
137         }
138         // If the high part is the same we can test a ULP tolerance on the low part.
139         if (x.hi() == y.hi() && Precision.equals(x.lo(), y.lo(), 1)) {
140             return true;
141         }
142         // Note that the high part could be different by 1 ulp and then the low part
143         // can be significantly different (opposite signed values) to create numbers that
144         // are almost the same: x+xx ~ y-yy.
145         // Here we obtain the difference and use a relative error of 2^-105:
146         //  | x - y |
147         // ------------   <=  relative error
148         // max(|x|, |y|)
149         return Math.abs(x.subtract(y).doubleValue()) /
150             Math.max(Math.abs(x.doubleValue()), Math.abs(y.doubleValue())) <= Math.pow(2, -105);
151     }
152 
153     /**
154      * Subclasses that are "parametric" tests can forward the call to
155      * the "@Parameters"-annotated method to this method.
156      *
157      * @return the stream of all fields.
158      */
159     static Stream<FieldTestData<?>> stream() {
160         return LIST.stream();
161     }
162 }