home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Float.java < prev    next >
Text File  |  1997-05-20  |  14KB  |  396 lines

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