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

Print this page




   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 sun.misc.FloatingDecimal;
  29 import sun.misc.FpUtils;
  30 import sun.misc.FloatConsts;
  31 import sun.misc.DoubleConsts;
  32 
  33 /**
  34  * The {@code Float} class wraps a value of primitive type
  35  * {@code float} in an object. An object of type
  36  * {@code Float} contains a single field whose type is
  37  * {@code float}.
  38  *
  39  * <p>In addition, this class provides several methods for converting a
  40  * {@code float} to a {@code String} and a
  41  * {@code String} to a {@code float}, as well as other
  42  * constants and methods useful when dealing with a
  43  * {@code float}.
  44  *
  45  * @author  Lee Boynton
  46  * @author  Arthur van Hoff
  47  * @author  Joseph D. Darcy
  48  * @since JDK1.0
  49  */


 262      * <tr><td>{@code Float.MAX_VALUE}</td>
 263      *     <td>{@code 0x1.fffffep127}</td>
 264      * <tr><td>{@code Minimum Normal Value}</td>
 265      *     <td>{@code 0x1.0p-126}</td>
 266      * <tr><td>{@code Maximum Subnormal Value}</td>
 267      *     <td>{@code 0x0.fffffep-126}</td>
 268      * <tr><td>{@code Float.MIN_VALUE}</td>
 269      *     <td>{@code 0x0.000002p-126}</td>
 270      * </table>
 271      * @param   f   the {@code float} to be converted.
 272      * @return a hex string representation of the argument.
 273      * @since 1.5
 274      * @author Joseph D. Darcy
 275      */
 276     public static String toHexString(float f) {
 277         if (Math.abs(f) < FloatConsts.MIN_NORMAL
 278             &&  f != 0.0f ) {// float subnormal
 279             // Adjust exponent to create subnormal double, then
 280             // replace subnormal double exponent with subnormal float
 281             // exponent
 282             String s = Double.toHexString(FpUtils.scalb((double)f,
 283                                                         /* -1022+126 */
 284                                                         DoubleConsts.MIN_EXPONENT-
 285                                                         FloatConsts.MIN_EXPONENT));
 286             return s.replaceFirst("p-1022$", "p-126");
 287         }
 288         else // double string will be the same as float string
 289             return Double.toHexString(f);
 290     }
 291 
 292     /**
 293      * Returns a {@code Float} object holding the
 294      * {@code float} value represented by the argument string
 295      * {@code s}.
 296      *
 297      * <p>If {@code s} is {@code null}, then a
 298      * {@code NullPointerException} is thrown.
 299      *
 300      * <p>Leading and trailing whitespace characters in {@code s}
 301      * are ignored.  Whitespace is removed as if by the {@link
 302      * String#trim} method; that is, both ASCII space and control




   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 sun.misc.FloatingDecimal;

  29 import sun.misc.FloatConsts;
  30 import sun.misc.DoubleConsts;
  31 
  32 /**
  33  * The {@code Float} class wraps a value of primitive type
  34  * {@code float} in an object. An object of type
  35  * {@code Float} contains a single field whose type is
  36  * {@code float}.
  37  *
  38  * <p>In addition, this class provides several methods for converting a
  39  * {@code float} to a {@code String} and a
  40  * {@code String} to a {@code float}, as well as other
  41  * constants and methods useful when dealing with a
  42  * {@code float}.
  43  *
  44  * @author  Lee Boynton
  45  * @author  Arthur van Hoff
  46  * @author  Joseph D. Darcy
  47  * @since JDK1.0
  48  */


 261      * <tr><td>{@code Float.MAX_VALUE}</td>
 262      *     <td>{@code 0x1.fffffep127}</td>
 263      * <tr><td>{@code Minimum Normal Value}</td>
 264      *     <td>{@code 0x1.0p-126}</td>
 265      * <tr><td>{@code Maximum Subnormal Value}</td>
 266      *     <td>{@code 0x0.fffffep-126}</td>
 267      * <tr><td>{@code Float.MIN_VALUE}</td>
 268      *     <td>{@code 0x0.000002p-126}</td>
 269      * </table>
 270      * @param   f   the {@code float} to be converted.
 271      * @return a hex string representation of the argument.
 272      * @since 1.5
 273      * @author Joseph D. Darcy
 274      */
 275     public static String toHexString(float f) {
 276         if (Math.abs(f) < FloatConsts.MIN_NORMAL
 277             &&  f != 0.0f ) {// float subnormal
 278             // Adjust exponent to create subnormal double, then
 279             // replace subnormal double exponent with subnormal float
 280             // exponent
 281             String s = Double.toHexString(Math.scalb((double)f,
 282                                                      /* -1022+126 */
 283                                                      DoubleConsts.MIN_EXPONENT-
 284                                                      FloatConsts.MIN_EXPONENT));
 285             return s.replaceFirst("p-1022$", "p-126");
 286         }
 287         else // double string will be the same as float string
 288             return Double.toHexString(f);
 289     }
 290 
 291     /**
 292      * Returns a {@code Float} object holding the
 293      * {@code float} value represented by the argument string
 294      * {@code s}.
 295      *
 296      * <p>If {@code s} is {@code null}, then a
 297      * {@code NullPointerException} is thrown.
 298      *
 299      * <p>Leading and trailing whitespace characters in {@code s}
 300      * are ignored.  Whitespace is removed as if by the {@link
 301      * String#trim} method; that is, both ASCII space and control