1 7 8 package java.lang; 9 10 import sun.misc.FloatingDecimal; 11 import sun.misc.FpUtils; 12 import sun.misc.DoubleConsts; 13 14 32 public final class Double extends Number implements Comparable <Double > { 33 38 public static final double POSITIVE_INFINITY = 1.0 / 0.0; 39 40 45 public static final double NEGATIVE_INFINITY = -1.0 / 0.0; 46 47 52 public static final double NaN = 0.0d / 0.0; 53 54 62 public static final double MAX_VALUE = 1.7976931348623157e+308; 64 71 public static final double MIN_VALUE = 4.9e-324; 73 78 public static final int SIZE = 64; 79 80 86 public static final Class <Double > TYPE = (Class <Double >) Class.getPrimitiveClass("double"); 87 88 150 public static String toString(double d) { 151 return new FloatingDecimal(d).toJavaFormatString(); 152 } 153 154 228 public static String toHexString(double d) { 229 234 if (!FpUtils.isFinite(d) ) 235 return Double.toString(d); 237 else { 238 StringBuffer answer = new StringBuffer (24); 240 241 if (FpUtils.rawCopySign(1.0, d) == -1.0) answer.append("-"); 244 answer.append("0x"); 245 246 d = Math.abs(d); 247 248 if(d == 0.0) { 249 answer.append("0.0p0"); 250 } 251 else { 252 boolean subnormal = (d < DoubleConsts.MIN_NORMAL); 253 254 long signifBits = (Double.doubleToLongBits(d) 258 & DoubleConsts.SIGNIF_BIT_MASK) | 259 0x1000000000000000L; 260 261 answer.append(subnormal ? "0." : "1."); 264 265 String signif = Long.toHexString(signifBits).substring(3,16); 270 answer.append(signif.equals("0000000000000") ? "0": 272 signif.replaceFirst("0{1,12}$", "")); 273 274 answer.append("p" + (subnormal ? 279 DoubleConsts.MIN_EXPONENT: 280 FpUtils.getExponent(d) )); 281 } 282 return answer.toString(); 283 } 284 } 285 286 446 public static Double valueOf(String s) throws NumberFormatException { 447 return new Double (FloatingDecimal.readJavaFormatString(s).doubleValue()); 448 } 449 450 463 public static Double valueOf(double d) { 464 return new Double (d); 465 } 466 467 481 public static double parseDouble(String s) throws NumberFormatException { 482 return FloatingDecimal.readJavaFormatString(s).doubleValue(); 483 } 484 485 493 static public boolean isNaN(double v) { 494 return (v != v); 495 } 496 497 505 static public boolean isInfinite(double v) { 506 return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); 507 } 508 509 514 private final double value; 515 516 522 public Double(double value) { 523 this.value = value; 524 } 525 526 537 public Double(String s) throws NumberFormatException { 538 this(valueOf(s).doubleValue()); 540 } 541 542 549 public boolean isNaN() { 550 return isNaN(value); 551 } 552 553 561 public boolean isInfinite() { 562 return isInfinite(value); 563 } 564 565 574 public String toString() { 575 return String.valueOf(value); 576 } 577 578 586 public byte byteValue() { 587 return (byte)value; 588 } 589 590 598 public short shortValue() { 599 return (short)value; 600 } 601 602 609 public int intValue() { 610 return (int)value; 611 } 612 613 620 public long longValue() { 621 return (long)value; 622 } 623 624 632 public float floatValue() { 633 return (float)value; 634 } 635 636 642 public double doubleValue() { 643 return (double)value; 644 } 645 646 664 public int hashCode() { 665 long bits = doubleToLongBits(value); 666 return (int)(bits ^ (bits >>> 32)); 667 } 668 669 707 public boolean equals(Object obj) { 708 return (obj instanceof Double ) 709 && (doubleToLongBits(((Double )obj).value) == 710 doubleToLongBits(value)); 711 } 712 713 745 public static native long doubleToLongBits(double value); 746 747 782 public static native long doubleToRawLongBits(double value); 783 784 843 public static native double longBitsToDouble(long bits); 844 845 875 public int compareTo(Double anotherDouble) { 876 return Double.compare(value, anotherDouble.value); 877 } 878 879 897 public static int compare(double d1, double d2) { 898 if (d1 < d2) 899 return -1; if (d1 > d2) 901 return 1; 903 long thisBits = Double.doubleToLongBits(d1); 904 long anotherBits = Double.doubleToLongBits(d2); 905 906 return (thisBits == anotherBits ? 0 : (thisBits < anotherBits ? -1 : 1)); } 910 911 912 private static final long serialVersionUID = -9172774392245257468L; 913 } 914 | Popular Tags |