From f5bec5767b6bd162c3735f08b66e162ff6c9ec54 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sat, 11 Sep 2010 16:23:18 +0000 Subject: [PATCH] added tests for asin and acos JIRA: MATH-375 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@996161 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math/util/FastMathTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/test/java/org/apache/commons/math/util/FastMathTest.java b/src/test/java/org/apache/commons/math/util/FastMathTest.java index 877004f89..c2d8c54a2 100644 --- a/src/test/java/org/apache/commons/math/util/FastMathTest.java +++ b/src/test/java/org/apache/commons/math/util/FastMathTest.java @@ -741,6 +741,69 @@ public class FastMathTest { Assert.assertTrue("expm1() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } + @Test + public void testAsinAccuracy() { + double maxerrulp = 0.0; + + for (int i=0; i<10000; i++) { + double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); + + double tst = FastMath.asin(x); + double ref = DfpMath.asin(field.newDfp(x)).toDouble(); + double err = (tst - ref) / ref; + + if (err != 0) { + double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); + double errulp = field.newDfp(tst).subtract(DfpMath.asin(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); + //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); + + maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); + } + } + + Assert.assertTrue("asin() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); + } + + @Test + public void testAcosAccuracy() { + double maxerrulp = 0.0; + + for (int i=0; i<10000; i++) { + double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); + + double tst = FastMath.acos(x); + double ref = DfpMath.acos(field.newDfp(x)).toDouble(); + double err = (tst - ref) / ref; + + if (err != 0) { + double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); + double errulp = field.newDfp(tst).subtract(DfpMath.acos(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); + //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); + + maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); + } + } + + Assert.assertTrue("acos() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); + } + + private Dfp cbrt(Dfp x) { + boolean negative=false; + + if (x.lessThan(field.getZero())) { + negative = true; + x = x.negate(); + } + + Dfp y = DfpMath.pow(x, field.getOne().divide(3)); + + if (negative) { + y = y.negate(); + } + + return y; + } + @Test public void testToDegrees() { double maxerrulp = 0.0; @@ -847,6 +910,20 @@ public class FastMathTest { time = System.currentTimeMillis() - time; System.out.println("FastMath.sin " + time + "\t" + x); + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.asin(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.asin " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.asin(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.asin " + time + "\t" + x); + x = 0; time = System.currentTimeMillis(); for (int i = 0; i < numberOfRuns; i++) @@ -861,6 +938,19 @@ public class FastMathTest { time = System.currentTimeMillis() - time; System.out.println("FastMath.cos " + time + "\t" + x); + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += StrictMath.acos(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.print("StrictMath.acos " + time + "\t" + x + "\t"); + + x = 0; + time = System.currentTimeMillis(); + for (int i = 0; i < numberOfRuns; i++) + x += FastMath.acos(i / 10000000.0); + time = System.currentTimeMillis() - time; + System.out.println("FastMath.acos " + time + "\t" + x); + x = 0; time = System.currentTimeMillis(); for (int i = 0; i < numberOfRuns; i++)