1. /*
  2. * @(#)Float.java 1.94 04/05/11
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import sun.misc.FloatingDecimal;
  9. import sun.misc.FpUtils;
  10. import sun.misc.FloatConsts;
  11. import sun.misc.DoubleConsts;
  12. /**
  13. * The <code>Float</code> class wraps a value of primitive type
  14. * <code>float</code> in an object. An object of type
  15. * <code>Float</code> contains a single field whose type is
  16. * <code>float</code>.
  17. * <p>
  18. * In addition, this class provides several methods for converting a
  19. * <code>float</code> to a <code>String</code> and a
  20. * <code>String</code> to a <code>float</code>, as well as other
  21. * constants and methods useful when dealing with a
  22. * <code>float</code>.
  23. *
  24. * @author Lee Boynton
  25. * @author Arthur van Hoff
  26. * @author Joseph D. Darcy
  27. * @version 1.94, 05/11/04
  28. * @since JDK1.0
  29. */
  30. public final class Float extends Number implements Comparable<Float> {
  31. /**
  32. * A constant holding the positive infinity of type
  33. * <code>float</code>. It is equal to the value returned by
  34. * <code>Float.intBitsToFloat(0x7f800000)</code>.
  35. */
  36. public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  37. /**
  38. * A constant holding the negative infinity of type
  39. * <code>float</code>. It is equal to the value returned by
  40. * <code>Float.intBitsToFloat(0xff800000)</code>.
  41. */
  42. public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
  43. /**
  44. * A constant holding a Not-a-Number (NaN) value of type
  45. * <code>float</code>. It is equivalent to the value returned by
  46. * <code>Float.intBitsToFloat(0x7fc00000)</code>.
  47. */
  48. public static final float NaN = 0.0f / 0.0f;
  49. /**
  50. * A constant holding the largest positive finite value of type
  51. * <code>float</code>, (2-2<sup>-23</sup>)·2<sup>127</sup>.
  52. * It is equal to the hexadecimal floating-point literal
  53. * <code>0x1.fffffeP+127f</code> and also equal to
  54. * <code>Float.intBitsToFloat(0x7f7fffff)</code>.
  55. */
  56. public static final float MAX_VALUE = 3.4028235e+38f; // 0x1.fffffeP+127f
  57. /**
  58. * A constant holding the smallest positive nonzero value of type
  59. * <code>float</code>, 2<sup>-149</sup>. It is equal to the
  60. * hexadecimal floating-point literal <code>0x0.000002P-126f</code>
  61. * and also equal to <code>Float.intBitsToFloat(0x1)</code>.
  62. */
  63. public static final float MIN_VALUE = 1.4e-45f; // 0x0.000002P-126f
  64. /**
  65. * The number of bits used to represent a <tt>float</tt> value.
  66. *
  67. * @since 1.5
  68. */
  69. public static final int SIZE = 32;
  70. /**
  71. * The <code>Class</code> instance representing the primitive type
  72. * <code>float</code>.
  73. *
  74. * @since JDK1.1
  75. */
  76. public static final Class<Float> TYPE = Class.getPrimitiveClass("float");
  77. /**
  78. * Returns a string representation of the <code>float</code>
  79. * argument. All characters mentioned below are ASCII characters.
  80. * <ul>
  81. * <li>If the argument is NaN, the result is the string
  82. * "<code>NaN</code>".
  83. * <li>Otherwise, the result is a string that represents the sign and
  84. * magnitude (absolute value) of the argument. If the sign is
  85. * negative, the first character of the result is
  86. * '<code>-</code>' (<code>'\u002D'</code>); if the sign is
  87. * positive, no sign character appears in the result. As for
  88. * the magnitude <i>m</i>:
  89. * <ul>
  90. * <li>If <i>m</i> is infinity, it is represented by the characters
  91. * <code>"Infinity"</code> thus, positive infinity produces
  92. * the result <code>"Infinity"</code> and negative infinity
  93. * produces the result <code>"-Infinity"</code>.
  94. * <li>If <i>m</i> is zero, it is represented by the characters
  95. * <code>"0.0"</code> thus, negative zero produces the result
  96. * <code>"-0.0"</code> and positive zero produces the result
  97. * <code>"0.0"</code>.
  98. * <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
  99. * less than 10<sup>7</sup>, then it is represented as the
  100. * integer part of <i>m</i>, in decimal form with no leading
  101. * zeroes, followed by '<code>.</code>'
  102. * (<code>'\u002E'</code>), followed by one or more
  103. * decimal digits representing the fractional part of
  104. * <i>m</i>.
  105. * <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
  106. * equal to 10<sup>7</sup>, then it is represented in
  107. * so-called "computerized scientific notation." Let <i>n</i>
  108. * be the unique integer such that 10<sup><i>n</i> </sup><=
  109. * <i>m</i> < 10<sup><i>n</i>+1</sup> then let <i>a</i>
  110. * be the mathematically exact quotient of <i>m</i> and
  111. * 10<sup><i>n</i></sup> so that 1 <= <i>a</i> < 10.
  112. * The magnitude is then represented as the integer part of
  113. * <i>a</i>, as a single decimal digit, followed by
  114. * '<code>.</code>' (<code>'\u002E'</code>), followed by
  115. * decimal digits representing the fractional part of
  116. * <i>a</i>, followed by the letter '<code>E</code>'
  117. * (<code>'\u0045'</code>), followed by a representation
  118. * of <i>n</i> as a decimal integer, as produced by the
  119. * method <code>{@link
  120. * java.lang.Integer#toString(int)}</code>.
  121. * </ul>
  122. * </ul>
  123. * How many digits must be printed for the fractional part of
  124. * <i>m</i> or <i>a</i>? There must be at least one digit
  125. * to represent the fractional part, and beyond that as many, but
  126. * only as many, more digits as are needed to uniquely distinguish
  127. * the argument value from adjacent values of type
  128. * <code>float</code>. That is, suppose that <i>x</i> is the
  129. * exact mathematical value represented by the decimal
  130. * representation produced by this method for a finite nonzero
  131. * argument <i>f</i>. Then <i>f</i> must be the <code>float</code>
  132. * value nearest to <i>x</i> or, if two <code>float</code> values are
  133. * equally close to <i>x</i>, then <i>f</i> must be one of
  134. * them and the least significant bit of the significand of
  135. * <i>f</i> must be <code>0</code>.
  136. * <p>
  137. * To create localized string representations of a floating-point
  138. * value, use subclasses of {@link java.text.NumberFormat}.
  139. *
  140. * @param f the float to be converted.
  141. * @return a string representation of the argument.
  142. */
  143. public static String toString(float f) {
  144. return new FloatingDecimal(f).toJavaFormatString();
  145. }
  146. /**
  147. * Returns a hexadecimal string representation of the
  148. * <code>float</code> argument. All characters mentioned below are
  149. * ASCII characters.
  150. *
  151. * <ul>
  152. * <li>If the argument is NaN, the result is the string
  153. * "<code>NaN</code>".
  154. * <li>Otherwise, the result is a string that represents the sign and
  155. * magnitude (absolute value) of the argument. If the sign is negative,
  156. * the first character of the result is '<code>-</code>'
  157. * (<code>'\u002D'</code>); if the sign is positive, no sign character
  158. * appears in the result. As for the magnitude <i>m</i>:
  159. *
  160. * <ul>
  161. * <li>If <i>m</i> is infinity, it is represented by the string
  162. * <code>"Infinity"</code> thus, positive infinity produces the
  163. * result <code>"Infinity"</code> and negative infinity produces
  164. * the result <code>"-Infinity"</code>.
  165. *
  166. * <li>If <i>m</i> is zero, it is represented by the string
  167. * <code>"0x0.0p0"</code> thus, negative zero produces the result
  168. * <code>"-0x0.0p0"</code> and positive zero produces the result
  169. * <code>"0x0.0p0"</code>.
  170. *
  171. * <li>If <i>m</i> is a <code>float</code> value with a
  172. * normalized representation, substrings are used to represent the
  173. * significand and exponent fields. The significand is
  174. * represented by the characters <code>"0x1."</code>
  175. * followed by a lowercase hexadecimal representation of the rest
  176. * of the significand as a fraction. Trailing zeros in the
  177. * hexadecimal representation are removed unless all the digits
  178. * are zero, in which case a single zero is used. Next, the
  179. * exponent is represented by <code>"p"</code> followed
  180. * by a decimal string of the unbiased exponent as if produced by
  181. * a call to {@link Integer#toString(int) Integer.toString} on the
  182. * exponent value.
  183. *
  184. * <li>If <i>m</i> is a <code>float</code> value with a subnormal
  185. * representation, the significand is represented by the
  186. * characters <code>"0x0."</code> followed by a
  187. * hexadecimal representation of the rest of the significand as a
  188. * fraction. Trailing zeros in the hexadecimal representation are
  189. * removed. Next, the exponent is represented by
  190. * <code>"p-126"</code>. Note that there must be at
  191. * least one nonzero digit in a subnormal significand.
  192. *
  193. * </ul>
  194. *
  195. * </ul>
  196. *
  197. * <table border>
  198. * <caption><h3>Examples</h3></caption>
  199. * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
  200. * <tr><td><code>1.0</code></td> <td><code>0x1.0p0</code></td>
  201. * <tr><td><code>-1.0</code></td> <td><code>-0x1.0p0</code></td>
  202. * <tr><td><code>2.0</code></td> <td><code>0x1.0p1</code></td>
  203. * <tr><td><code>3.0</code></td> <td><code>0x1.8p1</code></td>
  204. * <tr><td><code>0.5</code></td> <td><code>0x1.0p-1</code></td>
  205. * <tr><td><code>0.25</code></td> <td><code>0x1.0p-2</code></td>
  206. * <tr><td><code>Float.MAX_VALUE</code></td>
  207. * <td><code>0x1.fffffep127</code></td>
  208. * <tr><td><code>Minimum Normal Value</code></td>
  209. * <td><code>0x1.0p-126</code></td>
  210. * <tr><td><code>Maximum Subnormal Value</code></td>
  211. * <td><code>0x0.fffffep-126</code></td>
  212. * <tr><td><code>Float.MIN_VALUE</code></td>
  213. * <td><code>0x0.000002p-126</code></td>
  214. * </table>
  215. * @param f the <code>float</code> to be converted.
  216. * @return a hex string representation of the argument.
  217. * @since 1.5
  218. * @author Joseph D. Darcy
  219. */
  220. public static String toHexString(float f) {
  221. if (Math.abs(f) < FloatConsts.MIN_NORMAL
  222. && f != 0.0f ) {// float subnormal
  223. // Adjust exponent to create subnormal double, then
  224. // replace subnormal double exponent with subnormal float
  225. // exponent
  226. String s = Double.toHexString(FpUtils.scalb((double)f,
  227. /* -1022+126 */
  228. DoubleConsts.MIN_EXPONENT-
  229. FloatConsts.MIN_EXPONENT));
  230. return s.replaceFirst("p-1022$", "p-126");
  231. }
  232. else // double string will be the same as float string
  233. return Double.toHexString(f);
  234. }
  235. /**
  236. * Returns a <code>Float</code> object holding the
  237. * <code>float</code> value represented by the argument string
  238. * <code>s</code>.
  239. *
  240. * <p>If <code>s</code> is <code>null</code>, then a
  241. * <code>NullPointerException</code> is thrown.
  242. *
  243. * <p>Leading and trailing whitespace characters in <code>s</code>
  244. * are ignored. Whitespace is removed as if by the {@link
  245. * String#trim} method; that is, both ASCII space and control
  246. * characters are removed. The rest of <code>s</code> should
  247. * constitute a <i>FloatValue</i> as described by the lexical
  248. * syntax rules:
  249. *
  250. * <blockquote>
  251. * <dl>
  252. * <dt><i>FloatValue:</i>
  253. * <dd><i>Sign<sub>opt</sub></i> <code>NaN</code>
  254. * <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code>
  255. * <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i>
  256. * <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i>
  257. * <dd><i>SignedInteger</i>
  258. * </dl>
  259. *
  260. * <p>
  261. *
  262. * <dl>
  263. * <dt><i>HexFloatingPointLiteral</i>:
  264. * <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i>
  265. * </dl>
  266. *
  267. * <p>
  268. *
  269. * <dl>
  270. * <dt><i>HexSignificand:</i>
  271. * <dd><i>HexNumeral</i>
  272. * <dd><i>HexNumeral</i> <code>.</code>
  273. * <dd><code>0x</code> <i>HexDigits<sub>opt</sub>
  274. * </i><code>.</code><i> HexDigits</i>
  275. * <dd><code>0X</code><i> HexDigits<sub>opt</sub>
  276. * </i><code>.</code> <i>HexDigits</i>
  277. * </dl>
  278. *
  279. * <p>
  280. *
  281. * <dl>
  282. * <dt><i>BinaryExponent:</i>
  283. * <dd><i>BinaryExponentIndicator SignedInteger</i>
  284. * </dl>
  285. *
  286. * <p>
  287. *
  288. * <dl>
  289. * <dt><i>BinaryExponentIndicator:</i>
  290. * <dd><code>p</code>
  291. * <dd><code>P</code>
  292. * </dl>
  293. *
  294. * </blockquote>
  295. *
  296. * where <i>Sign</i>, <i>FloatingPointLiteral</i>,
  297. * <i>HexNumeral</i>, <i>HexDigits</i>, <i>SignedInteger</i> and
  298. * <i>FloatTypeSuffix</i> are as defined in the lexical structure
  299. * sections of the of the <a
  300. * href="http://java.sun.com/docs/books/jls/html/">Java Language
  301. * Specification</a>. If <code>s</code> does not have the form of
  302. * a <i>FloatValue</i>, then a <code>NumberFormatException</code>
  303. * is thrown. Otherwise, <code>s</code> is regarded as
  304. * representing an exact decimal value in the usual
  305. * "computerized scientific notation" or as an exact
  306. * hexadecimal value; this exact numerical value is then
  307. * conceptually converted to an "infinitely precise"
  308. * binary value that is then rounded to type <code>float</code>
  309. * by the usual round-to-nearest rule of IEEE 754 floating-point
  310. * arithmetic, which includes preserving the sign of a zero
  311. * value. Finally, a <code>Float</code> object representing this
  312. * <code>float</code> value is returned.
  313. *
  314. * <p>To interpret localized string representations of a
  315. * floating-point value, use subclasses of {@link
  316. * java.text.NumberFormat}.
  317. *
  318. * <p>Note that trailing format specifiers, specifiers that
  319. * determine the type of a floating-point literal
  320. * (<code>1.0f</code> is a <code>float</code> value;
  321. * <code>1.0d</code> is a <code>double</code> value), do
  322. * <em>not</em> influence the results of this method. In other
  323. * words, the numerical value of the input string is converted
  324. * directly to the target floating-point type. In general, the
  325. * two-step sequence of conversions, string to <code>double</code>
  326. * followed by <code>double</code> to <code>float</code>, is
  327. * <em>not</em> equivalent to converting a string directly to
  328. * <code>float</code>. For example, if first converted to an
  329. * intermediate <code>double</code> and then to
  330. * <code>float</code>, the string<br>
  331. * <code>"1.00000017881393421514957253748434595763683319091796875001d"</code><br>
  332. * results in the <code>float</code> value
  333. * <code>1.0000002f</code> if the string is converted directly to
  334. * <code>float</code>, <code>1.000000<b>1</b>f</code> results.
  335. *
  336. * <p>To avoid calling this method on a invalid string and having
  337. * a <code>NumberFormatException</code> be thrown, the documentation
  338. * for {@link Double#valueOf Double.valueOf} lists a regular
  339. * expression which can be used to screen the input.
  340. *
  341. * @param s the string to be parsed.
  342. * @return a <code>Float</code> object holding the value
  343. * represented by the <code>String</code> argument.
  344. * @exception NumberFormatException if the string does not contain a
  345. * parsable number.
  346. */
  347. public static Float valueOf(String s) throws NumberFormatException {
  348. return new Float(FloatingDecimal.readJavaFormatString(s).floatValue());
  349. }
  350. /**
  351. * Returns a <tt>Float</tt> instance representing the specified
  352. * <tt>float</tt> value.
  353. * If a new <tt>Float</tt> instance is not required, this method
  354. * should generally be used in preference to the constructor
  355. * {@link #Float(float)}, as this method is likely to yield
  356. * significantly better space and time performance by caching
  357. * frequently requested values.
  358. *
  359. * @param f a float value.
  360. * @return a <tt>Float</tt> instance representing <tt>f</tt>.
  361. * @since 1.5
  362. */
  363. public static Float valueOf(float f) {
  364. return new Float(f);
  365. }
  366. /**
  367. * Returns a new <code>float</code> initialized to the value
  368. * represented by the specified <code>String</code>, as performed
  369. * by the <code>valueOf</code> method of class <code>Float</code>.
  370. *
  371. * @param s the string to be parsed.
  372. * @return the <code>float</code> value represented by the string
  373. * argument.
  374. * @exception NumberFormatException if the string does not contain a
  375. * parsable <code>float</code>.
  376. * @see java.lang.Float#valueOf(String)
  377. * @since 1.2
  378. */
  379. public static float parseFloat(String s) throws NumberFormatException {
  380. return FloatingDecimal.readJavaFormatString(s).floatValue();
  381. }
  382. /**
  383. * Returns <code>true</code> if the specified number is a
  384. * Not-a-Number (NaN) value, <code>false</code> otherwise.
  385. *
  386. * @param v the value to be tested.
  387. * @return <code>true</code> if the argument is NaN;
  388. * <code>false</code> otherwise.
  389. */
  390. static public boolean isNaN(float v) {
  391. return (v != v);
  392. }
  393. /**
  394. * Returns <code>true</code> if the specified number is infinitely
  395. * large in magnitude, <code>false</code> otherwise.
  396. *
  397. * @param v the value to be tested.
  398. * @return <code>true</code> if the argument is positive infinity or
  399. * negative infinity; <code>false</code> otherwise.
  400. */
  401. static public boolean isInfinite(float v) {
  402. return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  403. }
  404. /**
  405. * The value of the Float.
  406. *
  407. * @serial
  408. */
  409. private final float value;
  410. /**
  411. * Constructs a newly allocated <code>Float</code> object that
  412. * represents the primitive <code>float</code> argument.
  413. *
  414. * @param value the value to be represented by the <code>Float</code>.
  415. */
  416. public Float(float value) {
  417. this.value = value;
  418. }
  419. /**
  420. * Constructs a newly allocated <code>Float</code> object that
  421. * represents the argument converted to type <code>float</code>.
  422. *
  423. * @param value the value to be represented by the <code>Float</code>.
  424. */
  425. public Float(double value) {
  426. this.value = (float)value;
  427. }
  428. /**
  429. * Constructs a newly allocated <code>Float</code> object that
  430. * represents the floating-point value of type <code>float</code>
  431. * represented by the string. The string is converted to a
  432. * <code>float</code> value as if by the <code>valueOf</code> method.
  433. *
  434. * @param s a string to be converted to a <code>Float</code>.
  435. * @exception NumberFormatException if the string does not contain a
  436. * parsable number.
  437. * @see java.lang.Float#valueOf(java.lang.String)
  438. */
  439. public Float(String s) throws NumberFormatException {
  440. // REMIND: this is inefficient
  441. this(valueOf(s).floatValue());
  442. }
  443. /**
  444. * Returns <code>true</code> if this <code>Float</code> value is a
  445. * Not-a-Number (NaN), <code>false</code> otherwise.
  446. *
  447. * @return <code>true</code> if the value represented by this object is
  448. * NaN; <code>false</code> otherwise.
  449. */
  450. public boolean isNaN() {
  451. return isNaN(value);
  452. }
  453. /**
  454. * Returns <code>true</code> if this <code>Float</code> value is
  455. * infinitely large in magnitude, <code>false</code> otherwise.
  456. *
  457. * @return <code>true</code> if the value represented by this object is
  458. * positive infinity or negative infinity;
  459. * <code>false</code> otherwise.
  460. */
  461. public boolean isInfinite() {
  462. return isInfinite(value);
  463. }
  464. /**
  465. * Returns a string representation of this <code>Float</code> object.
  466. * The primitive <code>float</code> value represented by this object
  467. * is converted to a <code>String</code> exactly as if by the method
  468. * <code>toString</code> of one argument.
  469. *
  470. * @return a <code>String</code> representation of this object.
  471. * @see java.lang.Float#toString(float)
  472. */
  473. public String toString() {
  474. return String.valueOf(value);
  475. }
  476. /**
  477. * Returns the value of this <code>Float</code> as a
  478. * <code>byte</code> (by casting to a <code>byte</code>).
  479. *
  480. * @return the <code>float</code> value represented by this object
  481. * converted to type <code>byte</code>
  482. */
  483. public byte byteValue() {
  484. return (byte)value;
  485. }
  486. /**
  487. * Returns the value of this <code>Float</code> as a
  488. * <code>short</code> (by casting to a <code>short</code>).
  489. *
  490. * @return the <code>float</code> value represented by this object
  491. * converted to type <code>short</code>
  492. * @since JDK1.1
  493. */
  494. public short shortValue() {
  495. return (short)value;
  496. }
  497. /**
  498. * Returns the value of this <code>Float</code> as an
  499. * <code>int</code> (by casting to type <code>int</code>).
  500. *
  501. * @return the <code>float</code> value represented by this object
  502. * converted to type <code>int</code>
  503. */
  504. public int intValue() {
  505. return (int)value;
  506. }
  507. /**
  508. * Returns value of this <code>Float</code> as a <code>long</code>
  509. * (by casting to type <code>long</code>).
  510. *
  511. * @return the <code>float</code> value represented by this object
  512. * converted to type <code>long</code>
  513. */
  514. public long longValue() {
  515. return (long)value;
  516. }
  517. /**
  518. * Returns the <code>float</code> value of this <code>Float</code>
  519. * object.
  520. *
  521. * @return the <code>float</code> value represented by this object
  522. */
  523. public float floatValue() {
  524. return value;
  525. }
  526. /**
  527. * Returns the <code>double</code> value of this
  528. * <code>Float</code> object.
  529. *
  530. * @return the <code>float</code> value represented by this
  531. * object is converted to type <code>double</code> and the
  532. * result of the conversion is returned.
  533. */
  534. public double doubleValue() {
  535. return (double)value;
  536. }
  537. /**
  538. * Returns a hash code for this <code>Float</code> object. The
  539. * result is the integer bit representation, exactly as produced
  540. * by the method {@link #floatToIntBits(float)}, of the primitive
  541. * <code>float</code> value represented by this <code>Float</code>
  542. * object.
  543. *
  544. * @return a hash code value for this object.
  545. */
  546. public int hashCode() {
  547. return floatToIntBits(value);
  548. }
  549. /**
  550. * Compares this object against the specified object. The result
  551. * is <code>true</code> if and only if the argument is not
  552. * <code>null</code> and is a <code>Float</code> object that
  553. * represents a <code>float</code> with the same value as the
  554. * <code>float</code> represented by this object. For this
  555. * purpose, two <code>float</code> values are considered to be the
  556. * same if and only if the method {@link #floatToIntBits(float)}
  557. * returns the identical <code>int</code> value when applied to
  558. * each.
  559. * <p>
  560. * Note that in most cases, for two instances of class
  561. * <code>Float</code>, <code>f1</code> and <code>f2</code>, the value
  562. * of <code>f1.equals(f2)</code> is <code>true</code> if and only if
  563. * <blockquote><pre>
  564. * f1.floatValue() == f2.floatValue()
  565. * </pre></blockquote>
  566. * <p>
  567. * also has the value <code>true</code>. However, there are two exceptions:
  568. * <ul>
  569. * <li>If <code>f1</code> and <code>f2</code> both represent
  570. * <code>Float.NaN</code>, then the <code>equals</code> method returns
  571. * <code>true</code>, even though <code>Float.NaN==Float.NaN</code>
  572. * has the value <code>false</code>.
  573. * <li>If <code>f1</code> represents <code>+0.0f</code> while
  574. * <code>f2</code> represents <code>-0.0f</code>, or vice
  575. * versa, the <code>equal</code> test has the value
  576. * <code>false</code>, even though <code>0.0f==-0.0f</code>
  577. * has the value <code>true</code>.
  578. * </ul>
  579. * This definition allows hash tables to operate properly.
  580. *
  581. * @param obj the object to be compared
  582. * @return <code>true</code> if the objects are the same;
  583. * <code>false</code> otherwise.
  584. * @see java.lang.Float#floatToIntBits(float)
  585. */
  586. public boolean equals(Object obj) {
  587. return (obj instanceof Float)
  588. && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
  589. }
  590. /**
  591. * Returns a representation of the specified floating-point value
  592. * according to the IEEE 754 floating-point "single format" bit
  593. * layout.
  594. * <p>
  595. * Bit 31 (the bit that is selected by the mask
  596. * <code>0x80000000</code>) represents the sign of the floating-point
  597. * number.
  598. * Bits 30-23 (the bits that are selected by the mask
  599. * <code>0x7f800000</code>) represent the exponent.
  600. * Bits 22-0 (the bits that are selected by the mask
  601. * <code>0x007fffff</code>) represent the significand (sometimes called
  602. * the mantissa) of the floating-point number.
  603. * <p>If the argument is positive infinity, the result is
  604. * <code>0x7f800000</code>.
  605. * <p>If the argument is negative infinity, the result is
  606. * <code>0xff800000</code>.
  607. * <p>If the argument is NaN, the result is <code>0x7fc00000</code>.
  608. * <p>
  609. * In all cases, the result is an integer that, when given to the
  610. * {@link #intBitsToFloat(int)} method, will produce a floating-point
  611. * value the same as the argument to <code>floatToIntBits</code>
  612. * (except all NaN values are collapsed to a single
  613. * "canonical" NaN value).
  614. *
  615. * @param value a floating-point number.
  616. * @return the bits that represent the floating-point number.
  617. */
  618. public static native int floatToIntBits(float value);
  619. /**
  620. * Returns a representation of the specified floating-point value
  621. * according to the IEEE 754 floating-point "single format" bit
  622. * layout, preserving Not-a-Number (NaN) values.
  623. * <p>
  624. * Bit 31 (the bit that is selected by the mask
  625. * <code>0x80000000</code>) represents the sign of the floating-point
  626. * number.
  627. * Bits 30-23 (the bits that are selected by the mask
  628. * <code>0x7f800000</code>) represent the exponent.
  629. * Bits 22-0 (the bits that are selected by the mask
  630. * <code>0x007fffff</code>) represent the significand (sometimes called
  631. * the mantissa) of the floating-point number.
  632. * <p>If the argument is positive infinity, the result is
  633. * <code>0x7f800000</code>.
  634. * <p>If the argument is negative infinity, the result is
  635. * <code>0xff800000</code>.
  636. * <p>
  637. * If the argument is NaN, the result is the integer representing
  638. * the actual NaN value. Unlike the <code>floatToIntBits</code>
  639. * method, <code>intToRawIntBits</code> does not collapse all the
  640. * bit patterns encoding a NaN to a single "canonical"
  641. * NaN value.
  642. * <p>
  643. * In all cases, the result is an integer that, when given to the
  644. * {@link #intBitsToFloat(int)} method, will produce a
  645. * floating-point value the same as the argument to
  646. * <code>floatToRawIntBits</code>.
  647. * @param value a floating-point number.
  648. * @return the bits that represent the floating-point number.
  649. */
  650. public static native int floatToRawIntBits(float value);
  651. /**
  652. * Returns the <code>float</code> value corresponding to a given
  653. * bit representation.
  654. * The argument is considered to be a representation of a
  655. * floating-point value according to the IEEE 754 floating-point
  656. * "single format" bit layout.
  657. * <p>
  658. * If the argument is <code>0x7f800000</code>, the result is positive
  659. * infinity.
  660. * <p>
  661. * If the argument is <code>0xff800000</code>, the result is negative
  662. * infinity.
  663. * <p>
  664. * If the argument is any value in the range
  665. * <code>0x7f800001</code> through <code>0x7fffffff</code> or in
  666. * the range <code>0xff800001</code> through
  667. * <code>0xffffffff</code>, the result is a NaN. No IEEE 754
  668. * floating-point operation provided by Java can distinguish
  669. * between two NaN values of the same type with different bit
  670. * patterns. Distinct values of NaN are only distinguishable by
  671. * use of the <code>Float.floatToRawIntBits</code> method.
  672. * <p>
  673. * In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
  674. * values that can be computed from the argument:
  675. * <blockquote><pre>
  676. * int s = ((bits >> 31) == 0) ? 1 : -1;
  677. * int e = ((bits >> 23) & 0xff);
  678. * int m = (e == 0) ?
  679. * (bits & 0x7fffff) << 1 :
  680. * (bits & 0x7fffff) | 0x800000;
  681. * </pre></blockquote>
  682. * Then the floating-point result equals the value of the mathematical
  683. * expression <i>s</i>·<i>m</i>·2<sup><i>e</i>-150</sup>.
  684. *<p>
  685. * Note that this method may not be able to return a
  686. * <code>float</code> NaN with exactly same bit pattern as the
  687. * <code>int</code> argument. IEEE 754 distinguishes between two
  688. * kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>. The
  689. * differences between the two kinds of NaN are generally not
  690. * visible in Java. Arithmetic operations on signaling NaNs turn
  691. * them into quiet NaNs with a different, but often similar, bit
  692. * pattern. However, on some processors merely copying a
  693. * signaling NaN also performs that conversion. In particular,
  694. * copying a signaling NaN to return it to the calling method may
  695. * perform this conversion. So <code>intBitsToFloat</code> may
  696. * not be able to return a <code>float</code> with a signaling NaN
  697. * bit pattern. Consequently, for some <code>int</code> values,
  698. * <code>floatToRawIntBits(intBitsToFloat(start))</code> may
  699. * <i>not</i> equal <code>start</code>. Moreover, which
  700. * particular bit patterns represent signaling NaNs is platform
  701. * dependent; although all NaN bit patterns, quiet or signaling,
  702. * must be in the NaN range identified above.
  703. *
  704. * @param bits an integer.
  705. * @return the <code>float</code> floating-point value with the same bit
  706. * pattern.
  707. */
  708. public static native float intBitsToFloat(int bits);
  709. /**
  710. * Compares two <code>Float</code> objects numerically. There are
  711. * two ways in which comparisons performed by this method differ
  712. * from those performed by the Java language numerical comparison
  713. * operators (<code><, <=, ==, >= ></code>) when
  714. * applied to primitive <code>float</code> values:
  715. * <ul><li>
  716. * <code>Float.NaN</code> is considered by this method to
  717. * be equal to itself and greater than all other
  718. * <code>float</code> values
  719. * (including <code>Float.POSITIVE_INFINITY</code>).
  720. * <li>
  721. * <code>0.0f</code> is considered by this method to be greater
  722. * than <code>-0.0f</code>.
  723. * </ul>
  724. * This ensures that the <i>natural ordering</i> of <tt>Float</tt>
  725. * objects imposed by this method is <i>consistent with equals</i>.
  726. *
  727. * @param anotherFloat the <code>Float</code> to be compared.
  728. * @return the value <code>0</code> if <code>anotherFloat</code> is
  729. * numerically equal to this <code>Float</code> a value
  730. * less than <code>0</code> if this <code>Float</code>
  731. * is numerically less than <code>anotherFloat</code>
  732. * and a value greater than <code>0</code> if this
  733. * <code>Float</code> is numerically greater than
  734. * <code>anotherFloat</code>.
  735. *
  736. * @since 1.2
  737. * @see Comparable#compareTo(Object)
  738. */
  739. public int compareTo(Float anotherFloat) {
  740. return Float.compare(value, anotherFloat.value);
  741. }
  742. /**
  743. * Compares the two specified <code>float</code> values. The sign
  744. * of the integer value returned is the same as that of the
  745. * integer that would be returned by the call:
  746. * <pre>
  747. * new Float(f1).compareTo(new Float(f2))
  748. * </pre>
  749. *
  750. * @param f1 the first <code>float</code> to compare.
  751. * @param f2 the second <code>float</code> to compare.
  752. * @return the value <code>0</code> if <code>f1</code> is
  753. * numerically equal to <code>f2</code> a value less than
  754. * <code>0</code> if <code>f1</code> is numerically less than
  755. * <code>f2</code> and a value greater than <code>0</code>
  756. * if <code>f1</code> is numerically greater than
  757. * <code>f2</code>.
  758. * @since 1.4
  759. */
  760. public static int compare(float f1, float f2) {
  761. if (f1 < f2)
  762. return -1; // Neither val is NaN, thisVal is smaller
  763. if (f1 > f2)
  764. return 1; // Neither val is NaN, thisVal is larger
  765. int thisBits = Float.floatToIntBits(f1);
  766. int anotherBits = Float.floatToIntBits(f2);
  767. return (thisBits == anotherBits ? 0 : // Values are equal
  768. (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  769. 1)); // (0.0, -0.0) or (NaN, !NaN)
  770. }
  771. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  772. private static final long serialVersionUID = -2671257302660747028L;
  773. }