when I'm trying to compile the program ============================ #include <math.h> static double s = sin(0.0); int main(){ return 0; } ============================ with gcc t.c, the compiler produces the warnings t.c:3: warning: initializer element is not constant t.c:3: warning: (near initialization for ‘s’) t.c:3: warning: ‘s’ defined but not used I'm worried about the first warning, since sin(0) is constant gcc -v generates: ======================================= Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu Thread model: posix gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
No, sin(0) is not constant in C. C has specific rules about constant expression. You can use __builtin_sin(0) to always get the constant folding.
Ok, but __builtin_sin(M_PI) is not constant again
Worse, with __builtin_sin(M_PI), I get an error: test.c:2: error: initializer element is not constant Maybe this is fixed now in GCC 4.3 by using MPFR.
This is an extension that GCC allows some constant folding with some functions. So this is just a missed optimization of not constant folding sin. And yes this is fixed in 4.3.0 by the use of MPFR.