双重奇怪的位腐败 [英] Strange bit corruption in a double
问题描述
我在双重中得到了一个非常奇怪的一点腐败。我在英特尔
Red Hat Linux上。 uname -a返回:
Linux foo.com 2.6.9-34.0.2.ELsmp#1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU / Linux
我有一个双设置为0.00的变量。然后会出现一些数字
,然后,当我用printf(%f)打印这个变量
时,我得到0.00000。
然而,当我比较
如果(变量== 0.0),我得到假。
和if(变量0.0),我得到是的。
然后我运行了一个小函数来打印这个变量的位,并且
发现它的位模式很奇怪:
printf = 0.000000000000000
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000
任何想法??????
FWIW,我知道打印双b / b
位模式的功能是正确的:
>
void print_binary_double(double value)
{
unsigned char * a;
a =(unsigned char *)& value ;
int bytes = sizeof(double);
for(int i = 0; i< bytes; i ++){
print_binary_uc(* a);
printf(" ; );
a ++;
}
printf(" \ n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char)* 8;
for(i = len-1; i> = 0; i--)
{
value2 =价值& ((unsigned char)1<< i);
printf("%d",value2?1:0);
}
}
I''m getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux
I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.
However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.
I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:
printf = 0.000000000000000
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000
Any ideas??????
FWIW, I know the function to print the bit pattern of the double
is correct:
void print_binary_double(double value)
{
unsigned char *a;
a = (unsigned char *)&value;
int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc(*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
推荐答案
" Digital Puer" < di ********** @ hotmail.comwrote in message
news:11 ******************** **@v45g2000cwv.googlegr oups.com ...
"Digital Puer" <di**********@hotmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
我在双重中遇到了非常奇怪的一点腐败。我在英特尔
Red Hat Linux上。 uname -a返回:
Linux foo.com 2.6.9-34.0.2.ELsmp#1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU / Linux
我有一个双设置为0.00的变量。然后会出现一些数字
,然后,当我用printf(%f)打印这个变量
时,我得到0.00000。
然而,当我比较
如果(变量== 0.0),我得到假。
和if(变量0.0),我得到真正。
I''m getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux
I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.
However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.
我没有分析你提供的位模式,但你提供的信息是b
不一致腐败。
假设数字是正数,但非常小(让我们说10 ^( - 30))。
然后没有版本的printf具有实际的小数位数将
显示除零之外的任何内容。另外,它会像你上面指出的那样测试为正面。
为了打印这个数字,你要使用%e ;而不是%f
格式说明符。
大多数二进制科学记数法(即浮点数,双精度)格式包含
二进制指数,我怀疑在这种情况下是一个非常负数。
数字中设置了很多1的事实并不矛盾
,正数非常小。
尝试%e,并发布结果......
I haven''t analyzed the bit pattern you provided, but the information you''ve
presented isn''t consistent with "corruption".
Assume that the number is positive, but very small (let''s say 10^(-30)).
Then no version of printf with a practical number of decimal places will
show anything but zero. Additionally, it would test as positive as you
indicated above.
In order to print this number, you''d to use the "%e" rather than the "%f"
format specifier.
Most binary scientific notation (i.e. float, double) formats contain a
binary exponent, which I suspect in this case is a very negative number.
The fact that there are a lot of "1"s set in the number are not inconsistent
with a very small positive number.
Try "%e", and post your results ...
" Digital Puer" < di ********** @ hotmail.comwrote in message
news:11 ******************** **@v45g2000cwv.googlegr oups.com ...
"Digital Puer" <di**********@hotmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
我在双重中遇到了非常奇怪的一点腐败。我在英特尔
Red Hat Linux上。 uname -a返回:
Linux foo.com 2.6.9-34.0.2.ELsmp#1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU / Linux
我有一个双设置为0.00的变量。然后会出现一些数字
,然后,当我用printf(%f)打印这个变量
时,我得到0.00000。
然而,当我比较
如果(变量== 0.0),我得到假。
和if(变量0.0),我得到是的。
然后我运行了一个小函数来打印这个变量的位,并且
发现它的位模式很奇怪:
printf = 0.000000000000000
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000
任何想法??????
FWIW,我知道打印双b / b
位模式的功能是正确的:
>
void print_binary_double(double value)
{
unsigned char * a;
a =(unsigned char *)& value ;
int bytes = sizeof(double);
for(int i = 0; i< bytes; i ++){
print_binary_uc(* a);
的printf(QUOT; );
a ++;
}
printf(" \ n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char)* 8;
for(i = len-1; i> = 0; i--)
{
value2 =价值& ((unsigned char)1<< i);
printf("%d",value2?1:0);
}
}
I''m getting a very weird bit corruption in a double. I am on an Intel
Red Hat Linux box. uname -a returns:
Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006
i686 i686 i386 GNU/Linux
I have a "double" variable that is set to 0.00. Some number
crunching then occurs, and later on, when I printf this variable
with printf("%f"), I am getting 0.00000.
However, when I compare
if (variable == 0.0), I get false.
and if (variable 0.0), I get true.
I then ran a small function to print the bits of this variable and
found that its bit pattern is quite odd:
printf = 0.000000000000000
bits = 11001000 00010100 00010100 00001001 10001100 00000010 10111110
00000000
Any ideas??????
FWIW, I know the function to print the bit pattern of the double
is correct:
void print_binary_double(double value)
{
unsigned char *a;
a = (unsigned char *)&value;
int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc(*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
我敢打赌你的格式说明符需要tweeking。来源看起来很邋
。 LS
I''ll bet your format specifier needs tweeking. The source is sloppy-looking
too. LS
Lane Straatman说:
Lane Straatman said:
>
" Digital Puer" ; < di ********** @ hotmail.comwrote in message
news:11 ******************** **@v45g2000cwv.googlegr oups.com ...
>
"Digital Puer" <di**********@hotmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
< snip>
<snip>
> void print_binary_double(double value)
{
unsigned char * a;
a =(unsigned char *)& value;
int bytes = sizeof(double);
for(int i = 0; i< bytes; i ++){
print_binary_uc(* a);
printf("");
a ++;
}
printf(" \ n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char)* 8;
for(i = len-1; i> = 0; i - )
{
value2 = value& ((unsigned char)1<< i);
printf("%d",value2?1:0);
}
}
>void print_binary_double(double value)
{
unsigned char *a;
a = (unsigned char *)&value;
int bytes = sizeof(double);
for (int i = 0; i < bytes; i++) {
print_binary_uc(*a);
printf(" ");
a++;
}
printf("\n");
}
void print_binary_uc(unsigned char value)
{
unsigned char value2;
int i;
int len = sizeof(unsigned char) * 8;
for (i = len-1; i >= 0; i--)
{
value2 = value & ((unsigned char)1 << i);
printf("%d", value2 ? 1 : 0);
}
}
我敢打赌你的格式说明符需要tweeking。消息来源也很好看。 LS
I''ll bet your format specifier needs tweeking. The source is
sloppy-looking too. LS
您如何改善来源?
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
How would you improve the source?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于双重奇怪的位腐败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!