home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Double.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  201 lines

  1. /*
  2.  * @(#)Double.java    1.34 96/05/02  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * The Double class provides an object wrapper for Double data values and serves 
  24.  * as a place for double-oriented operations.  A wrapper is useful because most of
  25.  * Java's utility classes require the use of objects.  Since doubles are not 
  26.  * objects in Java, they need to be "wrapped" in a Double instance.
  27.  * @version     1.34, 02 May 1996
  28.  * @author    Lee Boynton
  29.  * @author    Arthur van Hoff
  30.  */
  31.  
  32. public final
  33. class Double extends Number {
  34.     /**
  35.      * Positive infinity.
  36.      */
  37.     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  38.  
  39.     /**
  40.      * Negative infinity.
  41.      */
  42.     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  43.  
  44.     /** 
  45.      * Not-a-Number. Is not equal to anything, including
  46.      * itself.
  47.      */
  48.     public static final double NaN = 0.0d / 0.0;
  49.  
  50.     /**
  51.      * The maximum value a double can have.  The greatest maximum value that a 
  52.      * double can have is 1.79769313486231570e+308d.
  53.      */
  54.     public static final double MAX_VALUE = 1.79769313486231570e+308;
  55.  
  56.     /**
  57.      * The minimum value a double can have.  The lowest minimum value that a
  58.      * double can have is 2.2250738585072014E-308.
  59.      */
  60.     public static final double MIN_VALUE = 2.2250738585072014E-308;
  61.  
  62.  
  63.     /**
  64.      * Returns a String representation for the specified double value.
  65.      * @param d    the double to be converted
  66.      */
  67.     public static native String toString(double d);
  68.  
  69.     /**
  70.      * Returns a new Double value initialized to the value represented by the 
  71.      * specified String.
  72.      * @param s        the String to be parsed
  73.      * @exception NumberFormatException If the String cannot be parsed.
  74.      */
  75.     public static native Double valueOf(String s) throws NumberFormatException;
  76.  
  77.  
  78.     /**
  79.      * Returns true if the specified number is the special Not-a-Number (NaN) value.
  80.      * @param v    the value to be tested
  81.      */
  82.     static public boolean isNaN(double v) {
  83.     return (v != v);
  84.     }
  85.  
  86.     /**
  87.      * Returns true if the specified number is infinitely large in magnitude.
  88.      * @param v    the value to be tested
  89.      */
  90.     static public boolean isInfinite(double v) {
  91.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  92.     }
  93.  
  94.     /**
  95.      * The value of the Double.
  96.      */
  97.     private double value;
  98.  
  99.     /**
  100.      * Constructs a Double wrapper for the specified double value.
  101.      * @param value the initial value of the double
  102.      */
  103.     public Double(double value) {
  104.     this.value = value;
  105.     }
  106.  
  107.     /**
  108.      * Constructs a Double object initialized to the value specified by the
  109.      * String parameter. 
  110.      * @param s        the String to be converted to a Double
  111.      * @exception    NumberFormatException If the String does not contain a parsable number.
  112.      */
  113.     public Double(String s) throws NumberFormatException {
  114.     // REMIND: this is inefficient
  115.     this(valueOf(s).doubleValue());
  116.     }
  117.  
  118.     /**
  119.      * Returns true if this Double value is the special Not-a-Number (NaN) value.
  120.      */
  121.     public boolean isNaN() {
  122.     return isNaN(value);
  123.     }
  124.  
  125.     /**
  126.      * Returns true if this Double value is infinitely large in magnitude.
  127.      */
  128.     public boolean isInfinite() {
  129.     return isInfinite(value);
  130.     }
  131.  
  132.     /**
  133.      * Returns a String representation of this Double object.
  134.      */
  135.     public String toString() {
  136.     return String.valueOf(value);
  137.     }
  138.  
  139.     /**
  140.      * Returns the integer value of this Double (by casting to an int).
  141.      */
  142.     public int intValue() {
  143.     return (int)value;
  144.     }
  145.  
  146.     /**
  147.      * Returns the long value of this Double (by casting to a long).
  148.      */
  149.     public long longValue() {
  150.     return (long)value;
  151.     }
  152.  
  153.     /**
  154.      * Returns the float value of this Double.
  155.      */
  156.     public float floatValue() {
  157.     return (float)value;
  158.     }
  159.  
  160.     /**
  161.      * Returns the double value of this Double.
  162.      */
  163.     public double doubleValue() {
  164.     return (double)value;
  165.     }
  166.  
  167.     /**
  168.      * Returns a hashcode for this Double.
  169.      */
  170.     public int hashCode() {
  171.     long bits = doubleToLongBits(value);
  172.     return (int)(bits ^ (bits >> 32));
  173.     }
  174.  
  175.     /**
  176.      * Compares this object against the specified object.
  177.      * To be useful in hashtables this method
  178.      * considers two NaN double values to be equal. This
  179.      * is not according to IEEE specification.
  180.      *
  181.      * @param obj        the object to compare with
  182.      * @return         true if the objects are the same; false otherwise.
  183.      */
  184.     public boolean equals(Object obj) {
  185.     return (obj != null)
  186.            && (obj instanceof Double) 
  187.            && (doubleToLongBits(((Double)obj).value) == 
  188.               doubleToLongBits(value));
  189.     }
  190.  
  191.     /**
  192.      * Returns the bit represention of a double-float value
  193.      */
  194.     public static native long doubleToLongBits(double value);
  195.  
  196.     /**
  197.      * Returns the double-float corresponding to a given bit represention.
  198.      */
  199.     public static native double longBitsToDouble(long bits);
  200. }
  201.