存储固定范围浮点数的有效方法

发布于 2025-01-05 07:07:22 字数 168 浏览 5 评论 0原文

我正在举起一个(大)浮点数数组,每个浮点数占用 4 个字节。

鉴于我的浮点数范围在 0 到 255 之间,有没有办法将每个浮点数存储在小于 4 个字节中?

我可以对整个数组进行任意数量的计算。

我用的是C。

I'm heaving an (big) array of floats, each float takes 4 bytes.

Is there a way, given the fact that my floats are ranged between 0 and 255, to store each float in less than 4 bytes?

I can do any amount of computation on the whole array.

I'm using C.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙与沫 2025-01-12 07:07:22

您需要多少精度?

您可以将每个浮点存储在 2 个字节中,方法是将其表示为 unsigned Short(范围从 0 到 65,535),并在需要实际值时将所有值除以 2^8 。这本质上与使用定点格式而不是浮点相同。

但是,当您执行此操作时,您的精度将限制为 1.0 / (2^8) = 0.00390625

How much precision do you need?

You can store each float in 2 bytes by representing it as an unsigned short (ranges from 0 to 65,535) and dividing all values by 2^8 when you need the actual value. This is essentially the same as using a fixed point format instead of floating point.

Your precision is limited to 1.0 / (2^8) = 0.00390625 when you do this, however.

对你而言 2025-01-12 07:07:22

数据的绝对范围实际上并不那么重要,重要的是您需要的精度。如果您可以达到 6 位精度,那么您只需要存储 1-1000000 之间的整数所需的存储空间,即 20 位。因此,假设这种情况,您可以做的是:

1) 移动数据,使最小元素的值为 0。即从每个元素中减去一个值。记录这个转变。

2)将数据缩放(乘以)一个足够大的数字,以便在截断为整数后,不会丢失所需的任何精度。

3) 现在这可能很棘手,除非您可以将数据打包成方便的 8 位或 16 位单元——将数据打包成连续的无符号整数。在此示例中,每个数据值都需要 20 位,因此值 1 占用整数 1 的前 20 位,值 2 占用整数 1 的剩余 12 位和整数 2 的前 8 位,依此类推。在这个假设的情况下,您最终节省了约 40%。

4)现在,“解密”。解压缩这些值(您已保存每个值中的位数)、取消缩放和取消移位。

因此,这可以做到这一点,并且可能比标准压缩算法更快、更紧凑,因为它们不允许假设您需要多少精度,但您可以。

The absolute range of your data doesn't really matter that much, it's the amount of precision you need. If you can get away with e.g. 6 digits of precision, then you only need as much storage as would be required to store the integers from 1-1000000, and that's 20 bits. So, supposing this, what you can do is:

1) Shift your data so that the smallest element has value 0. I.e. subtract a single value from every element. Record this shift.

2) Scale (multiply) your data by a number just large enough so that after truncation to an integer, you will not lose any precision you need.

3) Now this might be tricky unless you can pack your data into convenient 8- or 16-bit units--pack the data into successive unsigned integers. Each one of your data values needs 20 bits in this example, so value 1 takes up the first 20 bits of integer 1, value 2 takes up the remaining 12 bits of integer 1 and the first 8 bits of integer 2, and so on. In this hypothetical case you end up saving ~40%.

4) Now, 'decrypting'. Unpack the values (you have saved the # of bits in each one), un-scale, and un-shift.

So, this will do it, and might be faster and more compact than standard compression algorithms, as they aren't allowed to make assumptions about how much precision you need, but you are.

紫轩蝶泪 2025-01-12 07:07:22

例如,您可以在一个字节上存储整数(带有 .0 的浮点数),但另一个浮点数需要更多字节。

如果您不担心精度,您也可以使用定点......

For example you could store integers (floats with .0) on one byte, but the other float need more bytes.

You could also use fixed-point if you don't worry about precision...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文