Gentoo Archives: gentoo-science

From: Marco Matthies <marco-ml@×××.net>
To: gentoo-science@l.g.o
Subject: Re: [gentoo-science] question about signbit
Date: Fri, 28 Oct 2005 15:37:40
Message-Id: 43624657.4060301@gmx.net
In Reply to: Re: [gentoo-science] question about signbit by Darren Dale
1 Darren Dale wrote:
2 > signbit(-1): -2147483648
3 > isnan(0.0/0): 1
4 > isinf(1.0/0): 1
5
6 As other people have already noted, signbit from math.h is behaving as
7 it should.
8
9 > Do you know why signbit doesn't yield 1? I wonder if this might be the source
10 > of the problem in Scipy.
11
12 I actually had a look at scipy today, and it uses its own signbit
13 routine -- so looking at what signbit from math.h does is totally
14 irrelevant. The scipy signbit implementation does say it returns 0 or 1.
15
16 I extracted the routine from Lib/special/cephes/isnan.c and constructed
17 a little test program attached to the mail. On my machine it works as
18 advertised when defining IBMPC. You can try out the defines for other
19 machine types. On my machine, using DEC also works -- but using MIEEE or
20 leaving all undefined gives the results you're seeing, namely all
21 numbers are reported to have sign 0. So it may be that your machine type
22 was not #defined correctly. Probably Lib/special/cephes/isnan.c should
23 have something like:
24 #if !defined(IBMPC) && !defined(DEC) && !defined(MIEEE)
25 #error "machine type not defined"
26 #endif
27 in front of it to guard against the machine type being undefined (or
28 perhaps this should go in the .h file of 'cephes'). You can try adding
29 this to Lib/special/cephes/isnan.c and see if you still can compile or
30 if it spits out an error.
31
32 Cheers,
33 Marco
34
35
36 Here's the test program with scipy's signbit function:
37
38 #include <stdio.h>
39
40 #define IBMPC 1
41 //#define DEC 1
42 //#define MIEEE 1
43
44 int signbit(x)
45 double x;
46 {
47 union
48 {
49 double d;
50 short s[4];
51 int i[2];
52 } u;
53
54 u.d = x;
55
56 if( sizeof(int) == 4 )
57 {
58 #ifdef IBMPC
59 return( u.i[1] < 0 );
60 #endif
61 #ifdef DEC
62 return( u.s[3] < 0 );
63 #endif
64 #ifdef MIEEE
65 return( u.i[0] < 0 );
66 #endif
67 }
68 else
69 {
70 #ifdef IBMPC
71 return( u.s[3] < 0 );
72 #endif
73 #ifdef DEC
74 return( u.s[3] < 0 );
75 #endif
76 #ifdef MIEEE
77 return( u.s[0] < 0 );
78 #endif
79 }
80 }
81
82 int main() {
83 printf("signbit( 1.0) = %d\n", signbit(1.0));
84 printf("signbit(-1.0) = %d\n", signbit(-1.0));
85 printf("signbit( 0.0) = %d\n", signbit(0.0));
86 printf("signbit(-0.0) = %d\n", signbit(-0.0));
87 return 0;
88 }
89 --
90 gentoo-science@g.o mailing list

Replies

Subject Author
Re: [gentoo-science] question about signbit Darren Dale <dd55@×××××××.edu>