Paul Eggert wrote: > It is referring to the behavior of the VAX floating point unit, where the > hardware representation that one thinks would represent -0.0 behaves > sort of like a NaN. To avoid this problem, on a VAX copysign(0, -1) > returns +0.0, not the usual -0.0. That is why the C and POSIX > standards are worded the way they are.
Thanks for explaining. I have submitted the attached change request to the Linux man pages maintainer. > I suggested copysign instead of the signbit macro because copysign has > been around a lot longer -- it was in the 1985 IEEE floating point > standard -- and is optimized better. But if you want to play it safer > (and slower :-) you can use signbit. Ah, there is also signbit()! Thanks for mentioning it. It's optimized better (here with gcc-4.3 on i386): ----------------------------------------------------------------------- #include <math.h> int using_signbit (long double x) { return signbit (x); } int using_copysign (long double x) { return copysign (1.0L, x) < 0; } ----------------------------------------------------------------------- is compiled into using_signbit: movl 12(%esp), %eax andl $32768, %eax ret using_copysign: subl $12, %esp fldt 16(%esp) fstpl (%esp) movl 4(%esp), %edx fld1 testl %edx, %edx jns .L2 fstp %st(0) fld1 fchs .L2: fldz fucompp fnstsw %ax testb $69, %ah sete %al addl $12, %esp movzbl %al, %eax ret --- man-pages-2.43/man3/copysign.3.bak 2005-10-19 09:07:02.000000000 +0200 +++ man-pages-2.43/man3/copysign.3 2007-04-01 01:02:21.000000000 +0200 @@ -26,7 +26,8 @@ .\" 386BSD man pages .\" Modified 1993-07-24 by Rik Faith ([EMAIL PROTECTED]) .\" Modified 2002-08-10 by Walter Harms ([EMAIL PROTECTED]) -.TH COPYSIGN 3 2002-08-10 "GNU" "Linux Programmer's Manual" +.\" Modified 2007-03-31 by Bruno Haible <[EMAIL PROTECTED]> +.TH COPYSIGN 3 2007-03-31 "GNU" "Linux Programmer's Manual" .SH NAME copysign, copysignf, copysignl \- copy sign of a number .SH SYNOPSIS @@ -46,7 +47,8 @@ that of \fIx\fP, but whose sign matches that of \fIy\fP. If \fIx\fP is a NaN, then a NaN with the sign of \fIy\fP is returned. .SH NOTES -The \fBcopysign\fP() functions may treat a negative zero as positive. +On architectures where the floating-point formats are not IEEE 754 compliant, +the \fBcopysign\fP() functions may treat a negative zero as positive. .SH "CONFORMING TO" C99, 4.3BSD. This function is defined in IEC 559 (and the appendix with