(双一= 1。)!= 1.? [英] (double one=1.) != 1. ?

查看:76
本文介绍了(双一= 1。)!= 1.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




当我编译下面的程序时

gcc -O0 -o ctest ctest.c

(平台:Intel Celeron(Coppermine),LINUX 2.4.20)我得到

以下输出:


one!= one_alt

one_alt!=一个

1.!= one_alt

one_alt!= 1.


这对我来说看起来很合理。毕竟,one和one_alt没有

相同的二进制表示。当我用

g ++ -O0 -o ctest ctest.c

编译它时,我得到了不同的输出:


一个!= one_alt

one_alt!=一个

1. == one_alt

one_alt == 1.


有人可以告诉我为什么1。和一个使用C ++编译器g ++编译

时似乎有所不同吗?

Felix

程序ctest.c:


#include< stdio.h>


#define compare_a_b(a,b)\

printf(" ;%s%s%s \ n",#a,(a == b)?" ==":"!=",#b)


int main(){

unsigned char c [] = {2,0,0,0,0,02,63};

double one = 1 。,one_alt;

int i;

for(i = 0; i< 8; ++ i)

((unsigned char) *)& one_alt)[i] = c [i];

compare_a_b(one,one_alt);

compare_a_b(one_alt,one);

compare_a_b(1。,one_alt);

compare_a_b(one_alt,1。);


返回0;

}


-

如果要联系我,请不要回复,但要发送邮件给

felix DOT klee AT inka DOT de

否则您的电子邮件可能会被自动删除!

解决方案



" Felix E. Klee" < FE ************* @ gmx.net>在消息新闻中写道:20031201153753.592bf0ad.fe ************* @ gmx.n et ...

unsigned char c [] = {2 ,0,0,0,0,02,23} ;;
对于(i = 0; i <8; ++ i)
((unsigned char *)& one_alt)[i] = c [i];




有我,这一切都是实现依赖,你改变了实现。


On Mon,2003年12月1日10:16:43 -0500 Ron Natalie写道:

unsigned char c [] = {2,0,0,0,0,0,240,63};
double one = 1.,one_alt;
int i;
for(i = 0; i< 8; ++ i)
((unsigned char *)& one_alt)[i] = c [i];





Hm,但不应该是浮点数的二进制表示

独立于编译器?毕竟,我让两个可执行文件运行在

相同的系统上,浮点运算符由FPU处理。在

中,即使在不同的系统上,我也希望得到相同的

输出,只要它们都支持相同的IEEE标准和长期

因为我的程序的行为很好地定义了该标准。


Felix


-

要联系我,不要回复,但要发送邮件给

felix DOT klee AT inka DOT de

否则您的电子邮件可能会被自动删除!




" Felix E. Klee" < FE ************* @ gmx.net>在留言新闻中写道:20031201163638.33fe29ed.fe ************* @ gmx.n et ...

嗯,但不应该是浮点数的二进制表示是否独立于编译器?




表示可能是...但是代码生成的可能性很大对于b $ b,比较可能与不同的编译器不同。


实现依赖意味着实现依赖。

如果你想要详细说明为什么实现不同的东西,尝试询问gcc组的


Hi,

when I compile the program below with
gcc -O0 -o ctest ctest.c
(Platform: Intel Celeron (Coppermine), LINUX 2.4.20) I get the
following output:

one != one_alt
one_alt != one
1. != one_alt
one_alt != 1.

This looks reasonable to me. After all, one and one_alt don''t have the
same binary representation. When I compile it with
g++ -O0 -o ctest ctest.c
I get a different output, though:

one != one_alt
one_alt != one
1. == one_alt
one_alt == 1.

Could someone enlighten me why "1." and "one" seem to be different when
compiled with the C++ compiler g++?

Felix
The program ctest.c:

#include <stdio.h>

#define compare_a_b(a,b) \
printf("%s %s %s\n", #a, (a == b) ? "==" : "!=", #b)

int main() {
unsigned char c[] = {2, 0, 0, 0, 0, 0, 240, 63};
double one = 1., one_alt;
int i;
for (i = 0; i < 8; ++i)
((unsigned char*)&one_alt)[i] = c[i];
compare_a_b(one, one_alt);
compare_a_b(one_alt, one);
compare_a_b(1., one_alt);
compare_a_b(one_alt, 1.);

return 0;
}

--
To contact me off list don''t reply but send mail to
felix DOT klee AT inka DOT de
Otherwise your email to me might get automatically deleted!

解决方案


"Felix E. Klee" <fe*************@gmx.net> wrote in message news:20031201153753.592bf0ad.fe*************@gmx.n et...

unsigned char c[] = {2, 0, 0, 0, 0, 0, 240, 63};
double one = 1., one_alt;
int i;
for (i = 0; i < 8; ++i)
((unsigned char*)&one_alt)[i] = c[i];



Got me, all this is implementation dependent and you change the implementation.


On Mon, 1 Dec 2003 10:16:43 -0500 Ron Natalie wrote:

unsigned char c[] = {2, 0, 0, 0, 0, 0, 240, 63};
double one = 1., one_alt;
int i;
for (i = 0; i < 8; ++i)
((unsigned char*)&one_alt)[i] = c[i];



Got me, all this is implementation dependent and you change the implementation.



Hm, but shouldn''t the binary representation of floating point numbers be
independent of the compiler? After all, I let both executables run on
the same system and floating point arithmetics is handled by the FPU. In
addition, even on different systems I would expect to get the same
output, as long as they both support the same IEEE standard and as long
as the behavior of my program is well defined by that standard.

Felix

--
To contact me off list don''t reply but send mail to
felix DOT klee AT inka DOT de
Otherwise your email to me might get automatically deleted!



"Felix E. Klee" <fe*************@gmx.net> wrote in message news:20031201163638.33fe29ed.fe*************@gmx.n et...

Hm, but shouldn''t the binary representation of floating point numbers be
independent of the compiler?



The representation may be...but it''s quite possible that the code generated to
do the comparisons may be different with different compilers.

Implementation dependent means just that IMPLEMENTATION DEPENDENT.
If you want details as to why the implemetnations do different things, try
asking on a gcc group.


这篇关于(双一= 1。)!= 1.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆