F
Felix E. Klee
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) = c;
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;
}
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) = c;
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;
}