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