32 位到 16 位浮点转换 [英] 32-bit to 16-bit Floating Point Conversion

查看:41
本文介绍了32 位到 16 位浮点转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个跨平台的库/算法来在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆