32-bit to 16-bit Floating Point Conversion(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 位浮点转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:32 位到 16 位浮点转换
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
