Search code examples
cfloating-pointtype-conversionfixed-size-types

Is there a type with fixed size of two bytes for floating points in c?


I need something like uint16_t but for floating point numbers instead of integer. I need to be able to convert a float to it so I can transfer it and convert back to float later (obviously I will lose some precision). Is there a simple solution for this or should I do it manually?


Solution

  • There is no such thing in the C standard. Some compilers do have __fp16.

    You can use Q numbers, but these are limited in a fixed range.

    If you really need floating point, with the exponent, then you should implement the ieee standard half precision.

    Regular artimetics work on the Q numbers. You should write your own arithmetic for the half precision. Unless your compiler support it.

    Or go open source.