package net.sourceforge.retroweaver.runtime.java.lang;
public class Double_ {
private Double_() {
// private constructor
}
public static Double valueOf(final double val) {
return new Double(val);
}
public static String toHexString(double d) {
/*
* Reference: http://en.wikipedia.org/wiki/IEEE_754
*/
if (d != d) {
return "NaN"; //$NON-NLS-1$
}
if (d == Double.POSITIVE_INFINITY) {
return "Infinity"; //$NON-NLS-1$
}
if (d == Double.NEGATIVE_INFINITY) {
return "-Infinity"; //$NON-NLS-1$
}
long bitValue = Double.doubleToLongBits(d);
boolean negative = (bitValue & 0x8000000000000000L) != 0;
// mask exponent bits and shift down
long exponent = (bitValue & 0x7FF0000000000000L) >>> 52;
// mask significand bits and shift up
long significand = bitValue & 0x000FFFFFFFFFFFFFL;
if (exponent == 0 && significand == 0) {
return (negative ? "-0x0.0p0" : "0x0.0p0"); //$NON-NLS-1$ //$NON-NLS-2$
}
StringBuffer hexString = new StringBuffer(10);
if (negative) {
hexString.append("-0x"); //$NON-NLS-1$
} else {
hexString.append("0x"); //$NON-NLS-1$
}
if (exponent == 0) { // denormal (subnormal) value
hexString.append("0."); //$NON-NLS-1$
// significand is 52-bits, so there can be 13 hex digits
int fractionDigits = 13;
// remove trailing hex zeros, so Integer.toHexString() won't print
// them
while ((significand != 0) && ((significand & 0xF) == 0)) {
significand >>>= 4;
fractionDigits--;
}
// this assumes Integer.toHexString() returns lowercase characters
String hexSignificand = Long.toHexString(significand);
// if there are digits left, then insert some '0' chars first
if (significand != 0 && fractionDigits > hexSignificand.length()) {
int digitDiff = fractionDigits - hexSignificand.length();
while (digitDiff-- != 0) {
hexString.append('0');
}
}
hexString.append(hexSignificand);
hexString.append("p-1022"); //$NON-NLS-1$
} else { // normal value
hexString.append("1."); //$NON-NLS-1$
// significand is 52-bits, so there can be 13 hex digits
int fractionDigits = 13;
// remove trailing hex zeros, so Integer.toHexString() won't print
// them
while ((significand != 0) && ((significand & 0xF) == 0)) {
significand >>>= 4;
fractionDigits--;
}
// this assumes Integer.toHexString() returns lowercase characters
String hexSignificand = Long.toHexString(significand);
// if there are digits left, then insert some '0' chars first
if (significand != 0 && fractionDigits > hexSignificand.length()) {
int digitDiff = fractionDigits - hexSignificand.length();
while (digitDiff-- != 0) {
hexString.append('0');
}
}
hexString.append(hexSignificand);
hexString.append('p');
// remove exponent's 'bias' and convert to a string
hexString.append(Long.toString(exponent - 1023));
}
return hexString.toString();
}
}