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

Print this page


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


 121     public static final int SIZE = 32;
 122 
 123     /**
 124      * The {@code Class} instance representing the primitive type
 125      * {@code float}.
 126      *
 127      * @since JDK1.1
 128      */
 129     @SuppressWarnings("unchecked")
 130     public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");
 131 
 132     /**
 133      * Returns a string representation of the {@code float}
 134      * argument. All characters mentioned below are ASCII characters.
 135      * <ul>
 136      * <li>If the argument is NaN, the result is the string
 137      * "{@code NaN}".
 138      * <li>Otherwise, the result is a string that represents the sign and
 139      *     magnitude (absolute value) of the argument. If the sign is
 140      *     negative, the first character of the result is
 141      *     '{@code -}' (<code>'&#92;u002D'</code>); if the sign is
 142      *     positive, no sign character appears in the result. As for
 143      *     the magnitude <i>m</i>:
 144      * <ul>
 145      * <li>If <i>m</i> is infinity, it is represented by the characters
 146      *     {@code "Infinity"}; thus, positive infinity produces
 147      *     the result {@code "Infinity"} and negative infinity
 148      *     produces the result {@code "-Infinity"}.
 149      * <li>If <i>m</i> is zero, it is represented by the characters
 150      *     {@code "0.0"}; thus, negative zero produces the result
 151      *     {@code "-0.0"} and positive zero produces the result
 152      *     {@code "0.0"}.
 153      * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
 154      *      less than 10<sup>7</sup>, then it is represented as the
 155      *      integer part of <i>m</i>, in decimal form with no leading
 156      *      zeroes, followed by '{@code .}'
 157      *      (<code>'&#92;u002E'</code>), followed by one or more
 158      *      decimal digits representing the fractional part of
 159      *      <i>m</i>.
 160      * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
 161      *      equal to 10<sup>7</sup>, then it is represented in
 162      *      so-called "computerized scientific notation." Let <i>n</i>
 163      *      be the unique integer such that 10<sup><i>n</i> </sup>&le;
 164      *      <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
 165      *      be the mathematically exact quotient of <i>m</i> and
 166      *      10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10.
 167      *      The magnitude is then represented as the integer part of
 168      *      <i>a</i>, as a single decimal digit, followed by
 169      *      '{@code .}' (<code>'&#92;u002E'</code>), followed by
 170      *      decimal digits representing the fractional part of
 171      *      <i>a</i>, followed by the letter '{@code E}'
 172      *      (<code>'&#92;u0045'</code>), followed by a representation
 173      *      of <i>n</i> as a decimal integer, as produced by the
 174      *      method {@link java.lang.Integer#toString(int)}.
 175      *
 176      * </ul>
 177      * </ul>
 178      * How many digits must be printed for the fractional part of
 179      * <i>m</i> or <i>a</i>? There must be at least one digit
 180      * to represent the fractional part, and beyond that as many, but
 181      * only as many, more digits as are needed to uniquely distinguish
 182      * the argument value from adjacent values of type
 183      * {@code float}. That is, suppose that <i>x</i> is the
 184      * exact mathematical value represented by the decimal
 185      * representation produced by this method for a finite nonzero
 186      * argument <i>f</i>. Then <i>f</i> must be the {@code float}
 187      * value nearest to <i>x</i>; or, if two {@code float} values are
 188      * equally close to <i>x</i>, then <i>f</i> must be one of
 189      * them and the least significant bit of the significand of
 190      * <i>f</i> must be {@code 0}.
 191      *
 192      * <p>To create localized string representations of a floating-point
 193      * value, use subclasses of {@link java.text.NumberFormat}.
 194      *
 195      * @param   f   the float to be converted.
 196      * @return a string representation of the argument.
 197      */
 198     public static String toString(float f) {
 199         return new FloatingDecimal(f).toJavaFormatString();
 200     }
 201 
 202     /**
 203      * Returns a hexadecimal string representation of the
 204      * {@code float} argument. All characters mentioned below are
 205      * ASCII characters.
 206      *
 207      * <ul>
 208      * <li>If the argument is NaN, the result is the string
 209      *     "{@code NaN}".
 210      * <li>Otherwise, the result is a string that represents the sign and
 211      * magnitude (absolute value) of the argument. If the sign is negative,
 212      * the first character of the result is '{@code -}'
 213      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
 214      * appears in the result. As for the magnitude <i>m</i>:
 215      *
 216      * <ul>
 217      * <li>If <i>m</i> is infinity, it is represented by the string
 218      * {@code "Infinity"}; thus, positive infinity produces the
 219      * result {@code "Infinity"} and negative infinity produces
 220      * the result {@code "-Infinity"}.
 221      *
 222      * <li>If <i>m</i> is zero, it is represented by the string
 223      * {@code "0x0.0p0"}; thus, negative zero produces the result
 224      * {@code "-0x0.0p0"} and positive zero produces the result
 225      * {@code "0x0.0p0"}.
 226      *
 227      * <li>If <i>m</i> is a {@code float} value with a
 228      * normalized representation, substrings are used to represent the
 229      * significand and exponent fields.  The significand is
 230      * represented by the characters {@code "0x1."}
 231      * followed by a lowercase hexadecimal representation of the rest
 232      * of the significand as a fraction.  Trailing zeros in the
 233      * hexadecimal representation are removed unless all the digits


   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


 121     public static final int SIZE = 32;
 122 
 123     /**
 124      * The {@code Class} instance representing the primitive type
 125      * {@code float}.
 126      *
 127      * @since JDK1.1
 128      */
 129     @SuppressWarnings("unchecked")
 130     public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");
 131 
 132     /**
 133      * Returns a string representation of the {@code float}
 134      * argument. All characters mentioned below are ASCII characters.
 135      * <ul>
 136      * <li>If the argument is NaN, the result is the string
 137      * "{@code NaN}".
 138      * <li>Otherwise, the result is a string that represents the sign and
 139      *     magnitude (absolute value) of the argument. If the sign is
 140      *     negative, the first character of the result is
 141      *     '{@code -}' ({@code '\u005Cu002D'}); if the sign is
 142      *     positive, no sign character appears in the result. As for
 143      *     the magnitude <i>m</i>:
 144      * <ul>
 145      * <li>If <i>m</i> is infinity, it is represented by the characters
 146      *     {@code "Infinity"}; thus, positive infinity produces
 147      *     the result {@code "Infinity"} and negative infinity
 148      *     produces the result {@code "-Infinity"}.
 149      * <li>If <i>m</i> is zero, it is represented by the characters
 150      *     {@code "0.0"}; thus, negative zero produces the result
 151      *     {@code "-0.0"} and positive zero produces the result
 152      *     {@code "0.0"}.
 153      * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
 154      *      less than 10<sup>7</sup>, then it is represented as the
 155      *      integer part of <i>m</i>, in decimal form with no leading
 156      *      zeroes, followed by '{@code .}'
 157      *      ({@code '\u005Cu002E'}), followed by one or more
 158      *      decimal digits representing the fractional part of
 159      *      <i>m</i>.
 160      * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
 161      *      equal to 10<sup>7</sup>, then it is represented in
 162      *      so-called "computerized scientific notation." Let <i>n</i>
 163      *      be the unique integer such that 10<sup><i>n</i> </sup>&le;
 164      *      <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
 165      *      be the mathematically exact quotient of <i>m</i> and
 166      *      10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10.
 167      *      The magnitude is then represented as the integer part of
 168      *      <i>a</i>, as a single decimal digit, followed by
 169      *      '{@code .}' ({@code '\u005Cu002E'}), followed by
 170      *      decimal digits representing the fractional part of
 171      *      <i>a</i>, followed by the letter '{@code E}'
 172      *      ({@code '\u005Cu0045'}), followed by a representation
 173      *      of <i>n</i> as a decimal integer, as produced by the
 174      *      method {@link java.lang.Integer#toString(int)}.
 175      *
 176      * </ul>
 177      * </ul>
 178      * How many digits must be printed for the fractional part of
 179      * <i>m</i> or <i>a</i>? There must be at least one digit
 180      * to represent the fractional part, and beyond that as many, but
 181      * only as many, more digits as are needed to uniquely distinguish
 182      * the argument value from adjacent values of type
 183      * {@code float}. That is, suppose that <i>x</i> is the
 184      * exact mathematical value represented by the decimal
 185      * representation produced by this method for a finite nonzero
 186      * argument <i>f</i>. Then <i>f</i> must be the {@code float}
 187      * value nearest to <i>x</i>; or, if two {@code float} values are
 188      * equally close to <i>x</i>, then <i>f</i> must be one of
 189      * them and the least significant bit of the significand of
 190      * <i>f</i> must be {@code 0}.
 191      *
 192      * <p>To create localized string representations of a floating-point
 193      * value, use subclasses of {@link java.text.NumberFormat}.
 194      *
 195      * @param   f   the float to be converted.
 196      * @return a string representation of the argument.
 197      */
 198     public static String toString(float f) {
 199         return new FloatingDecimal(f).toJavaFormatString();
 200     }
 201 
 202     /**
 203      * Returns a hexadecimal string representation of the
 204      * {@code float} argument. All characters mentioned below are
 205      * ASCII characters.
 206      *
 207      * <ul>
 208      * <li>If the argument is NaN, the result is the string
 209      *     "{@code NaN}".
 210      * <li>Otherwise, the result is a string that represents the sign and
 211      * magnitude (absolute value) of the argument. If the sign is negative,
 212      * the first character of the result is '{@code -}'
 213      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 214      * appears in the result. As for the magnitude <i>m</i>:
 215      *
 216      * <ul>
 217      * <li>If <i>m</i> is infinity, it is represented by the string
 218      * {@code "Infinity"}; thus, positive infinity produces the
 219      * result {@code "Infinity"} and negative infinity produces
 220      * the result {@code "-Infinity"}.
 221      *
 222      * <li>If <i>m</i> is zero, it is represented by the string
 223      * {@code "0x0.0p0"}; thus, negative zero produces the result
 224      * {@code "-0x0.0p0"} and positive zero produces the result
 225      * {@code "0x0.0p0"}.
 226      *
 227      * <li>If <i>m</i> is a {@code float} value with a
 228      * normalized representation, substrings are used to represent the
 229      * significand and exponent fields.  The significand is
 230      * represented by the characters {@code "0x1."}
 231      * followed by a lowercase hexadecimal representation of the rest
 232      * of the significand as a fraction.  Trailing zeros in the
 233      * hexadecimal representation are removed unless all the digits