command-linefloating-pointbinary-datahalf-precision-floatbfloat16

How do I print the half-precision / bfloat16 values from in a (binary) file?


This is a variant of:

How to print float value from binary file in shell?

in that question, we wanted to print IEEE 754 single-precision (i.e. 32-bit) floating-point values from a binary file.

Now suppose that I want to print half-precision (i.e. 16-bit) floats. od doesn't seem to like doing this:

$ od -t f2 c.bin
od: invalid type string ‘f2’;
this system doesn't provide a 2-byte floating point type

and neither does perl's pack...

Bonus points if your answer also covers binary files with bfloat16 (also 16-bit) values.


Solution

  • GNU coreutils will add support for this in version 9.5, with the -tfH and -tfB types respectively:

    $ printf '\x3F\x80\x00\x00' | od -An --endian=big -tfH -tf2 -tfB -tfF
               1.875               0
               1.875               0
                   1               0
                                   1