elm-cardano/float16/1.0.0

Bytes.Floating.Encode

Extra Floating-point binary representation for Elm

float16 : Endianness -> Float -> Encoder

Encode a Float as an IEEE 754 half-precision (16-bit) floating-point number, in the given byte order.

Half-precision layout (16 bits):

   exponent
          |        mantissa
sign      |               |
   |      |               |
   |      |               |
   | /---------\/-------------------\
   *  * * * * *  * * * * * * * * * *  (16-bit)

Depending on the exponent e:

e in [1..30] : normal    →  (-1)^s * 2^(e-15)   * (1 + m/1024)
e == 0, m/=0 : subnormal →  (-1)^s * 2^(-14)    * (0 + m/1024)
e == 0, m==0 : zero      →  +/- 0.0
e == 31, m==0: infinity  →  +/- Infinity
e == 31, m/=0: NaN

Representable range: ~6.0e-8 (smallest subnormal) to 65504 (largest finite). Values outside this range are flushed to zero or infinity respectively.

The encoding uses pure arithmetic (frexp-style decomposition) rather than bit reinterpretation through Bytes, making it ~35x faster than the float32-roundtrip approach.