32位到16位浮点转换 [英] 32-bit to 16-bit Floating Point Conversion
问题描述
我需要一个能够在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.
这篇关于32位到16位浮点转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!