public inbox for gentoo-science@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-science] question about signbit
@ 2005-10-22 18:16 Darren Dale
  2005-10-22 21:05 ` Marco Matthies
  2005-10-23  6:39 ` Ertugrul Soeylemez
  0 siblings, 2 replies; 10+ messages in thread
From: Darren Dale @ 2005-10-22 18:16 UTC (permalink / raw
  To: gentoo-science

Hello,

A while back, SciPy was added to the portage tree. There will be a new version 
of SciPy soon, which will hopefully unify the Numeric/numarray split in the 
Python community.

I am trying to track down the source of a bug in the SciPy development branch 
which, so far, has only been reported on a Gentoo platform (mine). It 
involves the use of the libc math function "signbit". Here is a clip from the 
manpage:

       `signbit' is a generic macro which can work on all real floating-
       point types.  It returns a non-zero value if the value of  X  has
       its sign bit set.

On my system, SciPy's signbit function reports that the sign bit is not set 
for any number, positive or negative. Could someone here help me understand 
how to test the libc signbit function? I have to admit I have no experience 
with C programming.

Thanks,

Darren
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-22 18:16 [gentoo-science] question about signbit Darren Dale
@ 2005-10-22 21:05 ` Marco Matthies
  2005-10-22 21:32   ` Darren Dale
                     ` (2 more replies)
  2005-10-23  6:39 ` Ertugrul Soeylemez
  1 sibling, 3 replies; 10+ messages in thread
From: Marco Matthies @ 2005-10-22 21:05 UTC (permalink / raw
  To: gentoo-science

Darren Dale wrote:
> On my system, SciPy's signbit function reports that the sign bit is not set 
> for any number, positive or negative. Could someone here help me understand 
> how to test the libc signbit function? I have to admit I have no experience 
> with C programming.

Hi Darren,

the signbit fuction is actually a macro (as the manpage says) defined in 
math.h that in turn calls the right inline function (for the type 
needed) which is defined in mathinline.h --- so as far as i can see, 
libc should not be involved, only header files. I have attached a small 
example below on how to use the function. Please note the use of 
-std=c99 (you may also use -std=gnu99) as the macro is only activated 
when in C99 mode and gcc's default mode is C89 ("ANSI C"). If you're 
interested in the differences between the two standards the wikipedia 
entry on c has some info: 
http://en.wikipedia.org/wiki/C_programming_language


the program (save it under signbit_test.c):
[cut]
#include <math.h>
#include <stdio.h>

int main() {
         printf("sign of  1.7 is %d\n", signbit(1.7));
         printf("sign of -1.1 is %d\n", signbit(-1.1));
         printf("sign of -0.0 is %d\n", signbit(-0.0));
         printf("sign of  0.0 is %d\n", signbit(0.0));
         return 0;
}
[/cut]

compile with:
gcc -Wall -std=c99 -lm signbit_test.c -o signbit_test

run with:
./signbit_test

should produce this output:
sign of  1.7 is 0
sign of -1.1 is -2147483648
sign of -0.0 is -2147483648
sign of  0.0 is 0

This was run with gcc 3.4.4 on amd64, if you want to i can try on a x86 
install in qemu.

Marco
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-22 21:05 ` Marco Matthies
@ 2005-10-22 21:32   ` Darren Dale
  2005-10-22 21:39   ` Marco Matthies
  2005-10-27 12:52   ` Darren Dale
  2 siblings, 0 replies; 10+ messages in thread
From: Darren Dale @ 2005-10-22 21:32 UTC (permalink / raw
  To: gentoo-science

On Saturday 22 October 2005 5:05 pm, Marco Matthies wrote:
> Darren Dale wrote:
> > On my system, SciPy's signbit function reports that the sign bit is not
> > set for any number, positive or negative. Could someone here help me
> > understand how to test the libc signbit function? I have to admit I have
> > no experience with C programming.
>
> Hi Darren,
>
> the signbit fuction is actually a macro (as the manpage says) defined in
> math.h that in turn calls the right inline function (for the type
> needed) which is defined in mathinline.h --- so as far as i can see,
> libc should not be involved, only header files. I have attached a small
> example below on how to use the function. Please note the use of
> -std=c99 (you may also use -std=gnu99) as the macro is only activated
> when in C99 mode and gcc's default mode is C89 ("ANSI C"). If you're
> interested in the differences between the two standards the wikipedia
> entry on c has some info:
> http://en.wikipedia.org/wiki/C_programming_language

Hi Marko,

Thank you for your thoughtful and informative response. I have learned a few 
things.

Your test shows that signbit is working just fine on my system. I will discuss 
my results with the SciPy folks.

Thanks again,
Darren
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-22 21:05 ` Marco Matthies
  2005-10-22 21:32   ` Darren Dale
@ 2005-10-22 21:39   ` Marco Matthies
  2005-10-27 12:52   ` Darren Dale
  2 siblings, 0 replies; 10+ messages in thread
From: Marco Matthies @ 2005-10-22 21:39 UTC (permalink / raw
  To: gentoo-science

Replying to myself as I just noticed this:

Marco Matthies wrote:
> compile with:
> gcc -Wall -std=c99 -lm signbit_test.c -o signbit_test

You can actually leave out the -lm, unless you're using other "real" 
functions from math.h that need to be linked in.

Marco
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-22 18:16 [gentoo-science] question about signbit Darren Dale
  2005-10-22 21:05 ` Marco Matthies
@ 2005-10-23  6:39 ` Ertugrul Soeylemez
  1 sibling, 0 replies; 10+ messages in thread
From: Ertugrul Soeylemez @ 2005-10-23  6:39 UTC (permalink / raw
  To: gentoo-science

[-- Attachment #1: Type: text/plain, Size: 1669 bytes --]

Darren Dale <dd55@cornell.edu> (Sat, 22 Oct 2005 14:16:02 -0400):
> I am trying to track down the source of a bug in the SciPy development branch
> which, so far, has only been reported on a Gentoo platform (mine). It
> involves the use of the libc math function "signbit". Here is a clip from the
> manpage:
>
>        `signbit' is a generic macro which can work on all real floating-
>        point types.  It returns a non-zero value if the value of  X  has
>        its sign bit set.
>
> On my system, SciPy's signbit function reports that the sign bit is not set
> for any number, positive or negative. Could someone here help me understand
> how to test the libc signbit function? I have to admit I have no experience
> with C programming.

Hello Darren,

This doesn't have to be a bug.  I don't know how SciPy works, but many
scientific libraries provide their own number types.  In most number
encoding schemes (like floating point) the sign of a number is
determined by a single bit (the most significant bit of the whole
number, for integer types; the most significant bit of the mantissa, for
floating point types).

The scheme could be totally different for SciPy, so the bit actually
reported might be the wrong bit.  This is not a bug, but rather a
missing feature (signbit() doesn't know the type of the number).

Regards.



-----
Get my public key via GnuPG with one of:
gpg --keyserver subkeys.pgp.net --recv-keys CE402012
gpg --keyserver ldap://keyserver.pgp.com/ --recv-keys CE402012

Or via web-based keyserver:
http://www.keyserver.de/

Key fingerprint:
0F12 0912 DFC8 2FC5 E2B8  A23E 6BAC 998E CE40 2012

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-22 21:05 ` Marco Matthies
  2005-10-22 21:32   ` Darren Dale
  2005-10-22 21:39   ` Marco Matthies
@ 2005-10-27 12:52   ` Darren Dale
  2005-10-27 14:24     ` Miguel Barao
  2005-10-28 15:40     ` Marco Matthies
  2 siblings, 2 replies; 10+ messages in thread
From: Darren Dale @ 2005-10-27 12:52 UTC (permalink / raw
  To: gentoo-science

Hi Marco,

On Saturday 22 October 2005 5:05 pm, Marco Matthies wrote:
> Darren Dale wrote:
> > On my system, SciPy's signbit function reports that the sign bit is not
> > set for any number, positive or negative. Could someone here help me
> > understand how to test the libc signbit function? I have to admit I have
> > no experience with C programming.
>
> Hi Darren,
>
> the signbit fuction is actually a macro (as the manpage says) defined in
> math.h that in turn calls the right inline function (for the type
> needed) which is defined in mathinline.h --- so as far as i can see,
> libc should not be involved, only header files. I have attached a small
> example below on how to use the function. Please note the use of
> -std=c99 (you may also use -std=gnu99) as the macro is only activated
> when in C99 mode and gcc's default mode is C89 ("ANSI C"). If you're
> interested in the differences between the two standards the wikipedia
> entry on c has some info:
> http://en.wikipedia.org/wiki/C_programming_language
>
>
> the program (save it under signbit_test.c):
> [cut]
> #include <math.h>
> #include <stdio.h>
>
> int main() {
>          printf("sign of  1.7 is %d\n", signbit(1.7));
>          printf("sign of -1.1 is %d\n", signbit(-1.1));
>          printf("sign of -0.0 is %d\n", signbit(-0.0));
>          printf("sign of  0.0 is %d\n", signbit(0.0));
>          return 0;
> }
> [/cut]
>
> compile with:
> gcc -Wall -std=c99 -lm signbit_test.c -o signbit_test
>
> run with:
> ./signbit_test
>
> should produce this output:
> sign of  1.7 is 0
> sign of -1.1 is -2147483648
> sign of -0.0 is -2147483648
> sign of  0.0 is 0
>
> This was run with gcc 3.4.4 on amd64, if you want to i can try on a x86
> install in qemu.

Here is another test:

#include <math.h>
#include <stdio.h>

int main() {
	printf("signbit(-1): %d\n", signbit(-1));
	printf("isnan(0.0/0): %d\n", isnan(0.0/0));
	printf("isinf(1.0/0): %d\n", isinf(1.0/0));
	return 0;
}

which yields:

signbit(-1): -2147483648
isnan(0.0/0): 1
isinf(1.0/0): 1

Do you know why signbit doesn't yield 1? I wonder if this might be the source 
of the problem in Scipy.

Thanks,
Darren
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-27 12:52   ` Darren Dale
@ 2005-10-27 14:24     ` Miguel Barao
  2005-10-28  0:27       ` Ertugrul Soeylemez
  2005-10-28 15:40     ` Marco Matthies
  1 sibling, 1 reply; 10+ messages in thread
From: Miguel Barao @ 2005-10-27 14:24 UTC (permalink / raw
  To: gentoo-science

On Thursday 27 October 2005 13:52, Darren Dale wrote:
>
> Here is another test:
>
> #include <math.h>
> #include <stdio.h>
>
> int main() {
> 	printf("signbit(-1): %d\n", signbit(-1));
> 	printf("isnan(0.0/0): %d\n", isnan(0.0/0));
> 	printf("isinf(1.0/0): %d\n", isinf(1.0/0));
> 	return 0;
> }
>
> which yields:
>
> signbit(-1): -2147483648
> isnan(0.0/0): 1
> isinf(1.0/0): 1
>
> Do you know why signbit doesn't yield 1? 

The answer is in 'man signbit'.

signbit is probably just ANDing your float value with 2^31=0x80000000 which 
reads -2147483648 if considered as a signed int (like in your printf).

Miguel
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-27 14:24     ` Miguel Barao
@ 2005-10-28  0:27       ` Ertugrul Soeylemez
  0 siblings, 0 replies; 10+ messages in thread
From: Ertugrul Soeylemez @ 2005-10-28  0:27 UTC (permalink / raw
  To: gentoo-science

[-- Attachment #1: Type: text/plain, Size: 793 bytes --]

Miguel Barao <mjsb@ramses.inesc-id.pt> (Thu, 27 Oct 2005 15:24:54 +0100):
> The answer is in 'man signbit'.
>
> signbit is probably just ANDing your float value with 2^31=0x80000000 which
> reads -2147483648 if considered as a signed int (like in your printf).

Yes.  The manpage doesn't say it returns 1, but it returns non-zero.
-2147483648 is non-zero.  You need to check if the value is !0, not if
the value is 1.  By the way, this holds for many boolean functions.

Regards.



-----
Get my public key via GnuPG with one of:
gpg --keyserver subkeys.pgp.net --recv-keys CE402012
gpg --keyserver ldap://keyserver.pgp.com/ --recv-keys CE402012

Or via web-based keyserver:
http://www.keyserver.de/

Key fingerprint:
0F12 0912 DFC8 2FC5 E2B8  A23E 6BAC 998E CE40 2012

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-27 12:52   ` Darren Dale
  2005-10-27 14:24     ` Miguel Barao
@ 2005-10-28 15:40     ` Marco Matthies
  2005-10-28 16:00       ` Darren Dale
  1 sibling, 1 reply; 10+ messages in thread
From: Marco Matthies @ 2005-10-28 15:40 UTC (permalink / raw
  To: gentoo-science

Darren Dale wrote:
> signbit(-1): -2147483648
> isnan(0.0/0): 1
> isinf(1.0/0): 1

As other people have already noted, signbit from math.h is behaving as 
it should.

> Do you know why signbit doesn't yield 1? I wonder if this might be the source 
> of the problem in Scipy.

I actually had a look at scipy today, and it uses its own signbit 
routine -- so looking at what signbit from math.h does is totally 
irrelevant. The scipy signbit implementation does say it returns 0 or 1.

I extracted the routine from Lib/special/cephes/isnan.c and constructed 
a little test program attached to the mail. On my machine it works as 
advertised when defining IBMPC. You can try out the defines for other 
machine types. On my machine, using DEC also works -- but using MIEEE or 
leaving all undefined gives the results you're seeing, namely all 
numbers are reported to have sign 0. So it may be that your machine type 
was not #defined correctly. Probably Lib/special/cephes/isnan.c should 
have something like:
#if !defined(IBMPC) && !defined(DEC) && !defined(MIEEE)
#error "machine type not defined"
#endif
in front of it to guard against the machine type being undefined (or 
perhaps this should go in the .h file of 'cephes'). You can try adding 
this to Lib/special/cephes/isnan.c and see if you still can compile or 
if it spits out an error.

Cheers,
Marco


Here's the test program with scipy's signbit function:

#include <stdio.h>

#define IBMPC 1
//#define DEC 1
//#define MIEEE 1

int signbit(x)
double x;
{
union
         {
         double d;
         short s[4];
         int i[2];
         } u;

u.d = x;

if( sizeof(int) == 4 )
         {
#ifdef IBMPC
         return( u.i[1] < 0 );
#endif
#ifdef DEC
         return( u.s[3] < 0 );
#endif
#ifdef MIEEE
         return( u.i[0] < 0 );
#endif
         }
else
         {
#ifdef IBMPC
         return( u.s[3] < 0 );
#endif
#ifdef DEC
         return( u.s[3] < 0 );
#endif
#ifdef MIEEE
         return( u.s[0] < 0 );
#endif
         }
}

int main() {
     printf("signbit( 1.0) = %d\n", signbit(1.0));
     printf("signbit(-1.0) = %d\n", signbit(-1.0));
     printf("signbit( 0.0) = %d\n", signbit(0.0));
     printf("signbit(-0.0) = %d\n", signbit(-0.0));
     return 0;
}
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-science] question about signbit
  2005-10-28 15:40     ` Marco Matthies
@ 2005-10-28 16:00       ` Darren Dale
  0 siblings, 0 replies; 10+ messages in thread
From: Darren Dale @ 2005-10-28 16:00 UTC (permalink / raw
  To: gentoo-science

On Friday 28 October 2005 11:40 am, Marco Matthies wrote:
> Darren Dale wrote:
> > signbit(-1): -2147483648
> > isnan(0.0/0): 1
> > isinf(1.0/0): 1
>
> As other people have already noted, signbit from math.h is behaving as
> it should.
>
> > Do you know why signbit doesn't yield 1? I wonder if this might be the
> > source of the problem in Scipy.
>
> I actually had a look at scipy today, and it uses its own signbit
> routine -- so looking at what signbit from math.h does is totally
> irrelevant. The scipy signbit implementation does say it returns 0 or 1.

Actually, SciPy's signbit is only used if math.h does not supply it. On my 
system, the math.h version of signbit is used. I talked with the lead scipy 
developer this morning, and based on the fact that signbit was returning a 
nonzero result rather than 1, he made a modification to scipy's code and we 
got the problem solved. Thank you everyone for your help. All of Scipy's 
tests are now successful on my arch, I think they will have a test release 
coming in the not-too-distant future.

Darren
-- 
gentoo-science@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-10-28 16:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-22 18:16 [gentoo-science] question about signbit Darren Dale
2005-10-22 21:05 ` Marco Matthies
2005-10-22 21:32   ` Darren Dale
2005-10-22 21:39   ` Marco Matthies
2005-10-27 12:52   ` Darren Dale
2005-10-27 14:24     ` Miguel Barao
2005-10-28  0:27       ` Ertugrul Soeylemez
2005-10-28 15:40     ` Marco Matthies
2005-10-28 16:00       ` Darren Dale
2005-10-23  6:39 ` Ertugrul Soeylemez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox