octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: signbit and logical tests


From: Daniel J Sebald
Subject: Re: signbit and logical tests
Date: Sun, 10 Feb 2013 12:54:09 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16

On 02/10/2013 10:05 AM, Mike Miller wrote:
On Sun, Feb 10, 2013 at 12:48 AM, Daniel J Sebald
<address@hidden>  wrote:
On 02/09/2013 09:02 PM, Michael D. Godfrey wrote:
This does not quite appear to be the case because this problem started
with bug #38291 which
showed that on at least one 32bit system (Ubuntu) the returned value for
true is 512.
That is why my test for == 1 failed.

Just curious how that is coming about.  Any guess?  I would think that C
defines a logical true as 1.

The return value for signbit is defined as zero for false, non-zero
for true. You can easily verify this with a C program:

address@hidden:~/src$ cat signbit.c
#include<math.h>
#include<stdio.h>
int main()
{
     printf ("signbit(-12.0) = %d\n", signbit(-12.0));
}
address@hidden:~/src$ gcc -m64 -o signbit signbit.c -lm; ./signbit
signbit(-12.0) = 128
address@hidden:~/src$ gcc -m32 -o signbit signbit.c -lm; ./signbit
signbit(-12.0) = -2147483648

As usual, this is a bit more complex/confusing than first meets the eye.

I see now it looks like C (not C++, but C) defined "signbit()" so that the compiler could generate adequate information about the sign bit with minimal amounts of assembly instructions. That is, whatever combination of shifting, register manipulations, etc. that is minimum is adequate. No need to make that signbit() result be 1 because more than likely, the programmer writing C code will do something like

if (signbit(x)) {
}

So, if the compiler does some extra step to make signbit() 0 or 1, it's a bit like doing the same conditional test twice.

Now, here is the monkey wrench. There is a standard C++ (not C, but C++) library function std:signbit which DOES produce a logical 0 or 1. Observe:

address@hidden cat signbit.c
#include <math.h>
#include <stdio.h>
int main()
{
    printf ("signbit(-12.0) = %d\n", signbit(-12.0));
}
[1]+  Done                    gvim signbit.c
address@hidden gcc -m64 -o signbit signbit.c
address@hidden ./signbit
signbit(-12.0) = 128


address@hidden cat signbit.c
/*#include <math.h>*/
#include <stdio.h>
int main()
{
    printf ("signbit(-12.0) = %d\n", signbit(-12.0));
}
address@hidden gcc -m64 -o signbit signbit.c
address@hidden ./signbit
signbit(-12.0) = 1

OK, so what should Octave produce? I would suggest Octave signbit() produce logical 0 or 1 simply to avoid the confusion we're sorting out. I don't think the extra assembly instruction or two is that critical to Octave. Would it make sense to have

signbit() - Logical
csignbit() - Wrapper around C library (not C++ library) signbit()

?

Dan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]