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
70 } catch (Exception e) {
71 e.printStackTrace(System.err);
72 throw new RuntimeException(e);
73 }
74 }
75
76 private FieldsList() {}
77
78 /**
79 * @param field Field.
80 * @param a Field element.
81 * @param b Field element.
82 * @param c Field element.
83 */
84 private static <T> void add(Field<T> field,
85 T a,
86 T b,
87 T c) {
88 LIST.add(new FieldTestData<>(field, a, b, c));
89 }
90
91 /**
92 * @param field Field.
93 * @param a Field element.
94 * @param b Field element.
95 * @param c Field element.
96 * @param equals Field equality predicate.
97 */
98 private static <T> void add(Field<T> field,
99 T a,
100 T b,
101 T c,
102 BiPredicate<T, T> equals) {
103 LIST.add(new FieldTestData<>(field, a, b, c, equals));
104 }
105
106 /**
107 * Creates the double-double number from two random values.
108 * The second value is scaled so that it does not overlap the first.
109 *
110 * @param a Value.
111 * @param b Value.
112 * @return the number
113 */
114 private static DD createDD(double a, double b) {
115 final int ea = Math.getExponent(a);
116 final int eb = Math.getExponent(b);
117 // Scale to have a non-overlapping 53-bit mantissa
118 double bb = Math.scalb(b, ea - eb - 53);
119 // If b has a larger mantissa than a (ignoring the exponent) then an overlap may occur
120 if (a != a + bb) {
121 bb *= 0.5;
122 }
123 Assertions.assertEquals(a, a + bb);
124 return DD.ofSum(a, bb);
125 }
126
127 /**
128 * Test if the two numbers are equal.
129 *
130 * @param x Value.
131 * @param y Value.
132 * @return true if equal
133 */
134 private static boolean areEqual(DD x, DD y) {
135 // A simple binary equality is fine for most cases.
136 if (x.equals(y)) {
137 return true;
138 }
139 // If the high part is the same we can test a ULP tolerance on the low part.
140 if (x.hi() == y.hi() && Precision.equals(x.lo(), y.lo(), 1)) {
141 return true;
142 }
143 // Note that the high part could be different by 1 ulp and then the low part
144 // can be significantly different (opposite signed values) to create numbers that
145 // are almost the same: x+xx ~ y-yy.
146 // Here we obtain the difference and use a relative error of 2^-105:
147 // | x - y |
148 // ------------ <= relative error
149 // max(|x|, |y|)
150 return Math.abs(x.subtract(y).doubleValue()) /
151 Math.max(Math.abs(x.doubleValue()), Math.abs(y.doubleValue())) <= Math.pow(2, -105);
152 }
153
154 /**
155 * Subclasses that are "parametric" tests can forward the call to
156 * the "@Parameters"-annotated method to this method.
157 *
158 * @return the stream of all fields.
159 */
160 static Stream<FieldTestData<?>> stream() {
161 return LIST.stream();
162 }
163 }