com.github.kiprobinson.bigfraction.util
Class DoubleUtil

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

public final class DoubleUtil
extends Object

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

See Also:
Wikipedia overview of IEEE 754 double-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 long 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 long[] getAllParts(double d)
          Returns an array containing the parts of the double.
static long[] getAllParts(double d, boolean exponentAsBits)
          Returns an array containing the parts of the double.
static double getDouble(int sign, int exponent, long mantissa)
          Creates a new double primitive using the provided component bits.
static double getDouble(int sign, int exponent, long mantissa, boolean exponentAsBits)
          Creates a new double primitive using the provided component bits.
static int getExponent(double d)
          Returns the exponent, after adding the exponent offset to the exponent bits.
static int getExponentBits(double d)
          Returns the raw exponent bits, without adjusting for the offset.
static long getMantissa(double d)
          Returns the mantissa bits (bits 51-0).
static int getSign(double d)
          Returns the sign bit (bit 63).
static boolean isFinite(double d)
          Returns true if d is finite--not infinite and not NaN.
static boolean isSubnormal(double d)
          Returns whether or not this double 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 long 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(double d)
Returns true if d is finite--not infinite and not NaN. (Equivalent to Double.isFinite() available from Java 8.)

Parameters:
d - a double value
Returns:
whether this value is finite.

getSign

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

Parameters:
d - a double value
Returns:
the sign bit

getExponent

public static int getExponent(double d)
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:
d - any double value
Returns:
adjusted exponent

getExponentBits

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

Examples:

Parameters:
d - a double value
Returns:
raw exponent bits

getMantissa

public static long getMantissa(double d)
Returns the mantissa bits (bits 51-0). Returned even for NaN values.

Parameters:
d - a double value
Returns:
mantissa bits

isSubnormal

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

Parameters:
d - a double value
Returns:
whether or not this double is a subnormal value

getAllParts

public static long[] getAllParts(double d)
Returns an array containing the parts of the double. Avoids the overhead of four separate function calls.

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

getAllParts

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

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

getDouble

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

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

getDouble

public static double getDouble(int sign,
                               int exponent,
                               long mantissa,
                               boolean exponentAsBits)
Creates a new double 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 double value represented by the provided binary parts.
Throws:
IllegalArgumentException - if any of the parts contain invalid bits.