test/java/lang/Double/ToHexString.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4826774 4926547
  27  * @summary Tests for {Float, Double}.toHexString methods



  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.util.regex.*;
  32 import sun.misc.DoubleConsts;
  33 
  34 public class ToHexString {
  35     private ToHexString() {}
  36 
  37     /*
  38      * Given a double value, create a hexadecimal floating-point
  39      * string via an intermediate long hex string.
  40      */
  41     static String doubleToHexString(double d) {
  42         return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));
  43     }
  44 
  45     /*
  46      * Transform the hexadecimal long output into the equivalent
  47      * hexadecimal double value.
  48      */
  49     static String hexLongStringtoHexDoubleString(String transString) {
  50         transString = transString.toLowerCase();
  51 
  52         String zeros = "";


  63                 result.append("-");
  64                 // clear sign bit
  65                 transString =
  66                     Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
  67                     transString.substring(1,16);
  68             }
  69 
  70             // check for NaN and infinity
  71             String signifString = transString.substring(3,16);
  72 
  73             if( transString.substring(0,3).equals("7ff") ) {
  74                 if(signifString.equals("0000000000000")) {
  75                     result.append("Infinity");
  76                 }
  77                 else
  78                     result.append("NaN");
  79             }
  80             else { // finite value
  81                 // Extract exponent
  82                 int exponent = Integer.parseInt(transString.substring(0,3), 16) -
  83                     DoubleConsts.EXP_BIAS;
  84                 result.append("0x");
  85 
  86                 if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
  87                     if(signifString.equals("0000000000000")) {
  88                         result.append("0.0p0");
  89                     }
  90                     else {
  91                         result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
  92                                       "p-1022");
  93                     }
  94                 }
  95                 else {  // normal value
  96                     result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
  97                                   "p" + exponent);
  98                 }
  99             }
 100             return result.toString();
 101     }
 102 
 103     public static int toHexStringTests() {
 104         int failures = 0;
 105         String [][] testCases1 = {
 106             {"Infinity",                "Infinity"},




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4826774 4926547
  27  * @summary Tests for {Float, Double}.toHexString methods
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils
  30  * @run main ToHexString
  31  * @author Joseph D. Darcy
  32  */
  33 
  34 import java.util.regex.*;
  35 import static jdk.testlibrary.DoubleUtils.*;
  36 
  37 public class ToHexString {
  38     private ToHexString() {}
  39 
  40     /*
  41      * Given a double value, create a hexadecimal floating-point
  42      * string via an intermediate long hex string.
  43      */
  44     static String doubleToHexString(double d) {
  45         return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));
  46     }
  47 
  48     /*
  49      * Transform the hexadecimal long output into the equivalent
  50      * hexadecimal double value.
  51      */
  52     static String hexLongStringtoHexDoubleString(String transString) {
  53         transString = transString.toLowerCase();
  54 
  55         String zeros = "";


  66                 result.append("-");
  67                 // clear sign bit
  68                 transString =
  69                     Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
  70                     transString.substring(1,16);
  71             }
  72 
  73             // check for NaN and infinity
  74             String signifString = transString.substring(3,16);
  75 
  76             if( transString.substring(0,3).equals("7ff") ) {
  77                 if(signifString.equals("0000000000000")) {
  78                     result.append("Infinity");
  79                 }
  80                 else
  81                     result.append("NaN");
  82             }
  83             else { // finite value
  84                 // Extract exponent
  85                 int exponent = Integer.parseInt(transString.substring(0,3), 16) -
  86                     EXP_BIAS;
  87                 result.append("0x");
  88 
  89                 if (exponent == Double.MIN_EXPONENT - 1) { // zero or subnormal
  90                     if(signifString.equals("0000000000000")) {
  91                         result.append("0.0p0");
  92                     }
  93                     else {
  94                         result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
  95                                       "p-1022");
  96                     }
  97                 }
  98                 else {  // normal value
  99                     result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
 100                                   "p" + exponent);
 101                 }
 102             }
 103             return result.toString();
 104     }
 105 
 106     public static int toHexStringTests() {
 107         int failures = 0;
 108         String [][] testCases1 = {
 109             {"Infinity",                "Infinity"},