32 位到 16 位浮点转换

2021-12-20 00:00:00 networking ieee-754 c++

我需要一个跨平台的库/算法来在 32 位和 16 位浮点数之间进行转换.我不需要用 16 位数字进行数学运算;我只需要减小 32 位浮点数的大小,以便它们可以通过网络发送.我正在使用 C++.

I need a cross-platform library/algorithm that will convert between 32-bit and 16-bit floating point numbers. I don't need to perform math with the 16-bit numbers; I just need to decrease the size of the 32-bit floats so they can be sent over the network. I am working in C++.

我知道我会损失多少精度,但这对我的应用程序来说没问题.

I understand how much precision I would be losing, but that's OK for my application.

IEEE 16 位格式会很棒.

The IEEE 16-bit format would be great.

推荐答案

std::frexp 从普通浮点数或双精度数中提取有效数和指数――然后您需要决定如何处理太大而无法放入半精度浮点数(饱和...?),进行相应调整,并将半精度数放在一起.这篇文章有 C 源代码向您展示如何执行转换.

std::frexp extracts the significand and exponent from normal floats or doubles -- then you need to decide what to do with exponents that are too large to fit in a half-precision float (saturate...?), adjust accordingly, and put the half-precision number together. This article has C source code to show you how to perform the conversion.

相关文章