Class NFPSignedFloatInt
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 Summary
Modifier and TypeMethodDescriptionstatic floatfromSignedNormalizedWithoutZero(int f, int b) Convertfto floating point format.static floatfromSignedNormalizedWithZero(int f, int b) Convertfto floating point format.static inttoSignedNormalizedWithoutZero(float x, int b) Convertxto fixed-point format using thewithout-zerorepresentation described in the documentation at the beginning of this class.static inttoSignedNormalizedWithZero(float x, int b) Convertxto fixed-point format using thewith-zerorepresentation described in the documentation at the beginning of this class.
-
Method Details
-
fromSignedNormalizedWithoutZero
public static float fromSignedNormalizedWithoutZero(int f, int b) Convertfto floating point format.fis assumed to be an signed fixed-point value withbbits of precision, using thewithout-zerorepresentation 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(float x, int b) Convertxto fixed-point format using thewithout-zerorepresentation 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
bbits of precision
-
fromSignedNormalizedWithZero
public static float fromSignedNormalizedWithZero(int f, int b) Convertfto floating point format.fis assumed to be an signed fixed-point value withbbits of precision, using thewith-zerorepresentation 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(float x, int b) Convertxto fixed-point format using thewith-zerorepresentation 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
bbits of precision
-