Back to index

plt-scheme  4.2.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Package Attributes | Private Attributes | Static Private Attributes
java.lang.Float Class Reference

Instances of class Float represent primitive float values. More...

Inheritance diagram for java.lang.Float:
Inheritance graph
[legend]
Collaboration diagram for java.lang.Float:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Float (float value)
 Create a Float from the primitive float specified.
 Float (double value)
 Create a Float from the primitive double specified.
 Float (String s)
 Create a Float from the specified String.
boolean isNaN ()
 Return true if the value of this Float is the same as NaN, otherwise return false.
boolean isInfinite ()
 Return true if the value of this Float is the same as NEGATIVE_INFINITY or POSITIVE_INFINITY, otherwise return false.
String toString ()
 Convert the float value of this Float to a String.
byte byteValue ()
 Return the value of this Float as a byte.
short shortValue ()
 Return the value of this Float as a short.
int intValue ()
 Return the value of this Integer as an int.
long longValue ()
 Return the value of this Integer as a long.
float floatValue ()
 Return the value of this Float.
double doubleValue ()
 Return the value of this Float as a double
int hashCode ()
 Return a hashcode representing this Object.
boolean equals (Object obj)
 Returns true if obj is an instance of Float and represents the same float value.
int compareTo (Float f)
 Compare two Floats numerically by comparing their float values.
int compareTo (Object o)
 Behaves like compareTo(Float) unless the Object is not an Float.

Static Public Member Functions

static String toString (float f)
 Convert the float to a String.
static Float valueOf (String s)
 Creates a new Float object using the String.
static float parseFloat (String str)
 Parse the specified String as a float.
static boolean isNaN (float v)
 Return true if the float has the same value as NaN, otherwise return false.
static boolean isInfinite (float v)
 Return true if the float has a value equal to either NEGATIVE_INFINITY or POSITIVE_INFINITY, otherwise return false.
static int floatToIntBits (float value)
 Convert the float to the IEEE 754 floating-point "single format" bit layout.
static int floatToRawIntBits (float value)
 Convert the float to the IEEE 754 floating-point "single format" bit layout.
static float intBitsToFloat (int bits)
 Convert the argument in IEEE 754 floating-point "single format" bit layout to the corresponding float.
static int compare (float x, float y)
 Behaves like new Float(x).compareTo(new Float(y)); in other words this compares two floats, special casing NaN and zero, without the overhead of objects.

Static Public Attributes

static final float MAX_VALUE = 3.4028235e+38f
 The maximum positive value a double may represent is 3.4028235e+38f.
static final float MIN_VALUE = 1.4e-45f
 The minimum positive value a float may represent is 1.4e-45.
static final float NEGATIVE_INFINITY = (float) Double.NEGATIVE_INFINITY
 The value of a float representation -1.0/0.0, negative infinity.
static final float POSITIVE_INFINITY = (float) Double.POSITIVE_INFINITY
 The value of a float representation 1.0/0.0, positive infinity.
static final float NaN = (float) Double.NaN
 All IEEE 754 values of NaN have the same value in Java.

Static Package Attributes

static final char[] digits
 Table for calculating digits, used in Character, Long, and Integer.

Private Attributes

final float value
 The primitive type float is represented by this Class object.

Static Private Attributes

static final long serialVersionUID = -2671257302660747028L
 Compatible with JDK 1.0+.

Detailed Description

Instances of class Float represent primitive float values.

Additionally, this class provides various helper functions and variables related to floats.

Author:
Paul Fisher
Andrew Haley aph@c.nosp@m.ygnu.nosp@m.s.com
Eric Blake ebb9@.nosp@m.emai.nosp@m.l.byu.nosp@m..edu
Since:
1.0 updated to 1.4

Definition at line 55 of file Float.java.


Constructor & Destructor Documentation

java.lang.Float.Float ( float  value) [inline]

Create a Float from the primitive float specified.

Parameters:
valuethe float argument

Definition at line 109 of file Float.java.

  {
    this.value = value;
  }

Here is the caller graph for this function:

java.lang.Float.Float ( double  value) [inline]

Create a Float from the primitive double specified.

Parameters:
valuethe double argument

Definition at line 120 of file Float.java.

  {
    this.value = (float) value;
  }
java.lang.Float.Float ( String  s) [inline]

Create a Float from the specified String.

This method calls Float.parseFloat().

Parameters:
sthe String to convert
Exceptions:
NumberFormatExceptionif s cannot be parsed as a float
NullPointerExceptionif s is null
See also:
parseFloat(String)

Definition at line 135 of file Float.java.

  {
    value = parseFloat(s);
  }

Here is the call graph for this function:


Member Function Documentation

Return the value of this Float as a byte.

Returns:
the byte value
Since:
1.1

Reimplemented from java.lang.Number.

Definition at line 319 of file Float.java.

  {
    return (byte) value;
  }
static int java.lang.Float.compare ( float  x,
float  y 
) [inline, static]

Behaves like new Float(x).compareTo(new Float(y)); in other words this compares two floats, special casing NaN and zero, without the overhead of objects.

Parameters:
xthe first float to compare
ythe second float to compare
Returns:
the comparison
Since:
1.4

Definition at line 516 of file Float.java.

  {
    if (isNaN(x))
      return isNaN(y) ? 0 : 1;
    if (isNaN(y))
      return -1;
    // recall that 0.0 == -0.0, so we convert to infinities and try again
    if (x == 0 && y == 0)
      return (int) (1 / x - 1 / y);
    if (x == y)
      return 0;

    return x > y ? 1 : -1;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Compare two Floats numerically by comparing their float values.

The result is positive if the first is greater, negative if the second is greater, and 0 if the two are equal. However, this special cases NaN and signed zero as follows: NaN is considered greater than all other floats, including POSITIVE_INFINITY, and positive zero is considered greater than negative zero.

Parameters:
fthe Float to compare
Returns:
the comparison
Since:
1.2

Definition at line 485 of file Float.java.

  {
    return compare(value, f.value);
  }

Here is the call graph for this function:

int java.lang.Float.compareTo ( Object  o) [inline]

Behaves like compareTo(Float) unless the Object is not an Float.

Parameters:
othe object to compare
Returns:
the comparison
Exceptions:
ClassCastExceptionif the argument is not a Float
See also:
compareTo(Float)
Comparable
Since:
1.2

Definition at line 501 of file Float.java.

  {
    return compare(value, ((Float) o).value);
  }

Here is the call graph for this function:

double java.lang.Float.doubleValue ( ) [inline, virtual]

Return the value of this Float as a double

Returns:
the double value

Implements java.lang.Number.

Definition at line 370 of file Float.java.

  {
    return value;
  }
boolean java.lang.Float.equals ( Object  obj) [inline]

Returns true if obj is an instance of Float and represents the same float value.

Unlike comparing two floats with ==, this treats two instances of Float.NaN as equal, but treats 0.0 and -0.0 as unequal.

Note that f1.equals(f2) is identical to floatToIntBits(f1.floatValue()) == floatToIntBits(f2.floatValue()).

Parameters:
objthe object to compare
Returns:
whether the objects are semantically equal

Definition at line 401 of file Float.java.

  {
    if (! (obj instanceof Float))
      return false;

    float f = ((Float) obj).value;

    // Avoid call to native method. However, some implementations, like gcj,
    // are better off using floatToIntBits(value) == floatToIntBits(f).
    // Check common case first, then check NaN and 0.
    if (value == f)
      return (value != 0) || (1 / value == 1 / f);
    return isNaN(value) && isNaN(f);
  }

Here is the call graph for this function:

static int java.lang.Float.floatToIntBits ( float  value) [inline, static]

Convert the float to the IEEE 754 floating-point "single format" bit layout.

Bit 31 (the most significant) is the sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and bits 22-0 (masked by 0x007fffff) are the mantissa. This function collapses all versions of NaN to 0x7fc00000. The result of this function can be used as the argument to Float.intBitsToFloat(int) to obtain the original float value.

Parameters:
valuethe float to convert
Returns:
the bits of the float
See also:
intBitsToFloat(int)

Definition at line 429 of file Float.java.

  {
      throw new RuntimeException("floatToIntBits has not been implemented");
//    return VMFloat.floatToIntBits(value);
  }

Here is the caller graph for this function:

static int java.lang.Float.floatToRawIntBits ( float  value) [inline, static]

Convert the float to the IEEE 754 floating-point "single format" bit layout.

Bit 31 (the most significant) is the sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves NaN alone, rather than collapsing to a canonical value. The result of this function can be used as the argument to Float.intBitsToFloat(int) to obtain the original float value.

Parameters:
valuethe float to convert
Returns:
the bits of the float
See also:
intBitsToFloat(int)

Definition at line 448 of file Float.java.

  {
      throw new RuntimeException("floatToRawIntBits has not been implemented");
//    return VMFloat.floatToRawIntBits(value);
  }
float java.lang.Float.floatValue ( ) [inline, virtual]

Return the value of this Float.

Returns:
the float value

Implements java.lang.Number.

Definition at line 360 of file Float.java.

  {
    return value;
  }

Return a hashcode representing this Object.

Float's hash code is calculated by calling floatToIntBits(floatValue()).

Returns:
this Object's hash code
See also:
floatToIntBits(float)

Definition at line 382 of file Float.java.

  {
    return floatToIntBits(value);
  }

Here is the call graph for this function:

static float java.lang.Float.intBitsToFloat ( int  bits) [inline, static]

Convert the argument in IEEE 754 floating-point "single format" bit layout to the corresponding float.

Bit 31 (the most significant) is the sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves NaN alone, so that you can recover the bit pattern with Float.floatToRawIntBits(float).

Parameters:
bitsthe bits to convert
Returns:
the float represented by the bits
See also:
floatToIntBits(float)
floatToRawIntBits(float)

Definition at line 467 of file Float.java.

  {
      throw new RuntimeException("intBitsToFloat has not been implmented");
//    return VMFloat.intBitsToFloat(bits);
  }
int java.lang.Float.intValue ( ) [inline, virtual]

Return the value of this Integer as an int.

Returns:
the int value

Implements java.lang.Number.

Definition at line 340 of file Float.java.

  {
    return (int) value;
  }
static boolean java.lang.Float.isInfinite ( float  v) [inline, static]

Return true if the float has a value equal to either NEGATIVE_INFINITY or POSITIVE_INFINITY, otherwise return false.

Parameters:
vthe float to compare
Returns:
whether the argument is (-/+) infinity

Definition at line 272 of file Float.java.

  {
    return v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY;
  }

Return true if the value of this Float is the same as NEGATIVE_INFINITY or POSITIVE_INFINITY, otherwise return false.

Returns:
whether this Float is (-/+) infinity

Definition at line 295 of file Float.java.

  {
    return isInfinite(value);
  }
static boolean java.lang.Float.isNaN ( float  v) [inline, static]

Return true if the float has the same value as NaN, otherwise return false.

Parameters:
vthe float to compare
Returns:
whether the argument is NaN

Definition at line 257 of file Float.java.

  {
    // This works since NaN != NaN is the only reflexive inequality
    // comparison which returns true.
    return v != v;
  }

Return true if the value of this Float is the same as NaN, otherwise return false.

Returns:
whether this Float is NaN

Definition at line 283 of file Float.java.

  {
    return isNaN(value);
  }

Here is the caller graph for this function:

long java.lang.Float.longValue ( ) [inline, virtual]

Return the value of this Integer as a long.

Returns:
the long value

Implements java.lang.Number.

Definition at line 350 of file Float.java.

  {
    return (long) value;
  }
static float java.lang.Float.parseFloat ( String  str) [inline, static]

Parse the specified String as a float.

The extended BNF grammar is as follows:

 DecodableString:
      ( [ - | + ] NaN )
    | ( [ - | + ] Infinity )
    | ( [ - | + ] FloatingPoint
              [ f | F | d
                | D] )
 FloatingPoint:
      ( { Digit }+ [ . { Digit } ]
              [ Exponent ] )
    | ( . { Digit }+ [ Exponent ] )
 Exponent:
      ( ( e | E )
              [ - | + ] { Digit }+ )
 Digit: '0' through '9'
 

NaN and infinity are special cases, to allow parsing of the output of toString. Otherwise, the result is determined by calculating n * 10exponent to infinite precision, then rounding to the nearest float. Remember that many numbers cannot be precisely represented in floating point. In case of overflow, infinity is used, and in case of underflow, signed zero is used. Unlike Integer.parseInt, this does not accept Unicode digits outside the ASCII range.

If an unexpected character is found in the String, a NumberFormatException will be thrown. Leading and trailing 'whitespace' is ignored via String.trim(), but spaces internal to the actual number are not allowed.

To parse numbers according to another format, consider using java.text.NumberFormat.

specify where/how we are not in accord with the spec.

Parameters:
strthe String to convert
Returns:
the float value of s
Exceptions:
NumberFormatExceptionif s cannot be parsed as a float
NullPointerExceptionif s is null
See also:
MIN_VALUE
MAX_VALUE
POSITIVE_INFINITY
NEGATIVE_INFINITY
Since:
1.2

Definition at line 243 of file Float.java.

  {
    // XXX Rounding parseDouble() causes some errors greater than 1 ulp from
    // the infinitely precise decimal.
    return (float) Double.parseDouble(str);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

short java.lang.Float.shortValue ( ) [inline]

Return the value of this Float as a short.

Returns:
the short value
Since:
1.1

Reimplemented from java.lang.Number.

Definition at line 330 of file Float.java.

  {
    return (short) value;
  }
static String java.lang.Float.toString ( float  f) [inline, static]

Convert the float to a String.

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

Value of FloatString Representation
[+-] 0 [-]0.0
Between [+-] 10-3 and 107, exclusive [-]number.number
Other numeric value [-]<digit>.<number> E[-]<number>
[+-] infinity [-]Infinity
NaN NaN

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. The result is rounded to the shortest decimal number which will parse back to the same float.

To create other output formats, use java.text.NumberFormat.

specify where we are not in accord with the spec.

Parameters:
fthe float to convert
Returns:
the String representing the float

Definition at line 174 of file Float.java.

  {
    return Double.toString(f, true);
  }

Here is the call graph for this function:

String java.lang.Float.toString ( ) [inline]

Convert the float value of this Float to a String.

This method calls Float.toString(float) to do its dirty work.

Returns:
the String representation
See also:
toString(float)

Definition at line 308 of file Float.java.

  {
    return toString(value);
  }
static Float java.lang.Float.valueOf ( String  s) [inline, static]

Creates a new Float object using the String.

Parameters:
sthe String to convert
Returns:
the new Float
Exceptions:
NumberFormatExceptionif s cannot be parsed as a float
NullPointerExceptionif s is null
See also:
parseFloat(String)

Definition at line 189 of file Float.java.

  {
    return new Float(parseFloat(s));
  }

Here is the call graph for this function:


Member Data Documentation

final char [] java.lang.Number.digits [static, package, inherited]
Initial value:
 {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
    'u', 'v', 'w', 'x', 'y', 'z',
  }

Table for calculating digits, used in Character, Long, and Integer.

Definition at line 68 of file Number.java.

final float java.lang.Float.MAX_VALUE = 3.4028235e+38f [static]

The maximum positive value a double may represent is 3.4028235e+38f.

Definition at line 66 of file Float.java.

final float java.lang.Float.MIN_VALUE = 1.4e-45f [static]

The minimum positive value a float may represent is 1.4e-45.

Definition at line 72 of file Float.java.

final float java.lang.Float.NaN = (float) Double.NaN [static]

All IEEE 754 values of NaN have the same value in Java.

Definition at line 87 of file Float.java.

The value of a float representation -1.0/0.0, negative infinity.

Definition at line 77 of file Float.java.

The value of a float representation 1.0/0.0, positive infinity.

Definition at line 82 of file Float.java.

final long java.lang.Float.serialVersionUID = -2671257302660747028L [static, private]

Compatible with JDK 1.0+.

Reimplemented from java.lang.Number.

Definition at line 60 of file Float.java.

final float java.lang.Float.value [private]

The primitive type float is represented by this Class object.

Since:
1.1 The immutable value of this Float.

the wrapped float

Definition at line 101 of file Float.java.


The documentation for this class was generated from the following file: