octave-maintainers
[Top][All Lists]
Advanced

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

Re: signbit and logical tests


From: Michael D. Godfrey
Subject: Re: signbit and logical tests
Date: Sun, 10 Feb 2013 11:58:04 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2

On 02/10/2013 11:45 AM, Mike Miller wrote:
On Sun, Feb 10, 2013 at 11:23 AM, Michael D. Godfrey wrote:
> On 02/10/2013 11:05 AM, Mike Miller wrote:
>>
>> On Sun, Feb 10, 2013 at 1:37 AM, Michael D. Godfrey wrote:
>>>
>>> >It is curious.  The code (in libinterp/corefcn/mappers.cc) seems to
>>> >read as it will return an integer which is what the C++ signbit function
>>> >does.
>>> >However:
>>> >octave:1> sbit = signbit(-1.2)
>>> >sbit =  1
>>> >octave:2> whos
>>> >Variables in the current scope:
>>> >
>>> >    Attr Name        Size                     Bytes  Class
>>> >    ==== ====        ====                     =====  =====
>>> >         sbit        1x1                          8  double
>>> >
>>> >So, I am missing something.
>>
>> The function called there is xsignbit, which is defined at
>>
>>
>> http://hg.savannah.gnu.org/hgweb/octave/file/999f8257313b/liboctave/numeric/lo-mappers.h#l404
>>
>> This template function returns the same type as the argument.
>>
>> -- mike
>
> But, John said:
>>
>> The signbit function in Octave is just a simple wrapper around the system
>> library signbit function, and that returns the value of the sign bit as an
>> integer.  I don't know whether it would be a good idea to change that
>> definition for Octave.  The other function that I know of in Matlab that
>> returns something different from what the C library function returns is
>> strcmp.  So there is precedent for that kind of thing.  Or, if you want a
>> predicate-style function, maybe we should have isnegative instead?
>>
>> jwe
>
> I think that return logical would be more consistent with other Octave
> "logical" functions.
> Changing signbit to isnegative is possible, but, of course, sort order
> treats -0 as 0, so isnegative
> would be a bit confusing.
Understand. I was just explaining why you are seeing a double instead
of an integer. It really is just the value that the C math library
function returns but cast to a double.

My preference is for C functions in Octave to return what the C
library returns, so I'd keep signbit the way it is. Also note that you
can wrap it in logical() to get what you want, e.g.
logical(signbit(-0)).

-- mike
It would not be obvious to most users that logical(signbit(xx)) is what they want.
Remember that this all started with the fact that on 64bit Fedora signbit(-1) returns 1,
but on a 32bit Ubuntu system it returns 512.  Whatever else, this seems to me not
good.  I understand the documentation (both Octave and Linux) is clear that "non zero"
is returned.

In any case, it would be useful if the documentation stated that the only correct
test for the "logical" functions (including signbit) is to test against 0.  (Sadly, I actually
knew this, but forgot and made a mistake.)

Michael

Michael


reply via email to

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