db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1556810 - in /db/derby/code/trunk/java: engine/org/apache/derby/catalog/SystemProcedures.java engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java testing/org/apache/derbyTesting/functionTests/tests/lang/MathTrigFunctionsTest.java
Date Thu, 09 Jan 2014 14:18:53 GMT
Author: kahatlen
Date: Thu Jan  9 14:18:53 2014
New Revision: 1556810

URL: http://svn.apache.org/r1556810
Log:
DERBY-6447: Use StrictMath for more functions in SYSFUN

Make LOG10, COSH, SINH and TANH use the corresponding methods in
java.lang.StrictMath instead of custom implementations.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MathTrigFunctionsTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java?rev=1556810&r1=1556809&r2=1556810&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java Thu Jan
 9 14:18:53 2014
@@ -62,8 +62,6 @@ import org.apache.derby.impl.sql.catalog
 import org.apache.derby.impl.sql.execute.JarUtil;
 import org.apache.derby.jdbc.InternalDriver;
 import org.apache.derby.iapi.store.access.TransactionController;
-import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
-import org.apache.derby.iapi.sql.dictionary.SystemColumn;
 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
 import org.apache.derby.iapi.sql.dictionary.PasswordHasher;
@@ -1803,24 +1801,6 @@ public class SystemProcedures  {
 	}
 	
 	/**
-	 * Constant for natural log(10).
-	 */
-	private static final double LOG10 = StrictMath.log(10.0d);
-	
-	/**
-	 * Base 10 log function. SYSFUN.LOG10
-	 * Calculated by
-	 * <code>
-	 * log(value) / log(10)
-	 * </code>
-	 * where log is the natural log.
-	 */
-	public static double LOG10(double value)
-	{
-		return StrictMath.log(value) / LOG10;
-	}
-
-	/**
 	 * Cotangent function. SYSFUN.COT
 	 * @see <a href="http://mathworld.wolfram.com/HyperbolicFunctions.html">HyperbolicFunctions</a>
 	 * @return 1 / tan(x)
@@ -1831,37 +1811,6 @@ public class SystemProcedures  {
 	}
 
 	/**
-	 * Hyperbolic Cosine function. SYSFUN.COSH
-	 * @see <a href="http://mathworld.wolfram.com/HyperbolicFunctions.html">HyperbolicFunctions</a>
-	 * @return 1/2 (e^x + e^-x)
-	 */
-	public static double COSH(double value)
-	{
-		return (StrictMath.exp(value) + StrictMath.exp(-value)) / 2.0;
-	}
-
-	/**
-	 * Hyperbolic Sine function. SYSFUN.SINH
-	 * @see <a href="http://mathworld.wolfram.com/HyperbolicFunctions.html">HyperbolicFunctions</a>
-	 * @return 1/2 (e^x - e^-x)
-	 */
-	public static double SINH(double value)
-	{
-		return (StrictMath.exp(value) - StrictMath.exp(-value)) / 2.0;
-	}
-
-	/**
-	 * Hyperbolic Tangent function. SYSFUN.TANH
-	 * @see <a href="http://mathworld.wolfram.com/HyperbolicFunctions.html">HyperbolicFunctions</a>
-	 * @return (e^x - e^-x) / (e^x + e^-x)
-	 */
-	public static double TANH(double value)
-	{
-		return (StrictMath.exp(value) - StrictMath.exp(-value)) /
-			(StrictMath.exp(value) + StrictMath.exp(-value));
-	}
-
-	/**
 	 * Method to return the sign of the given value.
 	 * SYSFUN.SIGN().
 	 * @return 0, 1 or -1

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java?rev=1556810&r1=1556809&r2=1556810&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
Thu Jan  9 14:18:53 2014
@@ -232,7 +232,7 @@ public final class	DataDictionaryImpl
         {"RADIANS", "DOUBLE", "java.lang.StrictMath", "toRadians(double)",  "true", "false",
"DOUBLE" },
         {"LN", "DOUBLE", "java.lang.StrictMath", "log(double)",  "true", "false", "DOUBLE"
},
         {"LOG", "DOUBLE", "java.lang.StrictMath", "log(double)",  "true", "false", "DOUBLE"
}, // Same as LN
-        {"LOG10", "DOUBLE", "org.apache.derby.catalog.SystemProcedures", "LOG10(double)",
 "true", "false", "DOUBLE" },
+        {"LOG10", "DOUBLE", "java.lang.StrictMath", "log10(double)",  "true", "false", "DOUBLE"
},
         {"EXP", "DOUBLE", "java.lang.StrictMath", "exp(double)",  "true", "false", "DOUBLE"
},
         {"CEIL", "DOUBLE", "java.lang.StrictMath", "ceil(double)",  "true", "false", "DOUBLE"
},
         {"CEILING", "DOUBLE", "java.lang.StrictMath", "ceil(double)",  "true", "false", "DOUBLE"
}, // Same as CEIL
@@ -241,9 +241,9 @@ public final class	DataDictionaryImpl
         {"RANDOM", "DOUBLE", "java.lang.StrictMath", "random()",  "false", "false" },
         {"RAND", "DOUBLE", "org.apache.derby.catalog.SystemProcedures", "RAND(int)",  "false",
"false", "INTEGER" }, // Escape function spec.
         {"COT", "DOUBLE", "org.apache.derby.catalog.SystemProcedures", "COT(double)",  "true",
"false", "DOUBLE" },
-        {"COSH", "DOUBLE", "org.apache.derby.catalog.SystemProcedures", "COSH(double)", 
"true", "false", "DOUBLE" },
-        {"SINH", "DOUBLE", "org.apache.derby.catalog.SystemProcedures", "SINH(double)", 
"true", "false", "DOUBLE" },
-        {"TANH", "DOUBLE", "org.apache.derby.catalog.SystemProcedures", "TANH(double)", 
"true", "false", "DOUBLE" }
+        {"COSH", "DOUBLE", "java.lang.StrictMath", "cosh(double)",  "true", "false", "DOUBLE"
},
+        {"SINH", "DOUBLE", "java.lang.StrictMath", "sinh(double)",  "true", "false", "DOUBLE"
},
+        {"TANH", "DOUBLE", "java.lang.StrictMath", "tanh(double)",  "true", "false", "DOUBLE"
}
 	};
 	
 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MathTrigFunctionsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MathTrigFunctionsTest.java?rev=1556810&r1=1556809&r2=1556810&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MathTrigFunctionsTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MathTrigFunctionsTest.java
Thu Jan  9 14:18:53 2014
@@ -23,16 +23,14 @@ import java.sql.Statement;
 import java.util.Random;
 
 import junit.framework.Test;
-import junit.framework.TestSuite;
 
 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.JDBC;
 import org.apache.derbyTesting.junit.TestConfiguration;
 
 public class MathTrigFunctionsTest extends BaseJDBCTestCase {
 
-	private static final boolean debugFlag = false;
-
    private static final double
             PRE_DERBY_3398_SMALLEST_NEG_DERBY_DOUBLE = -1.79769E+308;
 
@@ -57,7 +55,8 @@ public class MathTrigFunctionsTest exten
 
 	private static final double[] logValues = { 0.000000001, 0.25, 0.5, 1.0,
            45.0, 90.0, 135.0, 180.0, 270, PRE_DERBY_3398_SMALLEST_POS_DERBY_DOUBLE,
-           PRE_DERBY_3398_LARGEST_POS_DERBY_DOUBLE };
+           PRE_DERBY_3398_LARGEST_POS_DERBY_DOUBLE, 10, 100, 1000, 10000,
+           100000, 1000000, 10000000, 100000000, 1000000000};
 
    private static final double[] testValues = {
             PRE_DERBY_3398_SMALLEST_NEG_DERBY_DOUBLE,
@@ -408,6 +407,114 @@ public class MathTrigFunctionsTest exten
         psFN.close();
 	}
 
+    public void testCot() throws SQLException {
+        executeNullValues("COT");
+        executeNullFn("COT");
+        debug();
+
+        PreparedStatement ps = prepareStatement("VALUES COT(?)");
+        PreparedStatement psFN = prepareStatement("VALUES {fn COT(?)}");
+
+        for (double value : testValues) {
+            checkResult(ps, value, 1.0d / StrictMath.tan(value));
+        }
+
+        Random rand = new Random();
+        for (int i = 0; i < 100; i++) {
+            double randD = rand.nextDouble();
+            double expected = 1.0d / StrictMath.tan(randD);
+            checkResult(ps, randD, expected);
+            checkResult(psFN, randD, expected);
+        }
+    }
+
+    private void checkResult(PreparedStatement ps,
+                             double input, double expected)
+            throws SQLException {
+
+        // Derby doesn't distinguish between positive and negative zero.
+        if (expected == -0.0d) {
+            expected = 0.0d;
+        }
+
+        ps.setDouble(1, input);
+        if (Double.isNaN(expected) || Double.isInfinite(expected)) {
+            debug("input value: " + input + " expected value: " + expected
+                    + " : OUT OF RANGE");
+            assertStatementError("22003", ps);
+        } else {
+            debug("input value: " + input + " expected value: " + expected);
+            JDBC.assertSingleValueResultSet(
+                    ps.executeQuery(), String.valueOf(expected));
+        }
+    }
+
+    public void testCosh() throws SQLException {
+        executeNullValues("COSH");
+        debug();
+
+        PreparedStatement ps = prepareStatement("VALUES COSH(?)");
+
+        // COSH is not a JDBC escape function. Expect syntax error when
+        // called as one.
+        assertCompileError("42X01", "VALUES {fn COSH(?)}");
+
+        for (double value : testValues) {
+            checkResult(ps, value, StrictMath.cosh(value));
+        }
+
+        Random rand = new Random();
+        for (int i = 0; i < 100; i++) {
+            double randD = rand.nextDouble();
+            double expect = StrictMath.cosh(randD);
+            checkResult(ps, randD, expect);
+        }
+    }
+
+    public void testSinh() throws SQLException {
+        executeNullValues("SINH");
+        debug();
+
+        PreparedStatement ps = prepareStatement("VALUES SINH(?)");
+
+        // SINH is not a JDBC escape function. Expect syntax error when
+        // called as one.
+        assertCompileError("42X01", "VALUES {fn SINH(?)}");
+
+        for (double value : testValues) {
+            checkResult(ps, value, StrictMath.sinh(value));
+        }
+
+        Random rand = new Random();
+        for (int i = 0; i < 100; i++) {
+            double randD = rand.nextDouble();
+            double expect = StrictMath.sinh(randD);
+            checkResult(ps, randD, expect);
+        }
+    }
+
+    public void testTanh() throws SQLException {
+        executeNullValues("TANH");
+        debug();
+
+        PreparedStatement ps = prepareStatement("VALUES TANH(?)");
+
+        // TANH is not a JDBC escape function. Expect syntax error when
+        // called as one.
+        assertCompileError("42X01", "VALUES {fn TANH(?)}");
+
+        for (double value : testValues) {
+            checkResult(ps, value, StrictMath.tanh(value));
+        }
+
+        Random rand = new Random();
+        for (int i = 0; i < 100; i++) {
+            double randD = rand.nextDouble();
+            double expect = StrictMath.tanh(randD);
+            checkResult(ps, randD, expect);
+        }
+    }
+
 	/**
 	 * Tests the PI function which returns a value that is closer than any other
 	 * value to pi.
@@ -681,9 +788,7 @@ public class MathTrigFunctionsTest exten
         PreparedStatement psFN =
             prepareStatement("VALUES {fn LOG10(?)}");
 		for (int i = 0; i < logValues.length; i++) {
-			// ln 10 = y * (log base 10 (10))
-			// 2.3025850929940456840179914546844 = y * 1
-			double expected = java.lang.StrictMath.log(logValues[i]) / 2.3025850929940456840179914546844;
+            double expected = StrictMath.log10(logValues[i]);
 			double rValue = getValue(ps, logValues[i]);
 			debug("LOG10: input value: " + logValues[i] + " expected value: "
 					+ expected + " return value: " + rValue);
@@ -695,7 +800,7 @@ public class MathTrigFunctionsTest exten
 		Random rand = new java.util.Random();
 		for (int i = 0; i < 100; i++) {
 			double randD = rand.nextDouble();
-			double expect = java.lang.StrictMath.log(randD) / 2.3025850929940456840179914546844;
+            double expect = StrictMath.log10(randD);
 			double rVal = getValue(ps, randD);
 			assertEquals(expect, rVal, 0.0);
 			double fVal = getValue(psFN, randD);
@@ -1110,15 +1215,11 @@ public class MathTrigFunctionsTest exten
 	}
 
 	private void debug(String message) {
-		if (debugFlag) {
-			System.out.println(message);
-		}
+        println(message);
 	}
 
 	private void debug() {
-		if (debugFlag) {
-			System.out.println();
-		}
+        println("");
 	}
 
 	public MathTrigFunctionsTest(String name) {



Mime
View raw message