src/share/classes/java/lang/Float.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2012, 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


 186      * <i>m</i> or <i>a</i>? There must be at least one digit
 187      * to represent the fractional part, and beyond that as many, but
 188      * only as many, more digits as are needed to uniquely distinguish
 189      * the argument value from adjacent values of type
 190      * {@code float}. That is, suppose that <i>x</i> is the
 191      * exact mathematical value represented by the decimal
 192      * representation produced by this method for a finite nonzero
 193      * argument <i>f</i>. Then <i>f</i> must be the {@code float}
 194      * value nearest to <i>x</i>; or, if two {@code float} values are
 195      * equally close to <i>x</i>, then <i>f</i> must be one of
 196      * them and the least significant bit of the significand of
 197      * <i>f</i> must be {@code 0}.
 198      *
 199      * <p>To create localized string representations of a floating-point
 200      * value, use subclasses of {@link java.text.NumberFormat}.
 201      *
 202      * @param   f   the float to be converted.
 203      * @return a string representation of the argument.
 204      */
 205     public static String toString(float f) {
 206         return new FloatingDecimal(f).toJavaFormatString();
 207     }
 208 
 209     /**
 210      * Returns a hexadecimal string representation of the
 211      * {@code float} argument. All characters mentioned below are
 212      * ASCII characters.
 213      *
 214      * <ul>
 215      * <li>If the argument is NaN, the result is the string
 216      *     "{@code NaN}".
 217      * <li>Otherwise, the result is a string that represents the sign and
 218      * magnitude (absolute value) of the argument. If the sign is negative,
 219      * the first character of the result is '{@code -}'
 220      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 221      * appears in the result. As for the magnitude <i>m</i>:
 222      *
 223      * <ul>
 224      * <li>If <i>m</i> is infinity, it is represented by the string
 225      * {@code "Infinity"}; thus, positive infinity produces the
 226      * result {@code "Infinity"} and negative infinity produces


 404      * {@code float}.  For example, if first converted to an
 405      * intermediate {@code double} and then to
 406      * {@code float}, the string<br>
 407      * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
 408      * results in the {@code float} value
 409      * {@code 1.0000002f}; if the string is converted directly to
 410      * {@code float}, <code>1.000000<b>1</b>f</code> results.
 411      *
 412      * <p>To avoid calling this method on an invalid string and having
 413      * a {@code NumberFormatException} be thrown, the documentation
 414      * for {@link Double#valueOf Double.valueOf} lists a regular
 415      * expression which can be used to screen the input.
 416      *
 417      * @param   s   the string to be parsed.
 418      * @return  a {@code Float} object holding the value
 419      *          represented by the {@code String} argument.
 420      * @throws  NumberFormatException  if the string does not contain a
 421      *          parsable number.
 422      */
 423     public static Float valueOf(String s) throws NumberFormatException {
 424         return new Float(FloatingDecimal.readJavaFormatString(s).floatValue());
 425     }
 426 
 427     /**
 428      * Returns a {@code Float} instance representing the specified
 429      * {@code float} value.
 430      * If a new {@code Float} instance is not required, this method
 431      * should generally be used in preference to the constructor
 432      * {@link #Float(float)}, as this method is likely to yield
 433      * significantly better space and time performance by caching
 434      * frequently requested values.
 435      *
 436      * @param  f a float value.
 437      * @return a {@code Float} instance representing {@code f}.
 438      * @since  1.5
 439      */
 440     public static Float valueOf(float f) {
 441         return new Float(f);
 442     }
 443 
 444     /**
 445      * Returns a new {@code float} initialized to the value
 446      * represented by the specified {@code String}, as performed
 447      * by the {@code valueOf} method of class {@code Float}.
 448      *
 449      * @param  s the string to be parsed.
 450      * @return the {@code float} value represented by the string
 451      *         argument.
 452      * @throws NullPointerException  if the string is null
 453      * @throws NumberFormatException if the string does not contain a
 454      *               parsable {@code float}.
 455      * @see    java.lang.Float#valueOf(String)
 456      * @since 1.2
 457      */
 458     public static float parseFloat(String s) throws NumberFormatException {
 459         return FloatingDecimal.readJavaFormatString(s).floatValue();
 460     }
 461 
 462     /**
 463      * Returns {@code true} if the specified number is a
 464      * Not-a-Number (NaN) value, {@code false} otherwise.
 465      *
 466      * @param   v   the value to be tested.
 467      * @return  {@code true} if the argument is NaN;
 468      *          {@code false} otherwise.
 469      */
 470     public static boolean isNaN(float v) {
 471         return (v != v);
 472     }
 473 
 474     /**
 475      * Returns {@code true} if the specified number is infinitely
 476      * large in magnitude, {@code false} otherwise.
 477      *
 478      * @param   v   the value to be tested.
 479      * @return  {@code true} if the argument is positive infinity or


   1 /*
   2  * Copyright (c) 1994, 2013, 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


 186      * <i>m</i> or <i>a</i>? There must be at least one digit
 187      * to represent the fractional part, and beyond that as many, but
 188      * only as many, more digits as are needed to uniquely distinguish
 189      * the argument value from adjacent values of type
 190      * {@code float}. That is, suppose that <i>x</i> is the
 191      * exact mathematical value represented by the decimal
 192      * representation produced by this method for a finite nonzero
 193      * argument <i>f</i>. Then <i>f</i> must be the {@code float}
 194      * value nearest to <i>x</i>; or, if two {@code float} values are
 195      * equally close to <i>x</i>, then <i>f</i> must be one of
 196      * them and the least significant bit of the significand of
 197      * <i>f</i> must be {@code 0}.
 198      *
 199      * <p>To create localized string representations of a floating-point
 200      * value, use subclasses of {@link java.text.NumberFormat}.
 201      *
 202      * @param   f   the float to be converted.
 203      * @return a string representation of the argument.
 204      */
 205     public static String toString(float f) {
 206         return FloatingDecimal.toJavaFormatString(f);
 207     }
 208 
 209     /**
 210      * Returns a hexadecimal string representation of the
 211      * {@code float} argument. All characters mentioned below are
 212      * ASCII characters.
 213      *
 214      * <ul>
 215      * <li>If the argument is NaN, the result is the string
 216      *     "{@code NaN}".
 217      * <li>Otherwise, the result is a string that represents the sign and
 218      * magnitude (absolute value) of the argument. If the sign is negative,
 219      * the first character of the result is '{@code -}'
 220      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 221      * appears in the result. As for the magnitude <i>m</i>:
 222      *
 223      * <ul>
 224      * <li>If <i>m</i> is infinity, it is represented by the string
 225      * {@code "Infinity"}; thus, positive infinity produces the
 226      * result {@code "Infinity"} and negative infinity produces


 404      * {@code float}.  For example, if first converted to an
 405      * intermediate {@code double} and then to
 406      * {@code float}, the string<br>
 407      * {@code "1.00000017881393421514957253748434595763683319091796875001d"}<br>
 408      * results in the {@code float} value
 409      * {@code 1.0000002f}; if the string is converted directly to
 410      * {@code float}, <code>1.000000<b>1</b>f</code> results.
 411      *
 412      * <p>To avoid calling this method on an invalid string and having
 413      * a {@code NumberFormatException} be thrown, the documentation
 414      * for {@link Double#valueOf Double.valueOf} lists a regular
 415      * expression which can be used to screen the input.
 416      *
 417      * @param   s   the string to be parsed.
 418      * @return  a {@code Float} object holding the value
 419      *          represented by the {@code String} argument.
 420      * @throws  NumberFormatException  if the string does not contain a
 421      *          parsable number.
 422      */
 423     public static Float valueOf(String s) throws NumberFormatException {
 424         return new Float(parseFloat(s));
 425     }
 426 
 427     /**
 428      * Returns a {@code Float} instance representing the specified
 429      * {@code float} value.
 430      * If a new {@code Float} instance is not required, this method
 431      * should generally be used in preference to the constructor
 432      * {@link #Float(float)}, as this method is likely to yield
 433      * significantly better space and time performance by caching
 434      * frequently requested values.
 435      *
 436      * @param  f a float value.
 437      * @return a {@code Float} instance representing {@code f}.
 438      * @since  1.5
 439      */
 440     public static Float valueOf(float f) {
 441         return new Float(f);
 442     }
 443 
 444     /**
 445      * Returns a new {@code float} initialized to the value
 446      * represented by the specified {@code String}, as performed
 447      * by the {@code valueOf} method of class {@code Float}.
 448      *
 449      * @param  s the string to be parsed.
 450      * @return the {@code float} value represented by the string
 451      *         argument.
 452      * @throws NullPointerException  if the string is null
 453      * @throws NumberFormatException if the string does not contain a
 454      *               parsable {@code float}.
 455      * @see    java.lang.Float#valueOf(String)
 456      * @since 1.2
 457      */
 458     public static float parseFloat(String s) throws NumberFormatException {
 459         return FloatingDecimal.parseFloat(s);
 460     }
 461 
 462     /**
 463      * Returns {@code true} if the specified number is a
 464      * Not-a-Number (NaN) value, {@code false} otherwise.
 465      *
 466      * @param   v   the value to be tested.
 467      * @return  {@code true} if the argument is NaN;
 468      *          {@code false} otherwise.
 469      */
 470     public static boolean isNaN(float v) {
 471         return (v != v);
 472     }
 473 
 474     /**
 475      * Returns {@code true} if the specified number is infinitely
 476      * large in magnitude, {@code false} otherwise.
 477      *
 478      * @param   v   the value to be tested.
 479      * @return  {@code true} if the argument is positive infinity or