将浮子转换为n x位的数组,然后从n x位的数组回到浮点?

发布于 2025-02-09 21:39:59 字数 956 浏览 1 评论 0原文

我正在寻找一种编码浮点值作为可以指定位长度的位数组的方法。

我找到了使用示例代码进行半精度浮点对话的示例代码编码32位浮点的方法://stackoverflow.com/questions/37759848/convert-byte-arreay-to-16-bits-float/37761168#37761168“>此答案。它适用于将浮子编码为16位二进制数据。

使用该编码,我能够将吞吐量降低50%,而质量却没有明显损失。但是我真的想知道是否可以将浮子编码为12位二进制阵列,甚至10位?我真的很感兴趣,即使指定精度的确切数量。可变精度可能吗?性能是一个因素吗?

这是两个理想函数的示例,它们将浮子编码到字节数组中并返回浮点,但允许指定字节数组中的位精度:

//sudo code:

//Encoding:
n = 12; //12 bits precision (number of bits to represent the float) 
float myFloat = 7.777777; //the floating point value to encode as 12 bit array
bytes = ConvertFloatToBytes(myFloat, n); //encode

//Decoding:
float myFloat = ConvertBytesToFloat(bytes, n); //decode
print(myFloat); //the decoded float value 7.777000 (some loss..)

我对将浮子转换为位,反之亦然,反之亦然。浏览IEEE 754-2008示例代码绝对是我的理解或修改它的能力。搜索结果也不是很丰富的信息,我想继续研究或尝试原型一个函数,但我不确定从哪里开始。

我希望有人可以提供建议,甚至可能是关于如何在C#中编码float 的解决方案,作为长度为n的位数组,然后返回浮点?

I am looking for a way to encode a float value as a array of bits where the length of bits can be specified.

I have found ways of encoding a 32 bit float to 16 bit byte array using the sample code for Half-precision floating point conversation according to IEEE 754-2008 standard that I found from this answer. It works wonderfully for encoding floats as 16 bit binary data.

Using that encoding I was able to reduce the throughput by 50% without any noticeable loss in quality. But I really would like to know if its possible to encode a float as a 12 bit binary array, or even 10 bit? I'd really be interested in a way to even specify the exact number of bits of precision. Is variable precision possible? Would performance be a factor?

Here's a example of two ideal functions that encode a float into a byte array and back to float but allow specifying the precision of the bits within the byte array:

//sudo code:

//Encoding:
n = 12; //12 bits precision (number of bits to represent the float) 
float myFloat = 7.777777; //the floating point value to encode as 12 bit array
bytes = ConvertFloatToBytes(myFloat, n); //encode

//Decoding:
float myFloat = ConvertBytesToFloat(bytes, n); //decode
print(myFloat); //the decoded float value 7.777000 (some loss..)

I have little knowledge regarding converting floats to bits and vice versa. Glancing at the IEEE 754-2008 sample code was definitely beyond my understanding or ability to modify it. Search results have not been very informative either, I want to continue researching or try to prototype a function but I'm not sure where to begin.

I was hoping someone could provide advice or even possibly a solution on how to encode a float in c# as a array of bits of length n, then array back to a float?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文