David Kirkby <david.kir...@onetel.net> added the comment:

Hi Mark, 

Since 'copysign' is in the maths library, I would not expect the link phase to 
fail. Solaris does not ship with different maths libraries for C99 (one just 
links to libm). 

However, I would not be surprised if the behavior was ill defined if the 
compiler is not C99. Certainly header files behave differently on Solaris 
depending on the mode of the compiler. For example, trying to use the INFINITY 
macro when the compiler is not C99 seems to work on Linux, but fails on Solaris 
unless you force C99 mode with gcc -std=c99. 

The following bit of code gives the same results whether one uses 'gcc' or 'gcc 
-std=c99' on OpenSolaris or Linux. However, if one uses 'gcc -ansi' then the 
behavior is totally different. 

drkir...@hawk:~$ cat cs.c
#include <stdio.h>
#include <math.h>


int main(int argc, char **argv) { 
   double x, y;

   /* Set x and y differently if a command line arguement is given. 
   This will avoid the compiler optimising the values out, as they
   will not be known in advance. */ 
   if (argc==1) { /* This will stop compiler optimising 0.0 out x */
      x=1.0; 
      y=0.0;
   } else {
      x=2.0;
      y=-0.0;
   }
   printf("copysign(%lf,%lf)=%lf\n", x, y, copysign(x, y));
}

drkir...@hawk:~$ gcc -lm cs.c 
drkir...@hawk:~$ ./a.out 
copysign(1.000000,0.000000)=1.000000
drkir...@hawk:~$ ./a.out  z
copysign(2.000000,-0.000000)=-2.000000
drkir...@hawk:~$ gcc -lm -std=c99 cs.c 
drkir...@hawk:~$ ./a.out 
copysign(1.000000,0.000000)=1.000000
drkir...@hawk:~$ ./a.out  z
copysign(2.000000,-0.000000)=-2.000000

Note how -ansi screws it up completely

drkir...@hawk:~$ gcc -lm -ansi cs.c 
drkir...@hawk:~$ ./a.out  
copysign(1.000000,0.000000)=0.000000
drkir...@hawk:~$ ./a.out  z
copysign(2.000000,-0.000000)=0.000000

I also tried it on a Sun SPARC running a recent version of Solaris (2009 
release). Again the results are the same. 

I then tried it on a Solaris box running the first release of Solaris 10 
(03/2005). Then one gets even stranger behavior if one defines -ansi, where the 
results are almost right, but with poor rounding errors. 

drkir...@redstart:~$ gcc -ansi -lm cs.c
drkir...@redstart:~$ ./a.out
copysign(1.000000,0.000000)=1.000001
drkir...@redstart:~$ ./a.out d
copysign(2.000000,-0.000000)=-2.000002

But in C99 mode, it works fine. 

drkir...@redstart:~$ gcc -std=c99  -lm cs.c
drkir...@redstart:~$ ./a.out
copysign(1.000000,0.000000)=1.000000
drkir...@redstart:~$ ./a.out d
copysign(2.000000,-0.000000)=-2.000000

So I draw two conclusions. 


1) 'copysign' is in the maths library, so a program which tries to link to 
'copysign' will succeed. 

2) The behavior of 'copysign' is ill defined unless the compiler is a C99 
compiler. 

I don't think you should use copysign unless the compiler is C99. Trying to 
come up with a test for 'copysign' working is probably an impossible task, as 
it undefined. So you could try 99 different values of x and y and they all 
work, but its anyone guess what will happen with the 100th set of values. 

Dave

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue9069>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to