/* java.lang.Float Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. As a special exception, if you link this library with other files to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ package java.lang; import gnu.classpath.Configuration; /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com. * Status: Believed complete and correct. */ /** * Instances of class Float represent primitive * float values. * * Additionally, this class provides various helper functions and variables * related to floats. * * @author Paul Fisher * @author Andrew Haley * @since JDK 1.0 */ public final class Float extends Number implements Comparable { /** * The maximum positive value a double may represent * is 3.4028235e+38f. */ public static final float MAX_VALUE = 3.4028235e+38f; /** * The minimum positive value a float may represent * is 1.4e-45. */ public static final float MIN_VALUE = 1.4e-45f; /** * The value of a float representation -1.0/0.0, negative infinity. */ public static final float NEGATIVE_INFINITY = -1.0f/0.0f; /** * The value of a float representation 1.0/0.0, positive infinity. */ public static final float POSITIVE_INFINITY = 1.0f/0.0f; /** * All IEEE 754 values of NaN have the same value in Java. */ public static final float NaN = 0.0f/0.0f; /** * The primitive type float is represented by this * Class object. */ public static final Class TYPE = VMClassLoader.getPrimitiveClass ("float"); /** * The immutable value of this Float. */ private final float value; private static final long serialVersionUID = -2671257302660747028L; static { if (Configuration.INIT_LOAD_LIBRARY) { System.loadLibrary ("javalang"); } } /** * Create a float from the primitive Float * specified. * * @param value the Float argument */ public Float (float value) { this.value = value; } /** * Create a Float from the primitive double * specified. * * @param value the double argument */ public Float (double value) { this.value = (float)value; } /** * Create a Float from the specified String. * * This method calls Float.parseFloat(). * * @exception NumberFormatException when the String cannot * be parsed into a Float. * @param s the String to convert * @see #parseFloat(java.lang.String) */ public Float (String s) throws NumberFormatException { this.value = parseFloat (s); } /** * Parse the specified String as a float. * * The number is really read as n * 10exponent. The * first number is n, and if there is an "E" * ("e" is also acceptable), then the integer after that is * the exponent. *

* Here are the possible forms the number can take: *
* * * * * * *
FormExamples
[+-]<number>[.]345., -10, 12
[+-]<number>.<number>40.2, 80.00, -12.30
[+-]<number>[.]E[+-]<number>80E12, -12e+7, 4.E-123
[+-]<number>.<number>E[+-]<number>6.02e-22, -40.2E+6, 12.3e9
* * "[+-]" means either a plus or minus sign may go there, or * neither, in which case + is assumed. *
* "[.]" means a dot may be placed here, but is optional. *
* "<number>" means a string of digits (0-9), basically * an integer. "<number>.<number>" is basically * a real number, a floating-point value. *

* Remember that a float has a limited range. If the * number you specify is greater than Float.MAX_VALUE or less * than -Float.MAX_VALUE, it will be set at * Float.POSITIVE_INFINITY or * Float.NEGATIVE_INFINITY, respectively. *

* * Note also that float does not have perfect precision. Many * numbers cannot be precisely represented. The number you specify * will be rounded to the nearest representable value. * Float.MIN_VALUE is the margin of error for float * values. *

* If an unexpected character is found in the String, a * NumberFormatException will be thrown. Spaces are not * allowed and will cause this exception to be thrown. * * @XXX specify where/how we are not in accord with the spec. * * @param str the String to convert * @return the value of the String as a float. * @exception NumberFormatException when the string cannot be parsed to a * float. * @since JDK 1.2 * @see #MIN_VALUE * @see #MAX_VALUE * @see #POSITIVE_INFINITY * @see #NEGATIVE_INFINITY */ public static float parseFloat (String s) throws NumberFormatException { // The spec says that parseFloat() should work like // Double.valueOf(). This is equivalent, in our implementation, // but more efficient. return (float) Double.parseDouble (s); } /** * Convert the float value of this Float * to a String. This method calls * Float.toString(float) to do its dirty work. * * @return the String representation of this Float. * @see #toString(float) */ public String toString () { return toString (value); } /** * If the Object is not null, is an * instanceof Float, and represents * the same primitive float value return * true. Otherwise false is returned. * * @param obj the object to compare to * @return whether the objects are semantically equal. */ public boolean equals (Object obj) { if (!(obj instanceof Float)) return false; Float f = (Float) obj; return floatToIntBits (value) == floatToIntBits (f.floatValue ()); } /** * Return a hashcode representing this Object. * Float's hash code is calculated by calling the * floatToIntBits() function. * @return this Object's hash code. * @see java.lang.Float.floatToIntBits(float) */ public int hashCode () { return floatToIntBits (value); } /** * Return the value of this Double when cast to an * int. */ public int intValue () { return (int) value; } /** * Return the value of this Double when cast to a * long. */ public long longValue () { return (long) value; } /** * Return the value of this Double when cast to a * float. */ public float floatValue () { return (float) value; } /** * Return the primitive double value represented by this * Double. */ public double doubleValue () { return (double) value; } /** * Convert the float to a String. *

* * Floating-point string representation is fairly complex: here is a * rundown of the possible values. "[-]" indicates that a * negative sign will be printed if the value (or exponent) is negative. * "<number>" means a string of digits (0-9). * "<digit>" means a single digit (0-9). *

* * * * * * * * * * * * * * * * * * * * * * * *
Value of FloatString Representation
[+-] 0[-]0.0
Between [+-] 10-3 and 107[-]number.number
Other numeric value[-]<digit>.<number>E[-]<number>
[+-] infinity[-]Infinity
NaNNaN
* * Yes, negative zero is a possible value. Note that there is * always a . and at least one digit printed after * it: even if the number is 3, it will be printed as 3.0. * After the ".", all digits will be printed except trailing zeros. No * truncation or rounding is done by this function. * * @XXX specify where we are not in accord with the spec. * * @param f the float to convert * @return the String representing the float. */ public static String toString (float f) { return Double.toString ((double) f, true); } /** * Return the result of calling new Float(java.lang.String). * * @param s the String to convert to a Float. * @return a new Float representing the String's * numeric value. * * @exception NumberFormatException thrown if String cannot * be parsed as a double. * @see #Float(java.lang.String) * @see #parseFloat(java.lang.String) */ public static Float valueOf (String s) throws NumberFormatException { return new Float (s); } /** * Return true if the value of this Float * is the same as NaN, otherwise return false. * @return whether this Float is NaN. */ public boolean isNaN () { return isNaN (value); } /** * Return true if the float has the same * value as NaN, otherwise return false. * * @param v the float to compare * @return whether the argument is NaN. */ public static boolean isNaN (float v) { int bits = floatToIntBits (v); int e = bits & 0x7f800000; int f = bits & 0x007fffff; return e == 0x7f800000 && f != 0; } /** * Return true if the value of this Float * is the same as NEGATIVE_INFINITY or * POSITIVE_INFINITY, otherwise return false. * * @return whether this Float is (-/+) infinity. */ public boolean isInfinite () { return isInfinite (value); } /** * Return true if the float has a value * equal to either NEGATIVE_INFINITY or * POSITIVE_INFINITY, otherwise return false. * * @param v the float to compare * @return whether the argument is (-/+) infinity. */ public static boolean isInfinite (float v) { int bits = floatToIntBits (v); int f = bits & 0x7fffffff; return f == 0x7f800000; } /** * Return the int bits of the specified float. * The result of this function can be used as the argument to * Float.intBitsToFloat(long) to obtain the * original float value. * * @param value the float to convert * @return the bits of the float. */ public static native int floatToIntBits (float value); /** * Return the int bits of the specified float. * The result of this function can be used as the argument to * Float.intBitsToFloat(long) to obtain the * original float value. The difference between * this function and floatToIntBits is that this * function does not collapse NaN values. * * @param value the float to convert * @return the bits of the float. */ public static native int floatToRawIntBits (float value); /** * Return the float represented by the long * bits specified. * * @param bits the long bits representing a double * @return the float represented by the bits. */ public static native float intBitsToFloat (int bits); /** * Returns 0 if the float value of the argument is * equal to the value of this Float. Returns a number * less than zero if the value of this Float is less * than the Float value of the argument, and returns a * number greater than zero if the value of this Float * is greater than the float value of the argument. *
* Float.NaN is greater than any number other than itself, * even Float.POSITIVE_INFINITY. *
* 0.0 is greater than -0.0. * * @param f the Float to compare to. * @return 0 if the Floats are the same, < 0 if this * Float is less than the Float in * in question, or > 0 if it is greater. * * @since 1.2 */ public int compareTo (Float f) { return compare (value, f.value); } /** * Returns 0 if the first argument is equal to the second argument. * Returns a number less than zero if the first argument is less than the * second argument, and returns a number greater than zero if the first * argument is greater than the second argument. *
* Float.NaN is greater than any number other than itself, * even Float.POSITIVE_INFINITY. *
* 0.0 is greater than -0.0. * * @param x the first float to compare. * @param y the second float to compare. * @return 0 if the arguments are the same, < 0 if the * first argument is less than the second argument in * in question, or > 0 if it is greater. * @since 1.4 */ public static int compare (float x, float y) { if (isNaN (x)) return isNaN (y) ? 0 : 1; if (isNaN (y)) return -1; if (x == 0.0 && y == -0.0) return 1; if (x == -0.0 && y == 0.0) return -1; if (x == y) return 0; return x > y ? 1 : -1; } /** * Compares the specified Object to this Float * if and only if the Object is an instanceof * Float. * Otherwise it throws a ClassCastException * * @param o the Object to compare to. * @return 0 if the Floats are the same, < 0 if this * Float is less than the Float in * in question, or > 0 if it is greater. * @throws ClassCastException if the argument is not a Float * * @since 1.2 */ public int compareTo (Object o) { return compareTo ((Float) o); } }