存储非负浮点值 [英] Storing non-negative floating point values
问题描述
是否有一种使用现有 float32
和 float64
格式存储非负浮点值的有效方法?
想象一下默认的 float32
行为,它允许负/正:
val = bytes.readFloat32();
如果不需要负值,是否可以允许更大的正值?
val = bytes.readFloat32() + 0xFFFFFFFF;
基本上当我知道我只存储正值时,可以稍微修改浮点格式,以允许相同数量的位有更大的范围或精度.
例如.float32
格式定义为1 位 符号,8 位 指数,23 位 小数>
如果我不需要符号位怎么办,我们可以8位用于指数,24位用于分数,以便为相同的32位提供更高的精度吗?
浮点数(float32
和 float64
)具有明确的符号位.浮点数不存在无符号整数的等价物.
因此没有简单的方法可以将正浮点数的范围加倍.
Is there an efficient way to store non-negative floating point values using the existing float32
and float64
formats?
Imagine the default float32
behaviour which allows negative/positive:
val = bytes.readFloat32();
Is it possible to allow for greater positive values if negative values are not necessary?
val = bytes.readFloat32() + 0xFFFFFFFF;
Edit: Essentially when I know I'm storing only positive values, the float format could be modified a bit to allow for greater range or precision for the same amount of bits.
Eg. The float32
format is defined as 1 bit for sign, 8 bits for exponent, 23 bits for fraction
What if I don't need the sign bit, can we have 8 bits for exponent, 24 bits for fraction to give greater precision for the same 32 bits?
Floating-point numbers (float32
and float64
) have an explicit sign bit. The equivalent of unsigned integers doesn't exist for floating-point numbers.
So there is no easy way to double the range of positive floating-point numbers.
这篇关于存储非负浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!