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