home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / lang / double.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  5.0 KB  |  182 lines

  1. /*
  2.  * @(#)Double.java    1.29 95/10/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.29, 10/02/95
  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. <em>Note: is not equal to anything, including
  46.      * itself</em>
  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 4.94065645841246544e-324d.
  59.      */
  60.     public static final double MIN_VALUE = 4.94065645841246544e-324;
  61.  
  62.     /**
  63.      * Returns true if the specified number is the special Not-a-Number (NaN) value.
  64.      * @param v    the value to be tested
  65.      */
  66.     static public boolean isNaN(double v) {
  67.     return (v != v);
  68.     }
  69.  
  70.     /**
  71.      * Returns true if the specified number is infinitely large in magnitude.
  72.      * @param v    the value to be tested
  73.      */
  74.     static public boolean isInfinite(double v) {
  75.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  76.     }
  77.  
  78.     /**
  79.      * The value of the Double.
  80.      */
  81.     private double value;
  82.  
  83.     /**
  84.      * Constructs a Double wrapper for the specified double value.
  85.      * @param value the initial value of the double
  86.      */
  87.     public Double(double value) {
  88.     this.value = value;
  89.     }
  90.  
  91.     /**
  92.      * Returns true if this Double value is the special Not-a-Number (NaN) value.
  93.      */
  94.     public boolean isNaN() {
  95.     return isNaN(value);
  96.     }
  97.  
  98.     /**
  99.      * Returns true if this Double value is infinitely large in magnitude.
  100.      */
  101.     public boolean isInfinite() {
  102.     return isInfinite(value);
  103.     }
  104.  
  105.     /**
  106.      * Returns a String representation of this Double object.
  107.      */
  108.     public String toString() {
  109.     return String.valueOf(value);
  110.     }
  111.  
  112.     /**
  113.      * Returns the integer value of this Double (by casting to an int).
  114.      */
  115.     public int intValue() {
  116.     return (int)value;
  117.     }
  118.  
  119.     /**
  120.      * Returns the long value of this Double (by casting to a long).
  121.      */
  122.     public long longValue() {
  123.     return (long)value;
  124.     }
  125.  
  126.     /**
  127.      * Returns the float value of this Double.
  128.      */
  129.     public float floatValue() {
  130.     return (float)value;
  131.     }
  132.  
  133.     /**
  134.      * Returns the double value of this Double.
  135.      */
  136.     public double doubleValue() {
  137.     return (double)value;
  138.     }
  139.  
  140.     /**
  141.      * Returns a hashcode for this Double.
  142.      */
  143.     public int hashCode() {
  144.     return (int)value;
  145.     }
  146.  
  147.     /**
  148.      * Compares this object against the specified object.
  149.      * <p>
  150.      * <em>Note: To be useful in hashtables this method
  151.      * considers two NaN double values to be equal. This
  152.      * is not according to IEEE specification</em>
  153.      *
  154.      * @param obj        the object to compare with
  155.      * @return         true if the objects are the same; false otherwise.
  156.      */
  157.     public boolean equals(Object obj) {
  158.     return (obj != null)
  159.            && (obj instanceof Double) 
  160.            && (doubleToLongBits(((Double)obj).value) == 
  161.               doubleToLongBits(value));
  162.     }
  163.  
  164.     /**
  165.      * Returns a new Double value initialized to the value represented by the 
  166.      * specified String.
  167.      * @param s        the String to be parsed
  168.      * @exception NumberFormatException If the String cannot be parsed.
  169.      */
  170.     public static native Double valueOf(String s) throws NumberFormatException;
  171.  
  172.     /**
  173.      * Returns the bit represention of a double-float value
  174.      */
  175.     public static native long doubleToLongBits(double value);
  176.  
  177.     /**
  178.      * Returns the double-float corresponding to a given bit represention.
  179.      */
  180.     public static native double longBitsToDouble(long bits);
  181. }
  182.