Class NFPSignedDoubleLong
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 double
fromSignedNormalizedWithoutZero
(long f, int b) Convertf
to floating point format.static double
fromSignedNormalizedWithZero
(long f, int b) Convertf
to floating point format.static long
toSignedNormalizedWithoutZero
(double x, int b) Convertx
to fixed-point format using thewithout-zero
representation described in the documentation at the beginning of this class.static long
toSignedNormalizedWithZero
(double x, int b) Convertx
to fixed-point format using thewith-zero
representation described in the documentation at the beginning of this class.
-
Method Details
-
fromSignedNormalizedWithoutZero
public static double fromSignedNormalizedWithoutZero(long f, int b) Convertf
to floating point format.f
is assumed to be an signed fixed-point value withb
bits of precision, using thewithout-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 long toSignedNormalizedWithoutZero(double x, int b) Convertx
to fixed-point format using thewithout-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(long f, int b) Convertf
to floating point format.f
is assumed to be an signed fixed-point value withb
bits of precision, using thewith-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 long toSignedNormalizedWithZero(double x, int b) Convertx
to fixed-point format using thewith-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
-