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

  1. /*
  2.  * @(#)Float.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 Float class provides an object wrapper for Float data values, and serves as
  24.  * a place for float-oriented operations.  A wrapper is useful because most of Java's
  25.  * utility classes require the use of objects.  Since floats are not objects in 
  26.  * Java, they need to be "wrapped" in a Float instance.
  27.  * @version     1.29, 10/02/95
  28.  * @author    Lee Boynton
  29.  * @author    Arthur van Hoff
  30.  */
  31. public final
  32. class Float extends Number {
  33.     /**
  34.      * Positive infinity.
  35.      */
  36.     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  37.  
  38.     /**
  39.      * Negative infinity.
  40.      */
  41.     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
  42.  
  43.     /** 
  44.      * Not-a-Number. <em>Note: is not equal to anything, including
  45.      * itself</em>
  46.      */
  47.     public static final float NaN = 0.0f / 0.0f;
  48.  
  49.  
  50.     /**
  51.      * The maximum value a float can have.  The largest maximum value possible is  
  52.      * 3.40282346638528860e+38.
  53.      */
  54.     public static final float MAX_VALUE = 3.40282346638528860e+38f;
  55.  
  56.     /**
  57.      * The minimum value a float can have.  The lowest minimum value possible is 
  58.      * 1.40129846432481707e-45.
  59.      */
  60.     public static final float MIN_VALUE = 1.40129846432481707e-45f;
  61.  
  62.     /**
  63.      * Returns a String representation for the specified float value.
  64.      * @param f    the float to be converted
  65.      */
  66.     public static native String toString(float f);
  67.  
  68.     /**
  69.      * Returns the floating point value represented by the specified String.
  70.      * @param s        the String to be parsed
  71.      * @exception    NumberFormatException If the String does not contain a parsable 
  72.      * Float.
  73.      */
  74.     public static native Float valueOf(String s) throws NumberFormatException;
  75.  
  76.     /**
  77.      * Returns true if the specified number is the special Not-a-Number (NaN) value.
  78.      * @param v    the value to be tested
  79.      */
  80.     static public boolean isNaN(float v) {
  81.     return (v != v);
  82.     }
  83.  
  84.     /**
  85.      * Returns true if the specified number is infinitely large in magnitude.
  86.      * @param v    the value to be tested
  87.      */
  88.     static public boolean isInfinite(float v) {
  89.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  90.     }
  91.  
  92.     /**
  93.      * The value of the Float.
  94.      */
  95.     private float value;
  96.  
  97.     /**
  98.      * Constructs a Float wrapper for the specified float value.
  99.      * @param value the value of the Float
  100.      */
  101.     public Float(float value) {
  102.     this.value = value;
  103.     }
  104.  
  105.     /**
  106.      * Constructs a Float wrapper for the specified double value.
  107.      * @param value the value of the Float
  108.      */
  109.     public Float(double value) {
  110.     this.value = (float)value;
  111.     }
  112.  
  113.     /**
  114.      * Returns true if this Float value is Not-a-Number (NaN).
  115.      */
  116.     public boolean isNaN() {
  117.     return isNaN(value);
  118.     }
  119.  
  120.     /**
  121.      * Returns true if this Float value is infinitely large in magnitude.
  122.      */
  123.     public boolean isInfinite() {
  124.     return isInfinite(value);
  125.     }
  126.  
  127.     /**
  128.      * Returns a String representation of this Float object.
  129.      */
  130.     public String toString() {
  131.     return String.valueOf(value);
  132.     }
  133.  
  134.     /**
  135.      * Returns the integer value of this Float (by casting to an int).
  136.      */
  137.     public int intValue() {
  138.     return (int)value;
  139.     }
  140.  
  141.     /**
  142.      * Returns the long value of this Float (by casting to a long).
  143.      */
  144.     public long longValue() {
  145.     return (long)value;
  146.     }
  147.  
  148.     /**
  149.      * Returns the float value of this Float object.
  150.      */
  151.     public float floatValue() {
  152.     return value;
  153.     }
  154.  
  155.     /**
  156.      * Returns the double value of this Float.
  157.      */
  158.     public double doubleValue() {
  159.     return (double)value;
  160.     }
  161.  
  162.     /**
  163.      * Returns a hashcode for this Float.
  164.      */
  165.     public int hashCode() {
  166.     return (int)value;
  167.     }
  168.  
  169.     /**
  170.      * Compares this object against some other object.
  171.      * <p>
  172.      * <em>Note: To be useful in hashtables this method
  173.      * considers two Nan floating point values to be equal. This
  174.      * is not according to IEEE specification</em>
  175.      *
  176.      * @param obj        the object to compare with
  177.      * @return         true if the objects are the same; false otherwise.
  178.      */
  179.     public boolean equals(Object obj) {
  180.     return (obj != null)
  181.            && (obj instanceof Float) 
  182.            && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
  183.     }
  184.  
  185.     /**
  186.      * Returns the bit represention of a single-float value
  187.      */
  188.     public static native int floatToIntBits(float value);
  189.  
  190.     /**
  191.      * Returns the single-float corresponding to a given bit represention.
  192.      */
  193.     public static native float intBitsToFloat(int bits);
  194.  
  195. }
  196.