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