lofi-converters.Rmd
Underneath pack()
an unpack()
is a suite of low-level functions for handling each particular supported type
dbl_to_lofi()
, lofi_to_dbl()
int32_to_lofi()
, lofi_to_int32()
hex_colour_to_lofi()
, lofi_to_hex_colour()
lgl_to_lofi()
, lofi_to_lgl()
choice_to_lofi()
, lofi_to_choice()
Double precision floating point values are converted to low-fidelity representation by truncating the mantissa, and re-encoding the exponent. Low-fidelity floats have limited range, poorer precision, and will almost never give back the exact starting value when unpack()ed
.
The following converts a double into a 10 bit float (with a sign bit, 2-bit exponent and 7-bit mantissa). The reconstructed double is close to the original value, but not an exact match.
Representation | Bits | Value | Bit layout |
---|---|---|---|
Double precision | 64 | -1.234 | |
Lofi double dbl_to_lofi(-1.234, float_bits = c(1, 2, 7))
|
10 | 669L | |
Reconstructed double lofi_to_dbl(669L, float_bits = c(1, 2, 7))
|
64 | -1.226562 |
Representation | Bits | Value | Colour sample or bit layout |
---|---|---|---|
Original colour | 24 | #123456 | |
Low-fidelity colour hex_colour_to_lofi('#123456', rgb_bits = c(3, 3, 2)))
|
8 | 5L | |
Reconstructed colour lofi_to_hex_colour(5L, rgb_bits = c(3, 3, 2))
|
24 | #002455 |
lofi
correctly keeps the sign bit and twos-complement for negative valuesRepresentation | Bits | Value | bit layout |
---|---|---|---|
Original integer | 32 | -12 | |
Low-fidelity integer int32_to_lofi(-12L, nbits = 5, signed = TRUE)
|
5 | 20L | |
Reconstructed integer lofi_to_int32(20, nbits = 5, signed = TRUE)
|
32 | -12 |