|
31 | 31 | import java.util.Optional; |
32 | 32 |
|
33 | 33 | import jdk.internal.math.FloatingDecimal; |
| 34 | +import jdk.internal.math.FloatToDecimal; |
34 | 35 | import jdk.internal.vm.annotation.IntrinsicCandidate; |
35 | 36 |
|
36 | 37 | /** |
@@ -189,53 +190,119 @@ public final class Float extends Number |
189 | 190 | * {@code "0.0"}; thus, negative zero produces the result |
190 | 191 | * {@code "-0.0"} and positive zero produces the result |
191 | 192 | * {@code "0.0"}. |
192 | | - * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but |
193 | | - * less than 10<sup>7</sup>, then it is represented as the |
194 | | - * integer part of <i>m</i>, in decimal form with no leading |
195 | | - * zeroes, followed by '{@code .}' |
196 | | - * ({@code '\u005Cu002E'}), followed by one or more |
197 | | - * decimal digits representing the fractional part of |
198 | | - * <i>m</i>. |
199 | | - * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or |
200 | | - * equal to 10<sup>7</sup>, then it is represented in |
201 | | - * so-called "computerized scientific notation." Let <i>n</i> |
202 | | - * be the unique integer such that 10<sup><i>n</i> </sup>≤ |
203 | | - * <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i> |
204 | | - * be the mathematically exact quotient of <i>m</i> and |
205 | | - * 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. |
206 | | - * The magnitude is then represented as the integer part of |
207 | | - * <i>a</i>, as a single decimal digit, followed by |
208 | | - * '{@code .}' ({@code '\u005Cu002E'}), followed by |
209 | | - * decimal digits representing the fractional part of |
210 | | - * <i>a</i>, followed by the letter '{@code E}' |
211 | | - * ({@code '\u005Cu0045'}), followed by a representation |
212 | | - * of <i>n</i> as a decimal integer, as produced by the |
213 | | - * method {@link java.lang.Integer#toString(int)}. |
214 | 193 | * |
| 194 | + * <li> Otherwise <i>m</i> is positive and finite. |
| 195 | + * It is converted to a string in two stages: |
| 196 | + * <ul> |
| 197 | + * <li> <em>Selection of a decimal</em>: |
| 198 | + * A well-defined decimal <i>d</i><sub><i>m</i></sub> |
| 199 | + * is selected to represent <i>m</i>. |
| 200 | + * This decimal is (almost always) the <em>shortest</em> one that |
| 201 | + * rounds to <i>m</i> according to the round to nearest |
| 202 | + * rounding policy of IEEE 754 floating-point arithmetic. |
| 203 | + * <li> <em>Formatting as a string</em>: |
| 204 | + * The decimal <i>d</i><sub><i>m</i></sub> is formatted as a string, |
| 205 | + * either in plain or in computerized scientific notation, |
| 206 | + * depending on its value. |
| 207 | + * </ul> |
| 208 | + * </ul> |
| 209 | + * </ul> |
| 210 | + * |
| 211 | + * <p>A <em>decimal</em> is a number of the form |
| 212 | + * <i>s</i>×10<sup><i>i</i></sup> |
| 213 | + * for some (unique) integers <i>s</i> > 0 and <i>i</i> such that |
| 214 | + * <i>s</i> is not a multiple of 10. |
| 215 | + * These integers are the <em>significand</em> and |
| 216 | + * the <em>exponent</em>, respectively, of the decimal. |
| 217 | + * The <em>length</em> of the decimal is the (unique) |
| 218 | + * positive integer <i>n</i> meeting |
| 219 | + * 10<sup><i>n</i>-1</sup> ≤ <i>s</i> < 10<sup><i>n</i></sup>. |
| 220 | + * |
| 221 | + * <p>The decimal <i>d</i><sub><i>m</i></sub> for a finite positive <i>m</i> |
| 222 | + * is defined as follows: |
| 223 | + * <ul> |
| 224 | + * <li>Let <i>R</i> be the set of all decimals that round to <i>m</i> |
| 225 | + * according to the usual <em>round to nearest</em> rounding policy of |
| 226 | + * IEEE 754 floating-point arithmetic. |
| 227 | + * <li>Let <i>p</i> be the minimal length over all decimals in <i>R</i>. |
| 228 | + * <li>When <i>p</i> ≥ 2, let <i>T</i> be the set of all decimals |
| 229 | + * in <i>R</i> with length <i>p</i>. |
| 230 | + * Otherwise, let <i>T</i> be the set of all decimals |
| 231 | + * in <i>R</i> with length 1 or 2. |
| 232 | + * <li>Define <i>d</i><sub><i>m</i></sub> as the decimal in <i>T</i> |
| 233 | + * that is closest to <i>m</i>. |
| 234 | + * Or if there are two such decimals in <i>T</i>, |
| 235 | + * select the one with the even significand. |
| 236 | + * </ul> |
| 237 | + * |
| 238 | + * <p>The (uniquely) selected decimal <i>d</i><sub><i>m</i></sub> |
| 239 | + * is then formatted. |
| 240 | + * Let <i>s</i>, <i>i</i> and <i>n</i> be the significand, exponent and |
| 241 | + * length of <i>d</i><sub><i>m</i></sub>, respectively. |
| 242 | + * Further, let <i>e</i> = <i>n</i> + <i>i</i> - 1 and let |
| 243 | + * <i>s</i><sub>1</sub>…<i>s</i><sub><i>n</i></sub> |
| 244 | + * be the usual decimal expansion of <i>s</i>. |
| 245 | + * Note that <i>s</i><sub>1</sub> ≠ 0 |
| 246 | + * and <i>s</i><sub><i>n</i></sub> ≠ 0. |
| 247 | + * Below, the decimal point {@code '.'} is {@code '\u005Cu002E'} |
| 248 | + * and the exponent indicator {@code 'E'} is {@code '\u005Cu0045'}. |
| 249 | + * <ul> |
| 250 | + * <li>Case -3 ≤ <i>e</i> < 0: |
| 251 | + * <i>d</i><sub><i>m</i></sub> is formatted as |
| 252 | + * <code>0.0</code>…<code>0</code><!-- |
| 253 | + * --><i>s</i><sub>1</sub>…<i>s</i><sub><i>n</i></sub>, |
| 254 | + * where there are exactly -(<i>n</i> + <i>i</i>) zeroes between |
| 255 | + * the decimal point and <i>s</i><sub>1</sub>. |
| 256 | + * For example, 123 × 10<sup>-4</sup> is formatted as |
| 257 | + * {@code 0.0123}. |
| 258 | + * <li>Case 0 ≤ <i>e</i> < 7: |
| 259 | + * <ul> |
| 260 | + * <li>Subcase <i>i</i> ≥ 0: |
| 261 | + * <i>d</i><sub><i>m</i></sub> is formatted as |
| 262 | + * <i>s</i><sub>1</sub>…<i>s</i><sub><i>n</i></sub><!-- |
| 263 | + * --><code>0</code>…<code>0.0</code>, |
| 264 | + * where there are exactly <i>i</i> zeroes |
| 265 | + * between <i>s</i><sub><i>n</i></sub> and the decimal point. |
| 266 | + * For example, 123 × 10<sup>2</sup> is formatted as |
| 267 | + * {@code 12300.0}. |
| 268 | + * <li>Subcase <i>i</i> < 0: |
| 269 | + * <i>d</i><sub><i>m</i></sub> is formatted as |
| 270 | + * <i>s</i><sub>1</sub>…<!-- |
| 271 | + * --><i>s</i><sub><i>n</i>+<i>i</i></sub><code>.</code><!-- |
| 272 | + * --><i>s</i><sub><i>n</i>+<i>i</i>+1</sub>…<!-- |
| 273 | + * --><i>s</i><sub><i>n</i></sub>, |
| 274 | + * where there are exactly -<i>i</i> digits to the right of |
| 275 | + * the decimal point. |
| 276 | + * For example, 123 × 10<sup>-1</sup> is formatted as |
| 277 | + * {@code 12.3}. |
| 278 | + * </ul> |
| 279 | + * <li>Case <i>e</i> < -3 or <i>e</i> ≥ 7: |
| 280 | + * computerized scientific notation is used to format |
| 281 | + * <i>d</i><sub><i>m</i></sub>. |
| 282 | + * Here <i>e</i> is formatted as by {@link Integer#toString(int)}. |
| 283 | + * <ul> |
| 284 | + * <li>Subcase <i>n</i> = 1: |
| 285 | + * <i>d</i><sub><i>m</i></sub> is formatted as |
| 286 | + * <i>s</i><sub>1</sub><code>.0E</code><i>e</i>. |
| 287 | + * For example, 1 × 10<sup>23</sup> is formatted as |
| 288 | + * {@code 1.0E23}. |
| 289 | + * <li>Subcase <i>n</i> > 1: |
| 290 | + * <i>d</i><sub><i>m</i></sub> is formatted as |
| 291 | + * <i>s</i><sub>1</sub><code>.</code><i>s</i><sub>2</sub><!-- |
| 292 | + * -->…<i>s</i><sub><i>n</i></sub><code>E</code><i>e</i>. |
| 293 | + * For example, 123 × 10<sup>-21</sup> is formatted as |
| 294 | + * {@code 1.23E-19}. |
215 | 295 | * </ul> |
216 | 296 | * </ul> |
217 | | - * How many digits must be printed for the fractional part of |
218 | | - * <i>m</i> or <i>a</i>? There must be at least one digit |
219 | | - * to represent the fractional part, and beyond that as many, but |
220 | | - * only as many, more digits as are needed to uniquely distinguish |
221 | | - * the argument value from adjacent values of type |
222 | | - * {@code float}. That is, suppose that <i>x</i> is the |
223 | | - * exact mathematical value represented by the decimal |
224 | | - * representation produced by this method for a finite nonzero |
225 | | - * argument <i>f</i>. Then <i>f</i> must be the {@code float} |
226 | | - * value nearest to <i>x</i>; or, if two {@code float} values are |
227 | | - * equally close to <i>x</i>, then <i>f</i> must be one of |
228 | | - * them and the least significant bit of the significand of |
229 | | - * <i>f</i> must be {@code 0}. |
230 | 297 | * |
231 | 298 | * <p>To create localized string representations of a floating-point |
232 | 299 | * value, use subclasses of {@link java.text.NumberFormat}. |
233 | 300 | * |
234 | | - * @param f the float to be converted. |
| 301 | + * @param f the {@code float} to be converted. |
235 | 302 | * @return a string representation of the argument. |
236 | 303 | */ |
237 | 304 | public static String toString(float f) { |
238 | | - return FloatingDecimal.toJavaFormatString(f); |
| 305 | + return FloatToDecimal.toString(f); |
239 | 306 | } |
240 | 307 |
|
241 | 308 | /** |
|
0 commit comments