home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Double.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  14.0 KB  |  407 lines

  1. /*
  2.  * @(#)Double.java    1.46 98/03/18
  3.  *
  4.  * Copyright 1994-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The Double class wraps a value of the primitive type 
  19.  * <code>double</code> in an object. An object of type 
  20.  * <code>Double</code> contains a single field whose type is 
  21.  * <code>double</code>. 
  22.  * <p>
  23.  * In addition, this class provides several methods for converting a 
  24.  * <code>double</code> to a <code>String</code> and a 
  25.  * <code>String</code> to a <code>double</code>, as well as other 
  26.  * constants and methods useful when dealing with a 
  27.  * <code>double</code>. 
  28.  *
  29.  * @author  Lee Boynton
  30.  * @author  Arthur van Hoff
  31.  * @version 1.46, 03/18/98
  32.  * @since   JDK1.0
  33.  */
  34. public final class Double extends Number implements Comparable {
  35.     /**
  36.      * The positive infinity of type <code>double</code>. 
  37.      */
  38.     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  39.  
  40.     /**
  41.      * The negative infinity of type <code>double</code>. 
  42.      */
  43.     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  44.  
  45.     /** 
  46.      * A NaN value of type <code>double</code>. 
  47.      */
  48.     public static final double NaN = 0.0d / 0.0;
  49.  
  50.     /**
  51.      * The largest positive value of type <code>double</code>. 
  52.      */
  53.     public static final double MAX_VALUE = 1.79769313486231570e+308;
  54.  
  55.     /**
  56.      * The smallest positive value of type <code>double</code>. 
  57.      */
  58. //  public static final double MIN_VALUE = 4.94065645841246544e-324;
  59.     public static final double MIN_VALUE = longBitsToDouble(1L);
  60.  
  61.     /**
  62.      * The Class object representing the primitive type double.
  63.      *
  64.      * @since   JDK1.1
  65.      */
  66.     public static final Class    TYPE = Class.getPrimitiveClass("double");
  67.  
  68.     /**
  69.      * Creates a string representation of the <code>double</code> 
  70.      * argument. 
  71.      * <p>
  72.      * The values <code>NaN</code>, <code>NEGATIVE_INFINITY</code>, 
  73.      * <code>POSITIVE_INFINITY</code>, <code>-0.0</code>, and 
  74.      * <code>+0.0</code> are represented by the strings 
  75.      * <code>"NaN"</code>, <code>"-Infinity"</code>, 
  76.      * <code>"Infinity"</code>, <code>"-0.0"</code>, and 
  77.      * <code>"0.0"</code>, respectively. 
  78.      * <p>
  79.      * If <code>d</code> is in the range 
  80.      * <code>10<sup>-3</sup> <= |d| <=10<sup>7</sup></code>,
  81.      * then it is converted to a string in the style 
  82.      * <code>[-]ddd.ddd</code>. Otherwise, it is converted to a 
  83.      * string in the style <code>[-]m.ddddE±xx</code>.
  84.      * <p>
  85.      * There is always a minimum of one digit after the decimal point. 
  86.      * The number of digits is the minimum needed to uniquely distinguish 
  87.      * the argument value from adjacent values of type 
  88.      * <code>double</code>. 
  89.      *
  90.      * @param   d   the double to be converted.
  91.      * @return  a string representation of the argument.
  92.      */
  93.     public static String toString(double d){
  94.     return new FloatingDecimal(d).toJavaFormatString();
  95.     }
  96.  
  97.     /**
  98.      * Returns a new Double value initialized to the value represented by the 
  99.      * specified String.
  100.      *
  101.      * @param      s   the string to be parsed.
  102.      * @return     a newly constructed <code>Double</code> initialized to the
  103.      *             value represented by the string argument.
  104.      * @exception  NumberFormatException  if the string does not contain a
  105.      *               parsable number.
  106.      */
  107.     public static Double valueOf(String s) throws NumberFormatException { 
  108.     return new Double(valueOf0(s));
  109.     }
  110.  
  111.     /**
  112.      * Returns true if the specified number is the special Not-a-Number (NaN)
  113.      * value.
  114.      *
  115.      * @param   v   the value to be tested.
  116.      * @return  <code>true</code> if the value of the argument is NaN;
  117.      *          <code>false</code> otherwise.
  118.      */
  119.     static public boolean isNaN(double v) {
  120.     return (v != v);
  121.     }
  122.  
  123.     /**
  124.      * Returns true if the specified number is infinitely large in magnitude.
  125.      *
  126.      * @param   v   the value to be tested.
  127.      * @return  <code>true</code> if the value of the argument is positive
  128.      *          infinity or negative infinity; <code>false</code> otherwise.
  129.      */
  130.     static public boolean isInfinite(double v) {
  131.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  132.     }
  133.  
  134.     /**
  135.      * The value of the Double.
  136.      */
  137.     private double value;
  138.  
  139.     /**
  140.      * Constructs a newly allocated <code>Double</code> object that 
  141.      * represents the primitive <code>double</code> argument. 
  142.      *
  143.      * @param   value   the value to be represented by the <code>Double</code>.
  144.      */
  145.     public Double(double value) {
  146.     this.value = value;
  147.     }
  148.  
  149.     /**
  150.      * Constructs a newly allocated <code>Double</code> object that 
  151.      * represents the floating- point value of type <code>double</code> 
  152.      * represented by the string. The string is converted to a 
  153.      * <code>double</code> value as if by the <code>valueOf</code> method. 
  154.      *
  155.      * @param      s   a string to be converted to a <code>Double</code>.
  156.      * @exception  NumberFormatException  if the string does not contain a
  157.      *               parsable number.
  158.      * @see        java.lang.Double#valueOf(java.lang.String)
  159.      */
  160.     public Double(String s) throws NumberFormatException {
  161.     // REMIND: this is inefficient
  162.     this(valueOf(s).doubleValue());
  163.     }
  164.  
  165.     /**
  166.      * Returns true if this Double value is the special Not-a-Number (NaN)
  167.      * value.
  168.      *
  169.      * @return  <code>true</code> if the value represented by this object is
  170.      *          NaN; <code>false</code> otherwise.
  171.      */
  172.     public boolean isNaN() {
  173.     return isNaN(value);
  174.     }
  175.  
  176.     /**
  177.      * Returns true if this Double value is infinitely large in magnitude.
  178.      *
  179.      * @return  <code>true</code> if the value represented by this object is
  180.      *          positive infinity or negative infinity;
  181.      *          <code>false</code> otherwise.
  182.      */
  183.     public boolean isInfinite() {
  184.     return isInfinite(value);
  185.     }
  186.  
  187.     /**
  188.      * Returns a String representation of this Double object.
  189.      * The primitive <code>double</code> value represented by this 
  190.      * object is converted to a string exactly as if by the method 
  191.      * <code>toString</code> of one argument. 
  192.      *
  193.      * @return  a <code>String</code> representation of this object.
  194.      * @see     java.lang.Double#toString(double)
  195.      */
  196.     public String toString() {
  197.     return String.valueOf(value);
  198.     }
  199.  
  200.     /**
  201.      * Returns the value of this Double as a byte (by casting to a byte).
  202.      *
  203.      * @since   JDK1.1
  204.      */
  205.     public byte byteValue() {
  206.     return (byte)value;
  207.     }
  208.  
  209.     /**
  210.      * Returns the value of this Double as a short (by casting to a short).
  211.      *
  212.      * @since   JDK1.1
  213.      */
  214.     public short shortValue() {
  215.     return (short)value;
  216.     }
  217.  
  218.     /**
  219.      * Returns the integer value of this Double (by casting to an int).
  220.      *
  221.      * @return  the <code>double</code> value represented by this object is
  222.      *          converted to type <code>int</code> and the result of the
  223.      *          conversion is returned.
  224.      */
  225.     public int intValue() {
  226.     return (int)value;
  227.     }
  228.  
  229.     /**
  230.      * Returns the long value of this Double (by casting to a long).
  231.      *
  232.      * @return  the <code>double</code> value represented by this object is
  233.      *          converted to type <code>long</code> and the result of the
  234.      *          conversion is returned.
  235.      */
  236.     public long longValue() {
  237.     return (long)value;
  238.     }
  239.  
  240.     /**
  241.      * Returns the float value of this Double.
  242.      *
  243.      * @return  the <code>double</code> value represented by this object is
  244.      *          converted to type <code>float</code> and the result of the
  245.      *          conversion is returned.
  246.      * @since   JDK1.0     */
  247.     public float floatValue() {
  248.     return (float)value;
  249.     }
  250.  
  251.     /**
  252.      * Returns the double value of this Double.
  253.      *
  254.      * @return  the <code>double</code> value represented by this object.
  255.      */
  256.     public double doubleValue() {
  257.     return (double)value;
  258.     }
  259.  
  260.     /**
  261.      * Returns a hashcode for this Double.
  262.      *
  263.      * @return  a <code>hash code</code> value for this object. 
  264.      */
  265.     public int hashCode() {
  266.     long bits = doubleToLongBits(value);
  267.     return (int)(bits ^ (bits >> 32));
  268.     }
  269.  
  270.     /**
  271.      * Compares this object against the specified object.
  272.      * The result is <code>true</code> if and only if the argument is 
  273.      * not <code>null</code> and is a <code>Double</code> object that 
  274.      * represents a double that has the identical bit pattern to the bit 
  275.      * pattern of the double represented by this object. 
  276.      * <p>
  277.      * Note that in most cases, for two instances of class 
  278.      * <code>Double</code>, <code>d1</code> and <code>d2</code>, the 
  279.      * value of <code>d1.equals(d2)</code> is <code>true</code> if and 
  280.      * only if 
  281.      * <blockquote><pre>
  282.      *   d1.doubleValue() == d2.doubleValue()
  283.      * </pre></blockquote>
  284.      * <p>
  285.      * also has the value <code>true</code>. However, there are two 
  286.      * exceptions: 
  287.      * <ul>
  288.      * <li>If <code>d1</code> and <code>d2</code> both represent 
  289.      *     <code>Double.NaN</code>, then the <code>equals</code> method 
  290.      *     returns <code>true</code>, even though 
  291.      *     <code>Double.NaN==Double.NaN</code> has the value 
  292.      *     <code>false</code>.
  293.      * <li>If <code>d1</code> represents <code>+0.0</code> while
  294.      *     <code>d2</code> represents <code>-0.0</code>, or vice versa,
  295.      *     the <code>equal</code> test has the value <code>false</code>,
  296.      *     even though <code>+0.0==-0.0</code> has the value <code>true.</code>
  297.      * </ul>
  298.      *
  299.      * @param   obj   the object to compare with.
  300.      * @return  <code>true</code> if the objects are the same;
  301.      *          <code>false</code> otherwise.
  302.      */
  303.     public boolean equals(Object obj) {
  304.     return (obj != null)
  305.            && (obj instanceof Double) 
  306.            && (doubleToLongBits(((Double)obj).value) == 
  307.               doubleToLongBits(value));
  308.     }
  309.  
  310.     /**
  311.      * Returns a representation of the specified floating-point value 
  312.      * according to the IEEE 754 floating-point "double 
  313.      * format" bit layout. 
  314.      * <p>
  315.      * Bit 63 represents the sign of the floating-point number. Bits 
  316.      * 62-52 represent the exponent. Bits 51-0 represent 
  317.      * the significand (sometimes called the mantissa) of the 
  318.      * floating-point number. 
  319.      * <p>
  320.      * If the argument is positive infinity, the result is 
  321.      * <code>0x7ff0000000000000L</code>. 
  322.      * <p>
  323.      * If the argument is negative infinity, the result is 
  324.      * <code>0xfff0000000000000L</code>. 
  325.      * <p>
  326.      * If the argument is NaN, the result is 
  327.      * <code>0x7ff8000000000000L</code>. 
  328.      *
  329.      * @param   value   a double precision floating-point number.
  330.      * @return  the bits that represent the floating-point number.
  331.      */
  332.     public static native long doubleToLongBits(double value);
  333.  
  334.     /**
  335.      * Returns the double-float corresponding to a given bit represention.
  336.      * The argument is considered to be a representation of a 
  337.      * floating-point value according to the IEEE 754 floating-point 
  338.      * "double precision" bit layout. That floating-point 
  339.      * value is returned as the result. 
  340.      * <p>
  341.      * If the argument is <code>0x7f80000000000000L</code>, the result 
  342.      * is positive infinity. 
  343.      * <p>
  344.      * If the argument is <code>0xff80000000000000L</code>, the result 
  345.      * is negative infinity. 
  346.      * <p>
  347.      * If the argument is any value in the range 
  348.      * <code>0x7ff0000000000001L</code> through 
  349.      * <code>0x7fffffffffffffffL</code> or in the range 
  350.      * <code>0xfff0000000000001L</code> through 
  351.      * <code>0xffffffffffffffffL</code>, the result is NaN. All IEEE 754 
  352.      * NaN values are, in effect, lumped together by the Java language 
  353.      * into a single value. 
  354.      *
  355.      * @param   bits   any <code>long</code> integer.
  356.      * @return  the <code>double</code> floating-point value with the same
  357.      *          bit pattern.
  358.      */
  359.     public static native double longBitsToDouble(long bits);
  360.  
  361.     /**
  362.      * Compares two Doubles numerically.
  363.      *
  364.      * @param   anotherDouble   the <code>Double</code> to be compared.
  365.      * @return  the value <code>0</code> if the argument Double is equal to
  366.      *          this Double; a value less than <code>0</code> if this Double
  367.      *          is numerically less than the Double argument; and a
  368.      *          value greater than <code>0</code> if this Double is
  369.      *          numerically greater than the Double argument
  370.      *        (signed comparison).
  371.      * @since   JDK1.2
  372.      */
  373.     public int compareTo(Double anotherDouble) {
  374.     double thisVal = this.value;
  375.     double anotherVal = anotherDouble.value;
  376.     return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  377.     }
  378.  
  379.     /**
  380.      * Compares this Double to another Object.  If the Object is a Double,
  381.      * this function behaves like <code>compareTo(Double)</code>.  Otherwise,
  382.      * it throws a <code>ClassCastException</code> (as Doubles are comparable
  383.      * only to other Doubles).
  384.      *
  385.      * @param   o the <code>Object</code> to be compared.
  386.      * @return  the value <code>0</code> if the argument is a Double
  387.      *        numerically equal to this Double; a value less than
  388.      *        <code>0</code> if the argument is a Double numerically
  389.      *        greater than this Double; and a value greater than
  390.      *        <code>0</code> if the argument is a Double numerically
  391.      *        less than this Double.
  392.      * @exception <code>ClassCastException</code> if the argument is not a
  393.      *          <code>Double</code>. 
  394.      * @see     java.lang.Comparable
  395.      * @since   JDK1.2
  396.      */
  397.     public int compareTo(Object o) {
  398.     return compareTo((Double)o);
  399.     }
  400.  
  401.     /* Converts a string to a double.  Also used by Float.valueOf */
  402.     static native double valueOf0(String s) throws NumberFormatException;
  403.  
  404.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  405.     private static final long serialVersionUID = -9172774392245257468L;
  406. }
  407.