home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / lang / Double.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  5.8 KB  |  200 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, 05/02/96
  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.      * Returns a String representation for the specified double value.
  64.      * @param d    the double to be converted
  65.      */
  66.     public static native String toString(double d);
  67.  
  68.     /**
  69.      * Returns a new Double value initialized to the value represented by the 
  70.      * specified String.
  71.      * @param s        the String to be parsed
  72.      * @exception NumberFormatException If the String cannot be parsed.
  73.      */
  74.     public static native Double valueOf(String s) throws NumberFormatException;
  75.  
  76.  
  77.     /**
  78.      * Returns true if the specified number is the special Not-a-Number (NaN) value.
  79.      * @param v    the value to be tested
  80.      */
  81.     static public boolean isNaN(double v) {
  82.     return (v != v);
  83.     }
  84.  
  85.     /**
  86.      * Returns true if the specified number is infinitely large in magnitude.
  87.      * @param v    the value to be tested
  88.      */
  89.     static public boolean isInfinite(double v) {
  90.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  91.     }
  92.  
  93.     /**
  94.      * The value of the Double.
  95.      */
  96.     private double value;
  97.  
  98.     /**
  99.      * Constructs a Double wrapper for the specified double value.
  100.      * @param value the initial value of the double
  101.      */
  102.     public Double(double value) {
  103.     this.value = value;
  104.     }
  105.  
  106.     /**
  107.      * Constructs a Double object initialized to the value specified by the
  108.      * String parameter. 
  109.      * @param s        the String to be converted to a Double
  110.      * @exception    NumberFormatException If the String does not contain a parsable number.
  111.      */
  112.     public Double(String s) throws NumberFormatException {
  113.     // REMIND: this is inefficient
  114.     this(valueOf(s).doubleValue());
  115.     }
  116.  
  117.     /**
  118.      * Returns true if this Double value is the special Not-a-Number (NaN) value.
  119.      */
  120.     public boolean isNaN() {
  121.     return isNaN(value);
  122.     }
  123.  
  124.     /**
  125.      * Returns true if this Double value is infinitely large in magnitude.
  126.      */
  127.     public boolean isInfinite() {
  128.     return isInfinite(value);
  129.     }
  130.  
  131.     /**
  132.      * Returns a String representation of this Double object.
  133.      */
  134.     public String toString() {
  135.     return String.valueOf(value);
  136.     }
  137.  
  138.     /**
  139.      * Returns the integer value of this Double (by casting to an int).
  140.      */
  141.     public int intValue() {
  142.     return (int)value;
  143.     }
  144.  
  145.     /**
  146.      * Returns the long value of this Double (by casting to a long).
  147.      */
  148.     public long longValue() {
  149.     return (long)value;
  150.     }
  151.  
  152.     /**
  153.      * Returns the float value of this Double.
  154.      */
  155.     public float floatValue() {
  156.     return (float)value;
  157.     }
  158.  
  159.     /**
  160.      * Returns the double value of this Double.
  161.      */
  162.     public double doubleValue() {
  163.     return (double)value;
  164.     }
  165.  
  166.     /**
  167.      * Returns a hashcode for this Double.
  168.      */
  169.     public int hashCode() {
  170.     long bits = doubleToLongBits(value);
  171.     return (int)(bits ^ (bits >> 32));
  172.     }
  173.  
  174.     /**
  175.      * Compares this object against the specified object.
  176.      * To be useful in hashtables this method
  177.      * considers two NaN double values to be equal. This
  178.      * is not according to IEEE specification.
  179.      *
  180.      * @param obj        the object to compare with
  181.      * @return         true if the objects are the same; false otherwise.
  182.      */
  183.     public boolean equals(Object obj) {
  184.     return (obj != null)
  185.            && (obj instanceof Double) 
  186.            && (doubleToLongBits(((Double)obj).value) == 
  187.               doubleToLongBits(value));
  188.     }
  189.  
  190.     /**
  191.      * Returns the bit represention of a double-float value
  192.      */
  193.     public static native long doubleToLongBits(double value);
  194.  
  195.     /**
  196.      * Returns the double-float corresponding to a given bit represention.
  197.      */
  198.     public static native double longBitsToDouble(long bits);
  199. }
  200.