Skip to content

Commit 2275353

Browse files
author
Gilles Sadowski
committed
Class "JdkMath" bridges user code and alternative implementations.
It's a "drop-in" replacement for JDK's "Math" as of Java 8 (cf. MATH-1630). "AccurateMath" contains pure Java, acccurate, implementations of "Math" functions. But it is no longer required to implement all of them.
1 parent b351676 commit 2275353

4 files changed

Lines changed: 80 additions & 36 deletions

File tree

commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
* to the various JVM optimizations that have appeared since Java 5).
3030
* However, any change to this class should ensure that the current
3131
* accuracy is not lost.
32-
* <p>
33-
* AccurateMath is a drop-in replacement for both Math and StrictMath.
34-
* For example, a call to {@code Math.sin(x)} can be replaced by a call
35-
* to {@code AccurateMath.sin(x)}.
3632
* </p>
3733
* <p>
3834
* AccurateMath speed is achieved by relying heavily on optimizing compilers

commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/JdkMath.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@
2929

3030
/**
3131
* Wrapper for alternative implementations of {@link Math} functions.
32+
* For example, a call to {@code Math.sin(x)} can be replaced by a call
33+
* to {@code JdkMath.sin(x)}.
3234
*
35+
* <p>
36+
* This class is a "drop-in" replacement for both Math and StrictMath,
37+
* up to the <em>minimal</em> JDK version required by this library
38+
* (meaning that, although the library can be used on <em>more</em>
39+
* recent JVMs, the {@code JdkMath} class may be missing the methods
40+
* that were absent in older JDKs).
41+
*
42+
* <p>
3343
* Based on the value, at class initialization, of the system property
3444
* {@code org.apache.commons.math.jdkmath}, this class redirects to a
3545
* specific implementation:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.math4.core.jdkmath;
18+
19+
import java.util.List;
20+
import java.util.ArrayList;
21+
import java.lang.reflect.Method;
22+
import java.lang.reflect.Modifier;
23+
import java.lang.reflect.Type;
24+
25+
import org.junit.Test;
26+
import org.junit.Assert;
27+
28+
/**
29+
* Tests for {@link JdkMath}.
30+
*/
31+
public class JdkMathTest {
32+
/** Separator. */
33+
private static final String LINE_SEP = System.lineSeparator();
34+
35+
@Test
36+
public void checkMissingMethods() {
37+
final List<String> notFound = compareClassMethods(StrictMath.class,
38+
JdkMath.class);
39+
if (!notFound.isEmpty()) {
40+
final StringBuilder sb = new StringBuilder();
41+
sb.append("JdkMath is missing the following StrictMath methods:");
42+
for (String m : notFound) {
43+
sb.append(LINE_SEP).append(m);
44+
}
45+
Assert.fail(sb.toString());
46+
}
47+
}
48+
49+
/**
50+
* @param class1 Reference implementation.
51+
* @param class2 Alternate implementation.
52+
* @return the methods defined in {@code class1} that are not in {@code class2}.
53+
*/
54+
private List<String> compareClassMethods(Class<?> class1,
55+
Class<?> class2) {
56+
final List<String> notFound = new ArrayList<>();
57+
for (Method method1 : class1.getDeclaredMethods()) {
58+
if (Modifier.isPublic(method1.getModifiers())) {
59+
final Type[] params = method1.getGenericParameterTypes();
60+
try {
61+
class2.getDeclaredMethod(method1.getName(), (Class[]) params);
62+
} catch (NoSuchMethodException e) {
63+
notFound.add(method1.toString());
64+
}
65+
}
66+
}
67+
68+
return notFound;
69+
}
70+
}

commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMathTest.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@
2020
import static org.junit.Assert.assertTrue;
2121
import static org.junit.Assert.fail;
2222

23-
import java.lang.reflect.Method;
24-
import java.lang.reflect.Modifier;
25-
import java.lang.reflect.Type;
2623
import java.math.BigDecimal;
2724
import java.math.BigInteger;
2825
import java.math.RoundingMode;
2926

3027
import org.junit.Assert;
3128
import org.junit.Before;
32-
import org.junit.Ignore;
3329
import org.junit.Test;
3430
import org.junit.jupiter.api.Assertions;
3531
import org.apache.commons.numbers.core.ArithmeticUtils;
@@ -1296,34 +1292,6 @@ public void testFloatScalbSpecialCases() {
12961292
assertEquals(Float.NEGATIVE_INFINITY, AccurateMath.scalb(-3.4028235E38f, 2147483647), 0F);
12971293
}
12981294

1299-
private boolean compareClassMethods(Class<?> class1, Class<?> class2) {
1300-
boolean allfound = true;
1301-
for (Method method1 : class1.getDeclaredMethods()) {
1302-
if (Modifier.isPublic(method1.getModifiers())) {
1303-
Type[] params = method1.getGenericParameterTypes();
1304-
try {
1305-
class2.getDeclaredMethod(method1.getName(), (Class[]) params);
1306-
} catch (NoSuchMethodException e) {
1307-
allfound = false;
1308-
System.out.println(class2.getSimpleName() + " does not implement: " + method1);
1309-
}
1310-
}
1311-
}
1312-
return allfound;
1313-
}
1314-
1315-
@Test
1316-
public void checkMissingAccurateMathClasses() {
1317-
boolean ok = compareClassMethods(StrictMath.class, AccurateMath.class);
1318-
assertTrue("AccurateMath should implement all StrictMath methods", ok);
1319-
}
1320-
1321-
@Ignore
1322-
@Test
1323-
public void checkExtraAccurateMathClasses() {
1324-
compareClassMethods(AccurateMath.class, StrictMath.class);
1325-
}
1326-
13271295
@Test
13281296
public void testSignumDouble() {
13291297
final double delta = 0.0;

0 commit comments

Comments
 (0)