Class Binary16
Functions to convert values to/from the binary16
format
specified in IEEE 754 2008
.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The bias value used to offset the encoded exponent.static final char
The encoded form of negative infinity-∞
.static final char
The encoded form of negative zero-0
.static final char
The encoded form of positive infinity∞
.static final char
The encoded form of positive zero0
. -
Method Summary
Modifier and TypeMethodDescriptionstatic char
static boolean
isInfinite
(char k) static boolean
isNaN
(char k) static char
packDouble
(double k) Convert a double precision floating point value to a packedbinary16
value.static char
packFloat
(float k) Convert a single precision floating point value to a packedbinary16
value.static char
Encode the unbiased exponente
.static char
packSetSignificandUnchecked
(int s) Encode the significands
.static char
packSetSignUnchecked
(int s) Encode the sign bits
.static String
toRawBinaryString
(char k) Show the given raw packedbinary16
value as a string of binary digits.static double
unpackDouble
(char k) Convert a packedbinary16
valuek
to a double-precision floating point value.static float
unpackFloat
(char k) Convert a packedbinary16
valuek
to a single-precision floating point value.static int
unpackGetExponentUnbiased
(char k) Extract and unbias the exponent of the given packedbinary16
value.static int
unpackGetSign
(char k) Retrieve the sign bit of the given packedbinary16
value, as an integer in the range[0, 1]
.static int
unpackGetSignificand
(char k) Return the significand of the given packedbinary16
value as an integer in the range[0, 1023]
.
-
Field Details
-
NEGATIVE_INFINITY
public static final char NEGATIVE_INFINITYThe encoded form of negative infinity-∞
. -
POSITIVE_INFINITY
public static final char POSITIVE_INFINITYThe encoded form of positive infinity∞
. -
POSITIVE_ZERO
public static final char POSITIVE_ZEROThe encoded form of positive zero0
. -
NEGATIVE_ZERO
public static final char NEGATIVE_ZEROThe encoded form of negative zero-0
. -
BIAS
public static final int BIASThe bias value used to offset the encoded exponent. A given exponente
is encoded as
.BIAS
+ e
-
-
Method Details
-
exampleNaN
public static char exampleNaN()- Returns:
- One possible not-a-number value.
-
isInfinite
public static boolean isInfinite(char k) - Parameters:
k
- A packedbinary16
value- Returns:
true
if the given packedbinary16
value is infinite.
-
isNaN
public static boolean isNaN(char k) - Parameters:
k
- A packedbinary16
value- Returns:
true
if the given packedbinary16
value is not a number (NaN
).
-
packDouble
public static char packDouble(double k) Convert a double precision floating point value to a packed
binary16
value.For the following specific cases, the function returns:
NaN
iffisNaN(k)
POSITIVE_INFINITY
iffk ==
Double.POSITIVE_INFINITY
NEGATIVE_INFINITY
iffk ==
Double.NEGATIVE_INFINITY
NEGATIVE_ZERO
iffk == -0.0
POSITIVE_ZERO
iffk == 0.0
Otherwise, the
binary16
value that most closely representsk
is returned. This may obviously be an infinite value as the interval of double precision values is far larger than that of thebinary16
type.- Parameters:
k
- A floating point value- Returns:
- A packed
binary16
value - See Also:
-
unpackDouble
public static double unpackDouble(char k) Convert a packed
binary16
valuek
to a double-precision floating point value.The function returns:
NaN
iffisNaN(k)
Double.POSITIVE_INFINITY
iffk ==
POSITIVE_INFINITY
Double.NEGATIVE_INFINITY
iffk ==
NEGATIVE_INFINITY
-0.0
iffk ==
NEGATIVE_ZERO
0.0
iffk ==
POSITIVE_ZERO
(-1.0 * n) * (2 ^ e) * 1.s
, for the decoded signn
ofk
, the decoded exponente
ofk
, and the decoded significands
ofk
.
- Parameters:
k
- A packedbinary16
value- Returns:
- A floating point value
- See Also:
-
unpackFloat
public static float unpackFloat(char k) Convert a packed
binary16
valuek
to a single-precision floating point value.The function returns:
NaN
iffisNaN(k)
Double.POSITIVE_INFINITY
iffk ==
POSITIVE_INFINITY
Double.NEGATIVE_INFINITY
iffk ==
NEGATIVE_INFINITY
-0.0
iffk ==
NEGATIVE_ZERO
0.0
iffk ==
POSITIVE_ZERO
(-1.0 * n) * (2 ^ e) * 1.s
, for the decoded signn
ofk
, the decoded exponente
ofk
, and the decoded significands
ofk
.
- Parameters:
k
- A packedbinary16
value- Returns:
- A floating point value
- See Also:
-
packFloat
public static char packFloat(float k) Convert a single precision floating point value to a packed
binary16
value.For the following specific cases, the function returns:
NaN
iffisNaN(k)
POSITIVE_INFINITY
iffk ==
Float.POSITIVE_INFINITY
NEGATIVE_INFINITY
iffk ==
Float.NEGATIVE_INFINITY
NEGATIVE_ZERO
iffk == -0.0
POSITIVE_ZERO
iffk == 0.0
Otherwise, the
binary16
value that most closely representsk
is returned. This may obviously be an infinite value as the interval of single precision values is far larger than that of thebinary16
type.- Parameters:
k
- A floating point value- Returns:
- A packed
binary16
value - See Also:
-
packSetExponentUnbiasedUnchecked
public static char packSetExponentUnbiasedUnchecked(int e) Encode the unbiased exponent
e
. Values should be in the range[-15, 16]
- values outside of this range will be truncated.- Parameters:
e
- An exponent- Returns:
- A packed exponent
- See Also:
-
packSetSignificandUnchecked
public static char packSetSignificandUnchecked(int s) Encode the significand
s
. Values should be in the range[0, 1023]
. Values outside of this range will be truncated.- Parameters:
s
- A significand- Returns:
- A packed significand
- See Also:
-
packSetSignUnchecked
public static char packSetSignUnchecked(int s) Encode the sign bit
s
. Values should be in the range[0, 1]
, with0
ironically denoting a positive value. Values outside of this range will be truncated.- Parameters:
s
- A sign bit- Returns:
- A packed sign bit
- See Also:
-
toRawBinaryString
Show the given raw packedbinary16
value as a string of binary digits.- Parameters:
k
- A packedbinary16
value- Returns:
- A string representation
-
unpackGetExponentUnbiased
public static int unpackGetExponentUnbiased(char k) Extract and unbias the exponent of the given packed
binary16
value.The exponent is encoded biased as a number in the range
[0, 31]
, with0
indicating that the number is subnormal and[1, 30]
denoting the actual exponent plusBIAS
. Infinite andNaN
values always have an exponent of31
.This function will therefore return:
-
0 -
iff the input is a subnormal number.BIAS
= -15 - An integer in the range
[1 -
iff the input is a normal number.BIAS
, 30 -BIAS
] = [-14, 15] -
16
iff the input isPOSITIVE_INFINITY
,NEGATIVE_INFINITY
, orNaN
.
- Parameters:
k
- A packedbinary16
value- Returns:
- The unbiased exponent
- See Also:
-
-
unpackGetSign
public static int unpackGetSign(char k) Retrieve the sign bit of the given packedbinary16
value, as an integer in the range[0, 1]
.- Parameters:
k
- A packedbinary16
value- Returns:
- An unpacked sign bit
- See Also:
-
unpackGetSignificand
public static int unpackGetSignificand(char k) Return the significand of the given packed
binary16
value as an integer in the range[0, 1023]
.- Parameters:
k
- A packedbinary16
value- Returns:
- An unpacked significand
- See Also:
-