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