com.github.kiprobinson.bigfraction.util
Class FloatUtil

java.lang.Object
  extended by com.github.kiprobinson.bigfraction.util.FloatUtil

public final class FloatUtil
extends Object

Additional utilities for working with floay values. Useful to prevent hard-coding IEEE 754 constants in code. Consider this a complement to the methods provided in Float.

See Also:
Wikipedia overview of IEEE 754 float-precision floating point specifications

Field Summary
static int MAX_EXPONENT
          The largest permitted exponent value, when using an exponent with offset.
static int MAX_EXPONENT_BITS
          The largest permitted exponent value, when using raw exponent bits.
static int MAX_MANTISSA
          The largest permitted mantissa value.
static int MIN_EXPONENT
          The smallest permitted exponent value, when using an exponent with offset.
 
Method Summary
static int[] getAllParts(float f)
          Returns an array containing the parts of the float.
static int[] getAllParts(float f, boolean exponentAsBits)
          Returns an array containing the parts of the float.
static int getExponent(float f)
          Returns the exponent, after adding the exponent offset to the exponent bits.
static int getExponentBits(float f)
          Returns the raw exponent bits, without adjusting for the offset.
static float getFloat(int sign, int exponent, int mantissa)
          Creates a new float primitive using the provided component bits.
static float getFloat(int sign, int exponent, int mantissa, boolean exponentAsBits)
          Creates a new float primitive using the provided component bits.
static int getMantissa(float f)
          Returns the mantissa bits (bits 51-0).
static int getSign(float f)
          Returns the sign bit (bit 63).
static boolean isFinite(float f)
          Returns true if f is finite--not infinite and not NaN.
static boolean isSubnormal(float f)
          Returns whether or not this float is a subnormal value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_MANTISSA

public static final int MAX_MANTISSA
The largest permitted mantissa value.

See Also:
Constant Field Values

MAX_EXPONENT_BITS

public static final int MAX_EXPONENT_BITS
The largest permitted exponent value, when using raw exponent bits.

See Also:
Constant Field Values

MIN_EXPONENT

public static final int MIN_EXPONENT
The smallest permitted exponent value, when using an exponent with offset.

See Also:
Constant Field Values

MAX_EXPONENT

public static final int MAX_EXPONENT
The largest permitted exponent value, when using an exponent with offset.

See Also:
Constant Field Values
Method Detail

isFinite

public static boolean isFinite(float f)
Returns true if f is finite--not infinite and not NaN. (Equivalent to Float.isFinite() available from Java 8.)

Parameters:
f - a float value
Returns:
whether this value is finite.

getSign

public static int getSign(float f)
Returns the sign bit (bit 63). 0=positive, 1=negative. This is returned even for NaN values.

Parameters:
f - a float value
Returns:
the sign bit

getExponent

public static int getExponent(float f)
Returns the exponent, after adding the exponent offset to the exponent bits. In other words, the value in bits 62-52, plus 0x3ff.

Examples:

Parameters:
f - any float value
Returns:
adjusted exponent

getExponentBits

public static int getExponentBits(float f)
Returns the raw exponent bits, without adjusting for the offset. In other words, the value in bits 62-52.

Examples:

Parameters:
f - a float value
Returns:
raw exponent bits

getMantissa

public static int getMantissa(float f)
Returns the mantissa bits (bits 51-0). Returned even for NaN values.

Parameters:
f - a float value
Returns:
mantissa bits

isSubnormal

public static boolean isSubnormal(float f)
Returns whether or not this float is a subnormal value.

Parameters:
f - a float value
Returns:
whether or not this float is a subnormal value

getAllParts

public static int[] getAllParts(float f)
Returns an array containing the parts of the float. Avoids the overhead of four separate function calls.

Parameters:
f - a float value
Returns:
array with four elements:
  • return[0]: Equivalent to getSign(f)
  • return[1]: Equivalent to getExponent(f)
  • return[2]: Equivalent to getMantissa(f)
  • return[3]: Equivalent to isSubnormal(f) - Uses zero for false, non-zero for true.

getAllParts

public static int[] getAllParts(float f,
                                boolean exponentAsBits)
Returns an array containing the parts of the float. Avoids the overhead of four separate function calls.

Parameters:
f - a float value
exponentAsBits - whether to return exponent as raw bits rather than adjusted value
Returns:
array with four elements:
  • return[0]: Equivalent to getSign(f)
  • return[1]: Equivalent to (exponentAsBits ? getExponentBits(f) : getExponent(f))
  • return[2]: Equivalent to getMantissa(f)
  • return[3]: Equivalent to isSubnormal(f) - Uses zero for false, non-zero for true.

getFloat

public static float getFloat(int sign,
                             int exponent,
                             int mantissa)
Creates a new float primitive using the provided component bits. Assumes the exponent parameter is signed.

Parameters:
sign - sign bit
exponent - adjusted exponent
mantissa - mantissa bits
Returns:
The float value represented by the provided binary parts.
Throws:
IllegalArgumentException - if any of the parts contain invalid bits.

getFloat

public static float getFloat(int sign,
                             int exponent,
                             int mantissa,
                             boolean exponentAsBits)
Creates a new float primitive using the provided component bits.

Parameters:
sign - sign bit
exponent - exponent (either raw bits or adjusted value)
mantissa - mantissa bits
exponentAsBits - If true, assumes that exponent parameter represents the actual exponent bits. If false, IEEE exponent offset value will be added to the offset to get the bits.
Returns:
The float value represented by the provided binary parts.
Throws:
IllegalArgumentException - if any of the parts contain invalid bits.