sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jso...@apache.org
Subject svn commit: r1737193 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/math/MathFunctions.java test/java/org/apache/sis/math/MathFunctionsTest.java
Date Thu, 31 Mar 2016 08:34:26 GMT
Author: jsorel
Date: Thu Mar 31 08:34:26 2016
New Revision: 1737193

URL: http://svn.apache.org/viewvc?rev=1737193&view=rev
Log:
Add quadruple precision to java double math function

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java?rev=1737193&r1=1737192&r2=1737193&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
[UTF-8] Thu Mar 31 08:34:26 2016
@@ -787,4 +787,45 @@ testNextNumber:         while (true) { /
         }
         return ArraysExt.resize(divisors, count);
     }
+
+    /**
+     * Convert two long bits values containing a quadruple precision floating point number
+     * to a java double precision floating point number.
+     *
+     * @param l0 upper part of the quadruple precision floating point number
+     * @param l1 lower part of the quadruple precision floating point number
+     * @return double precision approximation
+     */
+    public static double quadrupleToDouble(long l0, long l1){
+        //build double
+        long sig = (l0 & 0x8000000000000000L);
+        long exp = (l0 & 0x7FFF000000000000L) >> 48;
+        l0 = (l0 & 0x0000FFFFFFFFFFFFL);
+        if(exp==0){
+            //subnormal number
+            //note : since we convert them to double precision, subnormal number
+            //can not be represented, we map them to zero preserving the sign.
+            return Double.longBitsToDouble(sig);
+        }
+        if(exp==0x7FFF){
+            //NaN number
+            //note : mantissa with all bits at 0 is used for infinite, this is the only
+            //special NaN we can preserve.
+            if(l0==0 && l1==0){
+                return Double.longBitsToDouble(sig|0x7FF0000000000000L);
+            }
+            //Other NaN values might have a meaning, when truncating the value
+            //we might change this meaning, which could cause several issues later
+            //Therefor we map all other NaNs to the default NaN
+            return Double.NaN;
+        }
+        exp = exp - 16383 + 1023; //change from 15 bias to 11 bias
+        //check cases where mantissa excess what double can support
+        if(exp<0)    return Double.NEGATIVE_INFINITY;
+        if(exp>2046) return Double.POSITIVE_INFINITY;
+
+        return Double.longBitsToDouble(
+                sig | (exp << 52) | (l0 << 4) | (l1 >>> 60));
+    }
+
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java?rev=1737193&r1=1737192&r2=1737193&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
[UTF-8] Thu Mar 31 08:34:26 2016
@@ -340,4 +340,68 @@ public final strictfp class MathFunction
             1, 5
         }, commonDivisors(2000, 15));
     }
+
+    /**
+     * Tests the {@link MathFunctions#quadrupleToDouble(int[])} method.
+     */
+    @Test
+    public void testQuadrupleToDouble(){
+
+        long l0,l1;
+
+        // 1.0
+        l0 = 0x3FFF000000000000L;
+        l1 = 0x0000000000000000L;
+        assertEquals(doubleToLongBits(1.0),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        // -2.0
+        l0 = 0xC000000000000000L;
+        l1 = 0x0000000000000000L;
+        assertEquals(doubleToLongBits(-2.0),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        // 3.1415926535897932384626433832795028
+        l0 = 0x4000921FB54442D1L;
+        l1 = 0x8469898CC51701B8L;
+        assertEquals(doubleToLongBits(3.1415926535897932384626433832795028),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        // ~1/3
+        l0 = 0x3FFD555555555555L;
+        l1 = 0x5555555555555555L;
+        assertEquals(doubleToLongBits(1.0/3.0),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        //positive zero
+        l0 = 0x0000000000000000L;
+        l1 = 0x0000000000000000L;
+        assertEquals(doubleToLongBits(+0.0),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        //negative zero
+        l0 = 0x8000000000000000L;
+        l1 = 0x0000000000000000L;
+        assertEquals(doubleToLongBits(-0.0),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        //positive infinite
+        l0 = 0x7FFF000000000000L;
+        l1 = 0x0000000000000000L;
+        assertEquals(doubleToLongBits(Double.POSITIVE_INFINITY),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        //negative infinite
+        l0 = 0xFFFF000000000000L;
+        l1 = 0x0000000000000000L;
+        assertEquals(doubleToLongBits(Double.NEGATIVE_INFINITY),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+        //a random NaN
+        l0 = 0x7FFF000100040000L;
+        l1 = 0x0001005000080000L;
+        assertEquals(doubleToLongBits(Double.NaN),
+                     doubleToLongBits(quadrupleToDouble(l0, l1)));
+
+    }
 }



Mime
View raw message