rt/emul/mini/src/main/java/java/lang/Float.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Mar 2013 11:58:50 +0100
changeset 826 fb751bcc23fd
parent 778 6f8683517f1f
child 1332 bad90a969596
permissions -rw-r--r--
doubleToLongBits and floatToIntBits implemented via typed arrays. Requires real browser (Rhino does not support typed arrays) and as such creating new module to hold the tests and execute them in 'brwsr' mode.
     1 /*
     2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    29 
    30 /**
    31  * The {@code Float} class wraps a value of primitive type
    32  * {@code float} in an object. An object of type
    33  * {@code Float} contains a single field whose type is
    34  * {@code float}.
    35  *
    36  * <p>In addition, this class provides several methods for converting a
    37  * {@code float} to a {@code String} and a
    38  * {@code String} to a {@code float}, as well as other
    39  * constants and methods useful when dealing with a
    40  * {@code float}.
    41  *
    42  * @author  Lee Boynton
    43  * @author  Arthur van Hoff
    44  * @author  Joseph D. Darcy
    45  * @since JDK1.0
    46  */
    47 public final class Float extends Number implements Comparable<Float> {
    48     /**
    49      * A constant holding the positive infinity of type
    50      * {@code float}. It is equal to the value returned by
    51      * {@code Float.intBitsToFloat(0x7f800000)}.
    52      */
    53     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
    54 
    55     /**
    56      * A constant holding the negative infinity of type
    57      * {@code float}. It is equal to the value returned by
    58      * {@code Float.intBitsToFloat(0xff800000)}.
    59      */
    60     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
    61 
    62     /**
    63      * A constant holding a Not-a-Number (NaN) value of type
    64      * {@code float}.  It is equivalent to the value returned by
    65      * {@code Float.intBitsToFloat(0x7fc00000)}.
    66      */
    67     public static final float NaN = 0.0f / 0.0f;
    68 
    69     /**
    70      * A constant holding the largest positive finite value of type
    71      * {@code float}, (2-2<sup>-23</sup>)&middot;2<sup>127</sup>.
    72      * It is equal to the hexadecimal floating-point literal
    73      * {@code 0x1.fffffeP+127f} and also equal to
    74      * {@code Float.intBitsToFloat(0x7f7fffff)}.
    75      */
    76     public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
    77 
    78     /**
    79      * A constant holding the smallest positive normal value of type
    80      * {@code float}, 2<sup>-126</sup>.  It is equal to the
    81      * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
    82      * equal to {@code Float.intBitsToFloat(0x00800000)}.
    83      *
    84      * @since 1.6
    85      */
    86     public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
    87 
    88     /**
    89      * A constant holding the smallest positive nonzero value of type
    90      * {@code float}, 2<sup>-149</sup>. It is equal to the
    91      * hexadecimal floating-point literal {@code 0x0.000002P-126f}
    92      * and also equal to {@code Float.intBitsToFloat(0x1)}.
    93      */
    94     public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
    95 
    96     /**
    97      * Maximum exponent a finite {@code float} variable may have.  It
    98      * is equal to the value returned by {@code
    99      * Math.getExponent(Float.MAX_VALUE)}.
   100      *
   101      * @since 1.6
   102      */
   103     public static final int MAX_EXPONENT = 127;
   104 
   105     /**
   106      * Minimum exponent a normalized {@code float} variable may have.
   107      * It is equal to the value returned by {@code
   108      * Math.getExponent(Float.MIN_NORMAL)}.
   109      *
   110      * @since 1.6
   111      */
   112     public static final int MIN_EXPONENT = -126;
   113 
   114     /**
   115      * The number of bits used to represent a {@code float} value.
   116      *
   117      * @since 1.5
   118      */
   119     public static final int SIZE = 32;
   120 
   121     /**
   122      * The {@code Class} instance representing the primitive type
   123      * {@code float}.
   124      *
   125      * @since JDK1.1
   126      */
   127     public static final Class<Float> TYPE = Class.getPrimitiveClass("float");
   128 
   129     /**
   130      * Returns a string representation of the {@code float}
   131      * argument. All characters mentioned below are ASCII characters.
   132      * <ul>
   133      * <li>If the argument is NaN, the result is the string
   134      * "{@code NaN}".
   135      * <li>Otherwise, the result is a string that represents the sign and
   136      *     magnitude (absolute value) of the argument. If the sign is
   137      *     negative, the first character of the result is
   138      *     '{@code -}' (<code>'&#92;u002D'</code>); if the sign is
   139      *     positive, no sign character appears in the result. As for
   140      *     the magnitude <i>m</i>:
   141      * <ul>
   142      * <li>If <i>m</i> is infinity, it is represented by the characters
   143      *     {@code "Infinity"}; thus, positive infinity produces
   144      *     the result {@code "Infinity"} and negative infinity
   145      *     produces the result {@code "-Infinity"}.
   146      * <li>If <i>m</i> is zero, it is represented by the characters
   147      *     {@code "0.0"}; thus, negative zero produces the result
   148      *     {@code "-0.0"} and positive zero produces the result
   149      *     {@code "0.0"}.
   150      * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
   151      *      less than 10<sup>7</sup>, then it is represented as the
   152      *      integer part of <i>m</i>, in decimal form with no leading
   153      *      zeroes, followed by '{@code .}'
   154      *      (<code>'&#92;u002E'</code>), followed by one or more
   155      *      decimal digits representing the fractional part of
   156      *      <i>m</i>.
   157      * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
   158      *      equal to 10<sup>7</sup>, then it is represented in
   159      *      so-called "computerized scientific notation." Let <i>n</i>
   160      *      be the unique integer such that 10<sup><i>n</i> </sup>&le;
   161      *      <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
   162      *      be the mathematically exact quotient of <i>m</i> and
   163      *      10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10.
   164      *      The magnitude is then represented as the integer part of
   165      *      <i>a</i>, as a single decimal digit, followed by
   166      *      '{@code .}' (<code>'&#92;u002E'</code>), followed by
   167      *      decimal digits representing the fractional part of
   168      *      <i>a</i>, followed by the letter '{@code E}'
   169      *      (<code>'&#92;u0045'</code>), followed by a representation
   170      *      of <i>n</i> as a decimal integer, as produced by the
   171      *      method {@link java.lang.Integer#toString(int)}.
   172      *
   173      * </ul>
   174      * </ul>
   175      * How many digits must be printed for the fractional part of
   176      * <i>m</i> or <i>a</i>? There must be at least one digit
   177      * to represent the fractional part, and beyond that as many, but
   178      * only as many, more digits as are needed to uniquely distinguish
   179      * the argument value from adjacent values of type
   180      * {@code float}. That is, suppose that <i>x</i> is the
   181      * exact mathematical value represented by the decimal
   182      * representation produced by this method for a finite nonzero
   183      * argument <i>f</i>. Then <i>f</i> must be the {@code float}
   184      * value nearest to <i>x</i>; or, if two {@code float} values are
   185      * equally close to <i>x</i>, then <i>f</i> must be one of
   186      * them and the least significant bit of the significand of
   187      * <i>f</i> must be {@code 0}.
   188      *
   189      * <p>To create localized string representations of a floating-point
   190      * value, use subclasses of {@link java.text.NumberFormat}.
   191      *
   192      * @param   f   the float to be converted.
   193      * @return a string representation of the argument.
   194      */
   195     public static String toString(float f) {
   196         return Double.toString(f);
   197     }
   198 
   199     /**
   200      * Returns a hexadecimal string representation of the
   201      * {@code float} argument. All characters mentioned below are
   202      * ASCII characters.
   203      *
   204      * <ul>
   205      * <li>If the argument is NaN, the result is the string
   206      *     "{@code NaN}".
   207      * <li>Otherwise, the result is a string that represents the sign and
   208      * magnitude (absolute value) of the argument. If the sign is negative,
   209      * the first character of the result is '{@code -}'
   210      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
   211      * appears in the result. As for the magnitude <i>m</i>:
   212      *
   213      * <ul>
   214      * <li>If <i>m</i> is infinity, it is represented by the string
   215      * {@code "Infinity"}; thus, positive infinity produces the
   216      * result {@code "Infinity"} and negative infinity produces
   217      * the result {@code "-Infinity"}.
   218      *
   219      * <li>If <i>m</i> is zero, it is represented by the string
   220      * {@code "0x0.0p0"}; thus, negative zero produces the result
   221      * {@code "-0x0.0p0"} and positive zero produces the result
   222      * {@code "0x0.0p0"}.
   223      *
   224      * <li>If <i>m</i> is a {@code float} value with a
   225      * normalized representation, substrings are used to represent the
   226      * significand and exponent fields.  The significand is
   227      * represented by the characters {@code "0x1."}
   228      * followed by a lowercase hexadecimal representation of the rest
   229      * of the significand as a fraction.  Trailing zeros in the
   230      * hexadecimal representation are removed unless all the digits
   231      * are zero, in which case a single zero is used. Next, the
   232      * exponent is represented by {@code "p"} followed
   233      * by a decimal string of the unbiased exponent as if produced by
   234      * a call to {@link Integer#toString(int) Integer.toString} on the
   235      * exponent value.
   236      *
   237      * <li>If <i>m</i> is a {@code float} value with a subnormal
   238      * representation, the significand is represented by the
   239      * characters {@code "0x0."} followed by a
   240      * hexadecimal representation of the rest of the significand as a
   241      * fraction.  Trailing zeros in the hexadecimal representation are
   242      * removed. Next, the exponent is represented by
   243      * {@code "p-126"}.  Note that there must be at
   244      * least one nonzero digit in a subnormal significand.
   245      *
   246      * </ul>
   247      *
   248      * </ul>
   249      *
   250      * <table border>
   251      * <caption><h3>Examples</h3></caption>
   252      * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
   253      * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
   254      * <tr><td>{@code -1.0}</td>        <td>{@code -0x1.0p0}</td>
   255      * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
   256      * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
   257      * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
   258      * <tr><td>{@code 0.25}</td>        <td>{@code 0x1.0p-2}</td>
   259      * <tr><td>{@code Float.MAX_VALUE}</td>
   260      *     <td>{@code 0x1.fffffep127}</td>
   261      * <tr><td>{@code Minimum Normal Value}</td>
   262      *     <td>{@code 0x1.0p-126}</td>
   263      * <tr><td>{@code Maximum Subnormal Value}</td>
   264      *     <td>{@code 0x0.fffffep-126}</td>
   265      * <tr><td>{@code Float.MIN_VALUE}</td>
   266      *     <td>{@code 0x0.000002p-126}</td>
   267      * </table>
   268      * @param   f   the {@code float} to be converted.
   269      * @return a hex string representation of the argument.
   270      * @since 1.5
   271      * @author Joseph D. Darcy
   272      */
   273     public static String toHexString(float f) {
   274         throw new UnsupportedOperationException();
   275 //        if (Math.abs(f) < FloatConsts.MIN_NORMAL
   276 //            &&  f != 0.0f ) {// float subnormal
   277 //            // Adjust exponent to create subnormal double, then
   278 //            // replace subnormal double exponent with subnormal float
   279 //            // exponent
   280 //            String s = Double.toHexString(FpUtils.scalb((double)f,
   281 //                                                        /* -1022+126 */
   282 //                                                        DoubleConsts.MIN_EXPONENT-
   283 //                                                        FloatConsts.MIN_EXPONENT));
   284 //            return s.replaceFirst("p-1022$", "p-126");
   285 //        }
   286 //        else // double string will be the same as float string
   287 //            return Double.toHexString(f);
   288     }
   289 
   290     /**
   291      * Returns a {@code Float} object holding the
   292      * {@code float} value represented by the argument string
   293      * {@code s}.
   294      *
   295      * <p>If {@code s} is {@code null}, then a
   296      * {@code NullPointerException} is thrown.
   297      *
   298      * <p>Leading and trailing whitespace characters in {@code s}
   299      * are ignored.  Whitespace is removed as if by the {@link
   300      * String#trim} method; that is, both ASCII space and control
   301      * characters are removed. The rest of {@code s} should
   302      * constitute a <i>FloatValue</i> as described by the lexical
   303      * syntax rules:
   304      *
   305      * <blockquote>
   306      * <dl>
   307      * <dt><i>FloatValue:</i>
   308      * <dd><i>Sign<sub>opt</sub></i> {@code NaN}
   309      * <dd><i>Sign<sub>opt</sub></i> {@code Infinity}
   310      * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
   311      * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
   312      * <dd><i>SignedInteger</i>
   313      * </dl>
   314      *
   315      * <p>
   316      *
   317      * <dl>
   318      * <dt><i>HexFloatingPointLiteral</i>:
   319      * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
   320      * </dl>
   321      *
   322      * <p>
   323      *
   324      * <dl>
   325      * <dt><i>HexSignificand:</i>
   326      * <dd><i>HexNumeral</i>
   327      * <dd><i>HexNumeral</i> {@code .}
   328      * <dd>{@code 0x} <i>HexDigits<sub>opt</sub>
   329      *     </i>{@code .}<i> HexDigits</i>
   330      * <dd>{@code 0X}<i> HexDigits<sub>opt</sub>
   331      *     </i>{@code .} <i>HexDigits</i>
   332      * </dl>
   333      *
   334      * <p>
   335      *
   336      * <dl>
   337      * <dt><i>BinaryExponent:</i>
   338      * <dd><i>BinaryExponentIndicator SignedInteger</i>
   339      * </dl>
   340      *
   341      * <p>
   342      *
   343      * <dl>
   344      * <dt><i>BinaryExponentIndicator:</i>
   345      * <dd>{@code p}
   346      * <dd>{@code P}
   347      * </dl>
   348      *
   349      * </blockquote>
   350      *
   351      * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
   352      * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
   353      * <i>FloatTypeSuffix</i> are as defined in the lexical structure
   354      * sections of
   355      * <cite>The Java&trade; Language Specification</cite>,
   356      * except that underscores are not accepted between digits.
   357      * If {@code s} does not have the form of
   358      * a <i>FloatValue</i>, then a {@code NumberFormatException}
   359      * is thrown. Otherwise, {@code s} is regarded as
   360      * representing an exact decimal value in the usual
   361      * "computerized scientific notation" or as an exact
   362      * hexadecimal value; this exact numerical value is then
   363      * conceptually converted to an "infinitely precise"
   364      * binary value that is then rounded to type {@code float}
   365      * by the usual round-to-nearest rule of IEEE 754 floating-point
   366      * arithmetic, which includes preserving the sign of a zero
   367      * value.
   368      *
   369      * Note that the round-to-nearest rule also implies overflow and
   370      * underflow behaviour; if the exact value of {@code s} is large
   371      * enough in magnitude (greater than or equal to ({@link
   372      * #MAX_VALUE} + {@link Math#ulp(float) ulp(MAX_VALUE)}/2),
   373      * rounding to {@code float} will result in an infinity and if the
   374      * exact value of {@code s} is small enough in magnitude (less
   375      * than or equal to {@link #MIN_VALUE}/2), rounding to float will
   376      * result in a zero.
   377      *
   378      * Finally, after rounding a {@code Float} object representing
   379      * this {@code float} value is returned.
   380      *
   381      * <p>To interpret localized string representations of a
   382      * floating-point value, use subclasses of {@link
   383      * java.text.NumberFormat}.
   384      *
   385      * <p>Note that trailing format specifiers, specifiers that
   386      * determine the type of a floating-point literal
   387      * ({@code 1.0f} is a {@code float} value;
   388      * {@code 1.0d} is a {@code double} value), do
   389      * <em>not</em> influence the results of this method.  In other
   390      * words, the numerical value of the input string is converted
   391      * directly to the target floating-point type.  In general, the
   392      * two-step sequence of conversions, string to {@code double}
   393      * followed by {@code double} to {@code float}, is
   394      * <em>not</em> equivalent to converting a string directly to
   395      * {@code float}.  For example, if first converted to an
   396      * intermediate {@code double} and then to
   397      * {@code float}, the string<br>
   398      * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
   399      * results in the {@code float} value
   400      * {@code 1.0000002f}; if the string is converted directly to
   401      * {@code float}, <code>1.000000<b>1</b>f</code> results.
   402      *
   403      * <p>To avoid calling this method on an invalid string and having
   404      * a {@code NumberFormatException} be thrown, the documentation
   405      * for {@link Double#valueOf Double.valueOf} lists a regular
   406      * expression which can be used to screen the input.
   407      *
   408      * @param   s   the string to be parsed.
   409      * @return  a {@code Float} object holding the value
   410      *          represented by the {@code String} argument.
   411      * @throws  NumberFormatException  if the string does not contain a
   412      *          parsable number.
   413      */
   414     public static Float valueOf(String s) throws NumberFormatException {
   415         throw new UnsupportedOperationException();
   416 //        return new Float(FloatingDecimal.readJavaFormatString(s).floatValue());
   417     }
   418 
   419     /**
   420      * Returns a {@code Float} instance representing the specified
   421      * {@code float} value.
   422      * If a new {@code Float} instance is not required, this method
   423      * should generally be used in preference to the constructor
   424      * {@link #Float(float)}, as this method is likely to yield
   425      * significantly better space and time performance by caching
   426      * frequently requested values.
   427      *
   428      * @param  f a float value.
   429      * @return a {@code Float} instance representing {@code f}.
   430      * @since  1.5
   431      */
   432     public static Float valueOf(float f) {
   433         return new Float(f);
   434     }
   435 
   436     /**
   437      * Returns a new {@code float} initialized to the value
   438      * represented by the specified {@code String}, as performed
   439      * by the {@code valueOf} method of class {@code Float}.
   440      *
   441      * @param  s the string to be parsed.
   442      * @return the {@code float} value represented by the string
   443      *         argument.
   444      * @throws NullPointerException  if the string is null
   445      * @throws NumberFormatException if the string does not contain a
   446      *               parsable {@code float}.
   447      * @see    java.lang.Float#valueOf(String)
   448      * @since 1.2
   449      */
   450     public static float parseFloat(String s) throws NumberFormatException {
   451         throw new UnsupportedOperationException();
   452 //        return FloatingDecimal.readJavaFormatString(s).floatValue();
   453     }
   454 
   455     /**
   456      * Returns {@code true} if the specified number is a
   457      * Not-a-Number (NaN) value, {@code false} otherwise.
   458      *
   459      * @param   v   the value to be tested.
   460      * @return  {@code true} if the argument is NaN;
   461      *          {@code false} otherwise.
   462      */
   463     static public boolean isNaN(float v) {
   464         return (v != v);
   465     }
   466 
   467     /**
   468      * Returns {@code true} if the specified number is infinitely
   469      * large in magnitude, {@code false} otherwise.
   470      *
   471      * @param   v   the value to be tested.
   472      * @return  {@code true} if the argument is positive infinity or
   473      *          negative infinity; {@code false} otherwise.
   474      */
   475     static public boolean isInfinite(float v) {
   476         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
   477     }
   478 
   479     /**
   480      * The value of the Float.
   481      *
   482      * @serial
   483      */
   484     private final float value;
   485 
   486     /**
   487      * Constructs a newly allocated {@code Float} object that
   488      * represents the primitive {@code float} argument.
   489      *
   490      * @param   value   the value to be represented by the {@code Float}.
   491      */
   492     public Float(float value) {
   493         this.value = value;
   494     }
   495 
   496     /**
   497      * Constructs a newly allocated {@code Float} object that
   498      * represents the argument converted to type {@code float}.
   499      *
   500      * @param   value   the value to be represented by the {@code Float}.
   501      */
   502     public Float(double value) {
   503         this.value = (float)value;
   504     }
   505 
   506     /**
   507      * Constructs a newly allocated {@code Float} object that
   508      * represents the floating-point value of type {@code float}
   509      * represented by the string. The string is converted to a
   510      * {@code float} value as if by the {@code valueOf} method.
   511      *
   512      * @param      s   a string to be converted to a {@code Float}.
   513      * @throws  NumberFormatException  if the string does not contain a
   514      *               parsable number.
   515      * @see        java.lang.Float#valueOf(java.lang.String)
   516      */
   517     public Float(String s) throws NumberFormatException {
   518         // REMIND: this is inefficient
   519         this(valueOf(s).floatValue());
   520     }
   521 
   522     /**
   523      * Returns {@code true} if this {@code Float} value is a
   524      * Not-a-Number (NaN), {@code false} otherwise.
   525      *
   526      * @return  {@code true} if the value represented by this object is
   527      *          NaN; {@code false} otherwise.
   528      */
   529     public boolean isNaN() {
   530         return isNaN(value);
   531     }
   532 
   533     /**
   534      * Returns {@code true} if this {@code Float} value is
   535      * infinitely large in magnitude, {@code false} otherwise.
   536      *
   537      * @return  {@code true} if the value represented by this object is
   538      *          positive infinity or negative infinity;
   539      *          {@code false} otherwise.
   540      */
   541     public boolean isInfinite() {
   542         return isInfinite(value);
   543     }
   544 
   545     /**
   546      * Returns a string representation of this {@code Float} object.
   547      * The primitive {@code float} value represented by this object
   548      * is converted to a {@code String} exactly as if by the method
   549      * {@code toString} of one argument.
   550      *
   551      * @return  a {@code String} representation of this object.
   552      * @see java.lang.Float#toString(float)
   553      */
   554     public String toString() {
   555         return Float.toString(value);
   556     }
   557 
   558     /**
   559      * Returns the value of this {@code Float} as a {@code byte} (by
   560      * casting to a {@code byte}).
   561      *
   562      * @return  the {@code float} value represented by this object
   563      *          converted to type {@code byte}
   564      */
   565     public byte byteValue() {
   566         return (byte)value;
   567     }
   568 
   569     /**
   570      * Returns the value of this {@code Float} as a {@code short} (by
   571      * casting to a {@code short}).
   572      *
   573      * @return  the {@code float} value represented by this object
   574      *          converted to type {@code short}
   575      * @since JDK1.1
   576      */
   577     public short shortValue() {
   578         return (short)value;
   579     }
   580 
   581     /**
   582      * Returns the value of this {@code Float} as an {@code int} (by
   583      * casting to type {@code int}).
   584      *
   585      * @return  the {@code float} value represented by this object
   586      *          converted to type {@code int}
   587      */
   588     public int intValue() {
   589         return (int)value;
   590     }
   591 
   592     /**
   593      * Returns value of this {@code Float} as a {@code long} (by
   594      * casting to type {@code long}).
   595      *
   596      * @return  the {@code float} value represented by this object
   597      *          converted to type {@code long}
   598      */
   599     public long longValue() {
   600         return (long)value;
   601     }
   602 
   603     /**
   604      * Returns the {@code float} value of this {@code Float} object.
   605      *
   606      * @return the {@code float} value represented by this object
   607      */
   608     public float floatValue() {
   609         return value;
   610     }
   611 
   612     /**
   613      * Returns the {@code double} value of this {@code Float} object.
   614      *
   615      * @return the {@code float} value represented by this
   616      *         object is converted to type {@code double} and the
   617      *         result of the conversion is returned.
   618      */
   619     public double doubleValue() {
   620         return (double)value;
   621     }
   622 
   623     /**
   624      * Returns a hash code for this {@code Float} object. The
   625      * result is the integer bit representation, exactly as produced
   626      * by the method {@link #floatToIntBits(float)}, of the primitive
   627      * {@code float} value represented by this {@code Float}
   628      * object.
   629      *
   630      * @return a hash code value for this object.
   631      */
   632     public int hashCode() {
   633         return floatToIntBits(value);
   634     }
   635 
   636     /**
   637 
   638      * Compares this object against the specified object.  The result
   639      * is {@code true} if and only if the argument is not
   640      * {@code null} and is a {@code Float} object that
   641      * represents a {@code float} with the same value as the
   642      * {@code float} represented by this object. For this
   643      * purpose, two {@code float} values are considered to be the
   644      * same if and only if the method {@link #floatToIntBits(float)}
   645      * returns the identical {@code int} value when applied to
   646      * each.
   647      *
   648      * <p>Note that in most cases, for two instances of class
   649      * {@code Float}, {@code f1} and {@code f2}, the value
   650      * of {@code f1.equals(f2)} is {@code true} if and only if
   651      *
   652      * <blockquote><pre>
   653      *   f1.floatValue() == f2.floatValue()
   654      * </pre></blockquote>
   655      *
   656      * <p>also has the value {@code true}. However, there are two exceptions:
   657      * <ul>
   658      * <li>If {@code f1} and {@code f2} both represent
   659      *     {@code Float.NaN}, then the {@code equals} method returns
   660      *     {@code true}, even though {@code Float.NaN==Float.NaN}
   661      *     has the value {@code false}.
   662      * <li>If {@code f1} represents {@code +0.0f} while
   663      *     {@code f2} represents {@code -0.0f}, or vice
   664      *     versa, the {@code equal} test has the value
   665      *     {@code false}, even though {@code 0.0f==-0.0f}
   666      *     has the value {@code true}.
   667      * </ul>
   668      *
   669      * This definition allows hash tables to operate properly.
   670      *
   671      * @param obj the object to be compared
   672      * @return  {@code true} if the objects are the same;
   673      *          {@code false} otherwise.
   674      * @see java.lang.Float#floatToIntBits(float)
   675      */
   676     public boolean equals(Object obj) {
   677         return (obj instanceof Float)
   678                && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
   679     }
   680 
   681     /**
   682      * Returns a representation of the specified floating-point value
   683      * according to the IEEE 754 floating-point "single format" bit
   684      * layout.
   685      *
   686      * <p>Bit 31 (the bit that is selected by the mask
   687      * {@code 0x80000000}) represents the sign of the floating-point
   688      * number.
   689      * Bits 30-23 (the bits that are selected by the mask
   690      * {@code 0x7f800000}) represent the exponent.
   691      * Bits 22-0 (the bits that are selected by the mask
   692      * {@code 0x007fffff}) represent the significand (sometimes called
   693      * the mantissa) of the floating-point number.
   694      *
   695      * <p>If the argument is positive infinity, the result is
   696      * {@code 0x7f800000}.
   697      *
   698      * <p>If the argument is negative infinity, the result is
   699      * {@code 0xff800000}.
   700      *
   701      * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
   702      *
   703      * <p>In all cases, the result is an integer that, when given to the
   704      * {@link #intBitsToFloat(int)} method, will produce a floating-point
   705      * value the same as the argument to {@code floatToIntBits}
   706      * (except all NaN values are collapsed to a single
   707      * "canonical" NaN value).
   708      *
   709      * @param   value   a floating-point number.
   710      * @return the bits that represent the floating-point number.
   711      */
   712     public static int floatToIntBits(float value) {
   713         final int EXP_BIT_MASK = 2139095040;
   714         final int SIGNIF_BIT_MASK = 8388607;
   715         
   716         int result = floatToRawIntBits(value);
   717         // Check for NaN based on values of bit fields, maximum
   718         // exponent and nonzero significand.
   719         if ( ((result & EXP_BIT_MASK) ==
   720               EXP_BIT_MASK) &&
   721              (result & SIGNIF_BIT_MASK) != 0)
   722             result = 0x7fc00000;
   723         return result;
   724     }
   725 
   726     /**
   727      * Returns a representation of the specified floating-point value
   728      * according to the IEEE 754 floating-point "single format" bit
   729      * layout, preserving Not-a-Number (NaN) values.
   730      *
   731      * <p>Bit 31 (the bit that is selected by the mask
   732      * {@code 0x80000000}) represents the sign of the floating-point
   733      * number.
   734      * Bits 30-23 (the bits that are selected by the mask
   735      * {@code 0x7f800000}) represent the exponent.
   736      * Bits 22-0 (the bits that are selected by the mask
   737      * {@code 0x007fffff}) represent the significand (sometimes called
   738      * the mantissa) of the floating-point number.
   739      *
   740      * <p>If the argument is positive infinity, the result is
   741      * {@code 0x7f800000}.
   742      *
   743      * <p>If the argument is negative infinity, the result is
   744      * {@code 0xff800000}.
   745      *
   746      * <p>If the argument is NaN, the result is the integer representing
   747      * the actual NaN value.  Unlike the {@code floatToIntBits}
   748      * method, {@code floatToRawIntBits} does not collapse all the
   749      * bit patterns encoding a NaN to a single "canonical"
   750      * NaN value.
   751      *
   752      * <p>In all cases, the result is an integer that, when given to the
   753      * {@link #intBitsToFloat(int)} method, will produce a
   754      * floating-point value the same as the argument to
   755      * {@code floatToRawIntBits}.
   756      *
   757      * @param   value   a floating-point number.
   758      * @return the bits that represent the floating-point number.
   759      * @since 1.3
   760      */
   761     @JavaScriptBody(args = { "value" }, body = ""
   762         + "var a = new ArrayBuffer(4);"
   763         + "new Float32Array(a)[0] = value;"
   764         + "return new Int32Array(a)[0];"
   765     )
   766     public static native int floatToRawIntBits(float value);
   767 
   768     /**
   769      * Returns the {@code float} value corresponding to a given
   770      * bit representation.
   771      * The argument is considered to be a representation of a
   772      * floating-point value according to the IEEE 754 floating-point
   773      * "single format" bit layout.
   774      *
   775      * <p>If the argument is {@code 0x7f800000}, the result is positive
   776      * infinity.
   777      *
   778      * <p>If the argument is {@code 0xff800000}, the result is negative
   779      * infinity.
   780      *
   781      * <p>If the argument is any value in the range
   782      * {@code 0x7f800001} through {@code 0x7fffffff} or in
   783      * the range {@code 0xff800001} through
   784      * {@code 0xffffffff}, the result is a NaN.  No IEEE 754
   785      * floating-point operation provided by Java can distinguish
   786      * between two NaN values of the same type with different bit
   787      * patterns.  Distinct values of NaN are only distinguishable by
   788      * use of the {@code Float.floatToRawIntBits} method.
   789      *
   790      * <p>In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
   791      * values that can be computed from the argument:
   792      *
   793      * <blockquote><pre>
   794      * int s = ((bits &gt;&gt; 31) == 0) ? 1 : -1;
   795      * int e = ((bits &gt;&gt; 23) & 0xff);
   796      * int m = (e == 0) ?
   797      *                 (bits & 0x7fffff) &lt;&lt; 1 :
   798      *                 (bits & 0x7fffff) | 0x800000;
   799      * </pre></blockquote>
   800      *
   801      * Then the floating-point result equals the value of the mathematical
   802      * expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-150</sup>.
   803      *
   804      * <p>Note that this method may not be able to return a
   805      * {@code float} NaN with exactly same bit pattern as the
   806      * {@code int} argument.  IEEE 754 distinguishes between two
   807      * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
   808      * differences between the two kinds of NaN are generally not
   809      * visible in Java.  Arithmetic operations on signaling NaNs turn
   810      * them into quiet NaNs with a different, but often similar, bit
   811      * pattern.  However, on some processors merely copying a
   812      * signaling NaN also performs that conversion.  In particular,
   813      * copying a signaling NaN to return it to the calling method may
   814      * perform this conversion.  So {@code intBitsToFloat} may
   815      * not be able to return a {@code float} with a signaling NaN
   816      * bit pattern.  Consequently, for some {@code int} values,
   817      * {@code floatToRawIntBits(intBitsToFloat(start))} may
   818      * <i>not</i> equal {@code start}.  Moreover, which
   819      * particular bit patterns represent signaling NaNs is platform
   820      * dependent; although all NaN bit patterns, quiet or signaling,
   821      * must be in the NaN range identified above.
   822      *
   823      * @param   bits   an integer.
   824      * @return  the {@code float} floating-point value with the same bit
   825      *          pattern.
   826      */
   827     @JavaScriptBody(args = "bits",
   828         body = 
   829           "var s = ((bits >> 31) == 0) ? 1 : -1;\n"
   830         + "var e = ((bits >> 23) & 0xff);\n"
   831         + "if (e === 0xff) {\n"
   832         + "  if ((bits & 0x7fffff) === 0) {\n"
   833         + "    return (s > 0) ? Number.POSITIVE_INFINITY"
   834                           + " : Number.NEGATIVE_INFINITY;\n"
   835         + "  }\n"
   836         + "  return Number.NaN;\n"
   837         + "}\n"
   838         + "var m = (e === 0) ?\n"
   839         + "  (bits & 0x7fffff) << 1 :\n"
   840         + "  (bits & 0x7fffff) | 0x800000;\n"
   841         + "return s * m * Math.pow(2.0, e - 150);\n"
   842     )
   843     public static native float intBitsToFloat(int bits);
   844 
   845     /**
   846      * Compares two {@code Float} objects numerically.  There are
   847      * two ways in which comparisons performed by this method differ
   848      * from those performed by the Java language numerical comparison
   849      * operators ({@code <, <=, ==, >=, >}) when
   850      * applied to primitive {@code float} values:
   851      *
   852      * <ul><li>
   853      *          {@code Float.NaN} is considered by this method to
   854      *          be equal to itself and greater than all other
   855      *          {@code float} values
   856      *          (including {@code Float.POSITIVE_INFINITY}).
   857      * <li>
   858      *          {@code 0.0f} is considered by this method to be greater
   859      *          than {@code -0.0f}.
   860      * </ul>
   861      *
   862      * This ensures that the <i>natural ordering</i> of {@code Float}
   863      * objects imposed by this method is <i>consistent with equals</i>.
   864      *
   865      * @param   anotherFloat   the {@code Float} to be compared.
   866      * @return  the value {@code 0} if {@code anotherFloat} is
   867      *          numerically equal to this {@code Float}; a value
   868      *          less than {@code 0} if this {@code Float}
   869      *          is numerically less than {@code anotherFloat};
   870      *          and a value greater than {@code 0} if this
   871      *          {@code Float} is numerically greater than
   872      *          {@code anotherFloat}.
   873      *
   874      * @since   1.2
   875      * @see Comparable#compareTo(Object)
   876      */
   877     public int compareTo(Float anotherFloat) {
   878         return Float.compare(value, anotherFloat.value);
   879     }
   880 
   881     /**
   882      * Compares the two specified {@code float} values. The sign
   883      * of the integer value returned is the same as that of the
   884      * integer that would be returned by the call:
   885      * <pre>
   886      *    new Float(f1).compareTo(new Float(f2))
   887      * </pre>
   888      *
   889      * @param   f1        the first {@code float} to compare.
   890      * @param   f2        the second {@code float} to compare.
   891      * @return  the value {@code 0} if {@code f1} is
   892      *          numerically equal to {@code f2}; a value less than
   893      *          {@code 0} if {@code f1} is numerically less than
   894      *          {@code f2}; and a value greater than {@code 0}
   895      *          if {@code f1} is numerically greater than
   896      *          {@code f2}.
   897      * @since 1.4
   898      */
   899     public static int compare(float f1, float f2) {
   900         if (f1 < f2)
   901             return -1;           // Neither val is NaN, thisVal is smaller
   902         if (f1 > f2)
   903             return 1;            // Neither val is NaN, thisVal is larger
   904 
   905         // Cannot use floatToRawIntBits because of possibility of NaNs.
   906         int thisBits    = Float.floatToIntBits(f1);
   907         int anotherBits = Float.floatToIntBits(f2);
   908 
   909         return (thisBits == anotherBits ?  0 : // Values are equal
   910                 (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
   911                  1));                          // (0.0, -0.0) or (NaN, !NaN)
   912     }
   913 
   914     /** use serialVersionUID from JDK 1.0.2 for interoperability */
   915     private static final long serialVersionUID = -2671257302660747028L;
   916 }