compiler/library behavior differences?
W. Carlisle
carliwh@auburn.edu
Tue Oct 17 17:26:00 GMT 2006
The following C program produces different results with Sun compiler,
ICC compiler and GCC compiler. Is it a rounding error due to
temporaries? Or is it in the printf library?
The other compilers seem to handle this better. I get same result
with cygwin and on linux, and believe both to be GCC.
#include <stdio.h>
#include <math.h>
int main() {
double d = log(8.0)/log(2.0);
printf("%d\n", (int) (d));
//prints 3
printf("%lf\n",log(8.0)/log(2.0));
//prints 3.000000
printf("%d\n", (int) ( log(8.0)/log(2.0)));
//prints 2
printf("%d\n", (int) (d=log(8.0)/log(2.0)));
//prints 2
printf("%d\n", (int) d);
//prints 3
}
Sun and ICC compilers give 3,3.000000,3,3,3
Linux compiler is GCC 3.4.4 20050721 RedHat 3.4.4-2
gcc prog.c -lm
More information about the Gcc-bugs
mailing list