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 = "";
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 ../Math
29 * @build DoubleConsts
30 * @run main ToHexString
31 * @author Joseph D. Darcy
32 */
33
34 import java.util.regex.*;
35
36 public class ToHexString {
37 private ToHexString() {}
38
39 /*
40 * Given a double value, create a hexadecimal floating-point
41 * string via an intermediate long hex string.
42 */
43 static String doubleToHexString(double d) {
44 return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));
45 }
46
47 /*
48 * Transform the hexadecimal long output into the equivalent
49 * hexadecimal double value.
50 */
51 static String hexLongStringtoHexDoubleString(String transString) {
52 transString = transString.toLowerCase();
53
54 String zeros = "";
68 Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
69 transString.substring(1,16);
70 }
71
72 // check for NaN and infinity
73 String signifString = transString.substring(3,16);
74
75 if( transString.substring(0,3).equals("7ff") ) {
76 if(signifString.equals("0000000000000")) {
77 result.append("Infinity");
78 }
79 else
80 result.append("NaN");
81 }
82 else { // finite value
83 // Extract exponent
84 int exponent = Integer.parseInt(transString.substring(0,3), 16) -
85 DoubleConsts.EXP_BIAS;
86 result.append("0x");
87
88 if (exponent == Double.MIN_EXPONENT - 1) { // zero or subnormal
89 if(signifString.equals("0000000000000")) {
90 result.append("0.0p0");
91 }
92 else {
93 result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
94 "p-1022");
95 }
96 }
97 else { // normal value
98 result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
99 "p" + exponent);
100 }
101 }
102 return result.toString();
103 }
104
105 public static int toHexStringTests() {
106 int failures = 0;
107 String [][] testCases1 = {
108 {"Infinity", "Infinity"},
|