Class NFPSignedDoubleInt

java.lang.Object
com.io7m.jnfp.core.NFPSignedDoubleInt

public final class NFPSignedDoubleInt extends Object

Conversion of signed normalized fixed-point values to and from floating point values.

The OpenGL 3.3 specification defines two representations of signed normalized fixed-point values with b bits of precision. The first representation maps -1.0 to -(2 ^ (b - 1)) and 1.0 to (2 ^ (b -1)) - 1. This representation does not allow for 0 to be expressed directly. The second representation maps -1.0 to -(2 ^ (b - 1)) + 1 and 1.0 to (2 ^ (b -1)) - 1, which allows for 0 to be represented exactly but means that -(2 ^ (b - 1)) is outside of the representable range.

For lack of better names, this implementation refers to the first representation as without-zero, and the second as with-zero.

For the without-zero representation, conversion from signed normalized fixed-point values f to floating point values x is defined as:

 x = ((2 * f) + 1) / (pow(2, b) - 1)
 

For the with-zero representation, conversion from signed normalized fixed-point values f to floating point values x is defined as:

 x = max (-1.0, f / (pow(2, b - 1) - 1))
 

For the without-zero representation, conversion from floating point values x (assumed to be in the range [-1.0, 1.0]) to signed normalized fixed-point values f is defined as:

 f = ((x * (pow(2, b) - 1)) - 1) / 2
 

For the with-zero representation, conversion from floating point values x (assumed to be in the range [-1.0, 1.0]) to signed normalized fixed-point values f is defined as:

 f = x * (pow(2, b - 1) - 1)
 
  • Method Details

    • fromSignedNormalizedWithoutZero

      public static double fromSignedNormalizedWithoutZero(int f, int b)
      Convert f to floating point format. f is assumed to be an signed fixed-point value with b bits of precision, using the without-zero representation described in the documentation at the beginning of this class.
      Parameters:
      f - A value in the range [-(2 ^ (b - 1)), (2 ^ (b -1)) - 1]
      b - A value in the range [2, 32]
      Returns:
      A floating point value in the range [-1, 1]
    • toSignedNormalizedWithoutZero

      public static int toSignedNormalizedWithoutZero(double x, int b)
      Convert x to fixed-point format using the without-zero representation described in the documentation at the beginning of this class.
      Parameters:
      x - A value in the range [-1, 1]
      b - A value in the range [2, 32]
      Returns:
      A signed normalized fixed-point value with b bits of precision
    • fromSignedNormalizedWithZero

      public static double fromSignedNormalizedWithZero(int f, int b)
      Convert f to floating point format. f is assumed to be an signed fixed-point value with b bits of precision, using the with-zero representation described in the documentation at the beginning of this class.
      Parameters:
      f - A value in the range [-(2 ^ (b - 1)) + 1, (2 ^ (b -1)) - 1]
      b - A value in the range [2, 32]
      Returns:
      A floating point value in the range [-1, 1]
    • toSignedNormalizedWithZero

      public static int toSignedNormalizedWithZero(double x, int b)
      Convert x to fixed-point format using the with-zero representation described in the documentation at the beginning of this class.
      Parameters:
      x - A value in the range [-1, 1]
      b - A value in the range [2, 32]
      Returns:
      A signed normalized fixed-point value with b bits of precision