Solaris 10 x86, C, unexpected NaN in simple calculation

Stefan Raaijmakers stefan@decomania.nl
Tue Oct 25 19:48:00 GMT 2005


Hello,

I have, I am afraid, an unreproducable problem. Sometimes I get a -NaN as a 
result in a float. I did not succeed when trying obtain a NaN by doing 
calculations in a infinite loop all night. It happens sometimes in the 
proces. The only difference is that the variables are declared locally and 
are not passed as an argument with a function. But when I print the input 
values when a NaN is calculated they are ok.

We are switching from Solaris 2.6 on Sun hardware (software succesfully used 
with gcc on this platform) to Solaris 10 GA x86 which we run on an Intel 
Pentium 4 computer.

Description of my problem:
I get a Nan when brackets are used (just for readability). But when I add 
the same calculation again in the same function the second one is calculated 
o.k.!?

For example (vars are all floats):

result = weight * (percentage / 100.0);    /* result = NaN */
result = weight * (percentage / 100.0);    /* result = normal value */

When the brackets are left out in the first line:
result = weight * percentage / 100.0;    /* result = normal value */
result = weight * (percentage / 100.0);    /* result = NaN */

And then more weird....:
result = weight * percentage / 100.0;    /* result = normal value */
result = weight * (percentage / 100.0);    /* result = NaN */
result = weight * (percentage / 100.0);    /* result = normal value */
..... at the second line with braces the calculation is o.k.

Since I have trouble with finding the correct compiler optimization options 
I want to ask if there is a recommended set of options which I should use?

We allready had these options:
   -g -Wcast-align

The NaN problem goes away when I add:
  -O1 -ffloat-store

....... but I'm absolutely not sure if this the correct solution, since I'm 
merely guessing (trial on error).

With only the -ffloat-store the problem remained.... ?

When I change the -O1 in -O2 I get problems in a different function. Then an 
input variable is interpreted as a NaN, so the result is also NaN. Is there 
a connection?

I hope that someone can tell me what the best usage for gcc for standard C 
on Solaris 10 on an Intel machine. I use the gcc that was on the companion 
CD provided by Sun, gcc -v gives:

Reading specs from /usr/sfw/lib/gcc/i386-pc-solaris2.10/3.4.3/specs
Configured with: 
/builds/sfw10-gate/usr/src/cmd/gcc/gcc-3.4.3/configure --prefix
=/usr/sfw --with-as=/usr/sfw/bin/gas --with-gnu-as --with-ld=/usr/ccs/bin/ld 
 --w
ithout-gnu-ld --enable-languages=c,c++ --enable-shared
Thread model: posix
gcc version 3.4.3 (csl-sol210-3_4-branch+sol_rpath)

Thank you very very much in advance for any help that will help me 
understand the problem and how to solve it. I have to be sure that I can 
rely on the compiled software. Not that the NaNs will suddenly apear again 
in the future.

With kind regards,


Stefan Raaijmakers
Hapert - The Netherlands 



More information about the Gcc-help mailing list