无法为浮点值保存到一个位域结构 [英] Unable to save a float value to a bitfield structure
问题描述
我有一个结构
struct {
u32 var1 :7;
u32 var2 :4;
u32 var3 :4;
u32 var4 :1;
u32 var5 :4;
u32 var6 :7;
u32 var7 :4;
u32 var8 :1;
} my_struct;
my_struct struct1[10];
for(int i=0;i<10; i++)
{
// left some portion
struct1[i].var5= x;// where x is a float value retrieved from a database with sqlapi++ asDouble()
cout<<"Value of x from db is:\t"<<x; // prints 0.1 if it is stored, prints 2.4 if 2.4 is fed
cout<<"Value of x stored in struct1 is:\t"<<struct1[i].var5; // prints 0 instead of 0.1, prints 2 instead of 2.4
}
我要存储浮点值0.1一样,3.4,0.8 VAR5。但我不能这样做。有人可以帮我我怎么能解决这个问题?
I want to store floating point values like 0.1, 3.4, 0.8 in var5. But i am unable to do so. Can somebody help me how could i fix this problem?
推荐答案
VAR5
是可以保存值0..15一个4位无符号整数(上一个工作合理的假设,即 U32
是东西的代名词像 unsigned int类型
)。
var5
is a 4-bit unsigned integer that can hold values 0..15 (working on a reasonable assumption that u32
is a synonym for something like unsigned int
).
您不能分数值存储在里面。 0.1
和 0.8
将被存储为 0
; 3.4
将被存储为 3
。
You can't store fractional values in it. 0.1
and 0.8
would be stored as 0
; 3.4
would be stored as 3
.
如果你真的想分数,你将不得不解决如何重新present他们(定点算术)。
If you really want fractions, you're going to have to work out how to represent them (fixed point arithmetic).
这篇关于无法为浮点值保存到一个位域结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!