001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang;
019:
020: /**
021: * <p>
022: * Float is the wrapper for the primitive type <code>float</code>.
023: * </p>
024: *
025: * @see java.lang.Number
026: * @since 1.0
027: */
028: public final class Float extends Number implements Comparable<Float> {
029:
030: private static final long serialVersionUID = -2671257302660747028L;
031:
032: /**
033: * The value which the receiver represents.
034: */
035: private final float value;
036:
037: /**
038: * <p>
039: * Constant for the maximum <code>float</code> value, (2 - 2<sup>-23</sup>) *
040: * 2<sup>127</sup>.
041: * </p>
042: */
043: public static final float MAX_VALUE = 3.40282346638528860e+38f;
044:
045: /**
046: * <p>
047: * Constant for the minimum <code>float</code> value, 2<sup>-149</sup>.
048: * </p>
049: */
050: public static final float MIN_VALUE = 1.40129846432481707e-45f;
051:
052: /**
053: * <p>
054: * Constant for the Not-a-Number (NaN) value of the <code>float</code>
055: * type.
056: * </p>
057: */
058: public static final float NaN = 0.0f / 0.0f;
059:
060: /**
061: * <p>
062: * Constant for the Positive Infinity value of the <code>float</code>
063: * type.
064: * </p>
065: */
066: public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
067:
068: /**
069: * <p>
070: * Constant for the Negative Infinity value of the <code>float</code>
071: * type.
072: * </p>
073: */
074: public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
075:
076: /**
077: * The java.lang.Class that represents this class.
078: *
079: * @since 1.1
080: */
081: @SuppressWarnings("unchecked")
082: public static final Class<Float> TYPE = (Class<Float>) new float[0]
083: .getClass().getComponentType();
084:
085: // Note: This can't be set to "float.class", since *that* is
086: // defined to be "java.lang.Float.TYPE";
087:
088: /**
089: * <p>
090: * Constant for the number of bits to represent a <code>float</code> in
091: * two's compliment form.
092: * </p>
093: *
094: * @since 1.5
095: */
096: public static final int SIZE = 32;
097:
098: /**
099: * Constructs a new instance of the receiver which represents the float
100: * valued argument.
101: *
102: * @param value
103: * the float to store in the new instance.
104: */
105: public Float(float value) {
106: this .value = value;
107: }
108:
109: /**
110: * Constructs a new instance of the receiver which represents the double
111: * valued argument.
112: *
113: * @param value
114: * the double to store in the new instance.
115: */
116: public Float(double value) {
117: this .value = (float) value;
118: }
119:
120: /**
121: * Constructs a new instance of this class given a string.
122: *
123: * @param string
124: * a string representation of a float quantity.
125: * @exception NumberFormatException
126: * if the argument could not be parsed as a float quantity.
127: */
128: public Float(String string) throws NumberFormatException {
129: this (parseFloat(string));
130: }
131:
132: /**
133: * Compares the receiver with the Float parameter. NaN is equal to NaN, and
134: * is greater than other float values. 0f is greater than -0f.
135: *
136: * @param object
137: * the Float to compare to the receiver
138: *
139: * @return Returns greater than zero when this.floatValue() is greater than
140: * object.floatValue(), zero when this.floatValue() equals
141: * object.floatValue(), and less than zero when this.floatValue() is
142: * less than object.floatValue()
143: * @throws NullPointerException
144: * if <code>object</code> is <code>null</code>.
145: * @since 1.2
146: */
147: public int compareTo(Float object) {
148: int f1, f2;
149: int NaNbits = Float.floatToIntBits(Float.NaN);
150: if ((f1 = Float.floatToIntBits(value)) == NaNbits) {
151: if (Float.floatToIntBits(object.value) == NaNbits) {
152: return 0;
153: }
154: return 1;
155: }
156: if ((f2 = Float.floatToIntBits(object.value)) == NaNbits) {
157: return -1;
158: }
159: if (value == object.value) {
160: if (f1 == f2) {
161: return 0;
162: }
163: // check for -0
164: return f1 > f2 ? 1 : -1;
165: }
166: return value > object.value ? 1 : -1;
167: }
168:
169: /**
170: * Answers the byte value which the receiver represents
171: *
172: * @return byte the value of the receiver.
173: */
174: @Override
175: public byte byteValue() {
176: return (byte) value;
177: }
178:
179: /**
180: * Answers the double value which the receiver represents
181: *
182: * @return double the value of the receiver.
183: */
184: @Override
185: public double doubleValue() {
186: return value;
187: }
188:
189: /**
190: * Compares the argument to the receiver, and answers true if they represent
191: * the <em>same</em> object using a class specific comparison. For Floats,
192: * the check verifies that the receiver's value's bit pattern matches the
193: * bit pattern of the argument, which must also be a Float.
194: *
195: * @param object
196: * the object to compare with this object
197: * @return <code>true</code> if the object is the same as this object
198: * <code>false</code> if it is different from this object
199: * @see #hashCode
200: */
201: @Override
202: public boolean equals(Object object) {
203: return (object == this )
204: || (object instanceof Float)
205: && (floatToIntBits(this .value) == floatToIntBits(((Float) object).value));
206: }
207:
208: /**
209: * Answers the binary representation of the argument, as an int.
210: *
211: * @param value
212: * The float value to convert
213: * @return the bits of the float.
214: */
215: public static native int floatToIntBits(float value);
216:
217: /**
218: * Answers the binary representation of the argument, as an int.
219: *
220: * @param value
221: * The float value to convert
222: * @return the bits of the float.
223: */
224: public static native int floatToRawIntBits(float value);
225:
226: /**
227: * Answers the receiver's value as a float.
228: *
229: * @return the receiver's value
230: */
231: @Override
232: public float floatValue() {
233: return value;
234: }
235:
236: /**
237: * Answers an integer hash code for the receiver. Any two objects which
238: * answer <code>true</code> when passed to <code>equals</code> must
239: * answer the same value for this method.
240: *
241: * @return the receiver's hash
242: *
243: * @see #equals
244: */
245: @Override
246: public int hashCode() {
247: return floatToIntBits(value);
248: }
249:
250: /**
251: * Answers a float built from the binary representation given in the
252: * argument.
253: *
254: * @param bits
255: * the bits of the float
256: * @return the float which matches the bits
257: */
258: public static native float intBitsToFloat(int bits);
259:
260: /**
261: * Answers the int value which the receiver represents
262: *
263: * @return int the value of the receiver.
264: */
265: @Override
266: public int intValue() {
267: return (int) value;
268: }
269:
270: /**
271: * Answers true if the receiver represents an infinite quantity, and false
272: * otherwise.
273: *
274: * @return <code>true</code> if the argument is positive or negative
275: * infinity <code>false</code> if it is not an infinite value
276: */
277: public boolean isInfinite() {
278: return isInfinite(value);
279: }
280:
281: /**
282: * Answers true if the argument represents an infinite quantity, and false
283: * otherwise.
284: *
285: * @param f
286: * value to check for infinitness.
287: * @return <code>true</code> if the argument is positive or negative
288: * infinity <code>false</code> if it is not an infinite value
289: */
290: public static boolean isInfinite(float f) {
291: return (f == POSITIVE_INFINITY) || (f == NEGATIVE_INFINITY);
292: }
293:
294: /**
295: * Answers true if the receiver does not represent a valid float quantity.
296: *
297: * @return <code>true</code> if the argument is Not A Number
298: * <code>false</code> if it is a (potentially infinite) float
299: * number
300: */
301: public boolean isNaN() {
302: return isNaN(value);
303: }
304:
305: /**
306: * Answers true if the argument does not represent a valid float quantity.
307: *
308: * @param f
309: * value to check for numberness.
310: * @return <code>true</code> if the argument is Not A Number
311: * <code>false</code> if it is a (potentially infinite) float
312: * number
313: */
314: public static boolean isNaN(float f) {
315: return f != f;
316: }
317:
318: /**
319: * Answers the long value which the receiver represents
320: *
321: * @return long the value of the receiver.
322: */
323: @Override
324: public long longValue() {
325: return (long) value;
326: }
327:
328: /**
329: * Answers the float which matches the passed in string.
330: * NumberFormatException is thrown if the string does not represent a valid
331: * float.
332: *
333: * @param string
334: * the value to convert
335: * @return a float which would print as the argument
336: * @see #valueOf(String)
337: * @since 1.2
338: */
339: public static float parseFloat(String string)
340: throws NumberFormatException {
341: return org.apache.harmony.luni.util.FloatingPointParser
342: .parseFloat(string);
343: }
344:
345: /**
346: * Answers the short value which the receiver represents
347: *
348: * @return short the value of the receiver.
349: * @since 1.1
350: */
351: @Override
352: public short shortValue() {
353: return (short) value;
354: }
355:
356: /**
357: * Answers a string containing a concise, human-readable description of the
358: * receiver.
359: *
360: * @return a printable representation for the receiver.
361: */
362: @Override
363: public String toString() {
364: return Float.toString(value);
365: }
366:
367: /**
368: * Answers a string containing a printable representation of the argument.
369: *
370: * @param f
371: * the float to print
372: * @return a printable representation of the argument.
373: */
374: public static String toString(float f) {
375: return org.apache.harmony.luni.util.NumberConverter.convert(f);
376: }
377:
378: /**
379: * Answers the float which matches the passed in string.
380: * NumberFormatException is thrown if the string does not represent a valid
381: * float.
382: *
383: * @param string
384: * the value to convert
385: * @return a float which would print as the argument
386: */
387: public static Float valueOf(String string)
388: throws NumberFormatException {
389: return valueOf(parseFloat(string));
390: }
391:
392: /**
393: * Compares the two floats. NaN is equal to NaN, and is greater than other
394: * float values. 0f is greater than -0f.
395: *
396: * @param float1
397: * the first value to compare
398: * @param float2
399: * the second value to compare
400: *
401: * @return Returns greater than zero when float1 is greater than float2,
402: * zero when float1 equals float2, and less than zero when float1 is
403: * less than float2
404: * @since 1.4
405: */
406: public static int compare(float float1, float float2) {
407: int f1, f2;
408: int NaNbits = Float.floatToIntBits(Float.NaN);
409: if ((f1 = Float.floatToIntBits(float1)) == NaNbits) {
410: if (Float.floatToIntBits(float2) == NaNbits) {
411: return 0;
412: }
413: return 1;
414: }
415: if ((f2 = Float.floatToIntBits(float2)) == NaNbits) {
416: return -1;
417: }
418: if (float1 == float2) {
419: if (f1 == f2) {
420: return 0;
421: }
422: // check for -0
423: return f1 > f2 ? 1 : -1;
424: }
425: return float1 > float2 ? 1 : -1;
426: }
427:
428: /**
429: * <p>
430: * Returns a <code>Float</code> instance for the <code>float</code>
431: * value passed. This method is preferred over the constructor, as this
432: * method may maintain a cache of instances.
433: * </p>
434: *
435: * @param f
436: * The float value.
437: * @return A <code>Float</code> instance.
438: * @since 1.5
439: */
440: public static Float valueOf(float f) {
441: return new Float(f);
442: }
443:
444: /**
445: * <p>
446: * Converts a <code>float</code> into a hexadecimal string representation.
447: * </p>
448: *
449: * @param f
450: * The <code>float</code> to convert.
451: * @return The hexadecimal string representation of <code>f</code>.
452: * @since 1.5
453: */
454: public static String toHexString(float f) {
455: /*
456: * Reference: http://en.wikipedia.org/wiki/IEEE_754
457: */
458: if (f != f) {
459: return "NaN"; //$NON-NLS-1$
460: }
461: if (f == POSITIVE_INFINITY) {
462: return "Infinity"; //$NON-NLS-1$
463: }
464: if (f == NEGATIVE_INFINITY) {
465: return "-Infinity"; //$NON-NLS-1$
466: }
467:
468: int bitValue = floatToIntBits(f);
469:
470: boolean negative = (bitValue & 0x80000000) != 0;
471: // mask exponent bits and shift down
472: int exponent = (bitValue & 0x7f800000) >>> 23;
473: // mask significand bits and shift up
474: // significand is 23-bits, so we shift to treat it like 24-bits
475: int significand = (bitValue & 0x007FFFFF) << 1;
476:
477: if (exponent == 0 && significand == 0) {
478: return (negative ? "-0x0.0p0" : "0x0.0p0"); //$NON-NLS-1$ //$NON-NLS-2$
479: }
480:
481: StringBuilder hexString = new StringBuilder(10);
482: if (negative) {
483: hexString.append("-0x"); //$NON-NLS-1$
484: } else {
485: hexString.append("0x"); //$NON-NLS-1$
486: }
487:
488: if (exponent == 0) { // denormal (subnormal) value
489: hexString.append("0."); //$NON-NLS-1$
490: // significand is 23-bits, so there can be 6 hex digits
491: int fractionDigits = 6;
492: // remove trailing hex zeros, so Integer.toHexString() won't print
493: // them
494: while ((significand != 0) && ((significand & 0xF) == 0)) {
495: significand >>>= 4;
496: fractionDigits--;
497: }
498: // this assumes Integer.toHexString() returns lowercase characters
499: String hexSignificand = Integer.toHexString(significand);
500:
501: // if there are digits left, then insert some '0' chars first
502: if (significand != 0
503: && fractionDigits > hexSignificand.length()) {
504: int digitDiff = fractionDigits
505: - hexSignificand.length();
506: while (digitDiff-- != 0) {
507: hexString.append('0');
508: }
509: }
510: hexString.append(hexSignificand);
511: hexString.append("p-126"); //$NON-NLS-1$
512: } else { // normal value
513: hexString.append("1."); //$NON-NLS-1$
514: // significand is 23-bits, so there can be 6 hex digits
515: int fractionDigits = 6;
516: // remove trailing hex zeros, so Integer.toHexString() won't print
517: // them
518: while ((significand != 0) && ((significand & 0xF) == 0)) {
519: significand >>>= 4;
520: fractionDigits--;
521: }
522: // this assumes Integer.toHexString() returns lowercase characters
523: String hexSignificand = Integer.toHexString(significand);
524:
525: // if there are digits left, then insert some '0' chars first
526: if (significand != 0
527: && fractionDigits > hexSignificand.length()) {
528: int digitDiff = fractionDigits
529: - hexSignificand.length();
530: while (digitDiff-- != 0) {
531: hexString.append('0');
532: }
533: }
534: hexString.append(hexSignificand);
535: hexString.append('p');
536: // remove exponent's 'bias' and convert to a string
537: hexString.append(Integer.toString(exponent - 127));
538: }
539: return hexString.toString();
540: }
541: }
|