home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / Float.java < prev    next >
Text File  |  1998-09-22  |  13KB  |  388 lines

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