bugGNU Octave - Bugs: bug #51283, function min/max inconsistent...

 
 

bug #51283: function min/max inconsistent handling of -0

Submitter:  Joel Dahne <urathai>
Submitted:  Wed 21 Jun 2017 08:56:37 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  None Assigned to:  None
Originator Name:  Joel Open/Closed:  * Open
Release:  * 4.2.1 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 21 Jun 2017 08:54:47 PM UTC, comment #4: 

The floating point values -0 and 0 must be considered equal and the max function may return either of them, see the maxNum function in IEEE Std 754-2008.

Also, the standard for floating point arithmetic says in chapter 11 (reproducible floating-point results) that users should write programs in such a way that they do not depend “on the sign of a zero result … for … maxNum (x, y) … when x and y are equal”.

Strictly speaking, Octave behaves well.  However, I'd find it reasonable if Octave could return bit-equal results for the expressions max (x, y) and max ([x, y]).  And it should be irrelevant whether x and y are scalars or vectors for choosing the first or second argument in cases where both are (semantically) equal.


Regarding the interval package: That should be fixed in the sine function.  There already is a “l(l == 0) = -0;” for fixing the lower boundary.  The upper boundary can equally be fixed with “u(u == 0) = +0;”.  I have added these expressions in all cases where they could be needed, but obviously forgot it in sine.

For intervals, the lower boundary may not be stored as +0 internally.  And the upper boundary may not be stored as -0 internally.  This is enforced by the constructor.  Many arithmetic operations use a shortcut and don't call the constructor, thus have to ensure this condition in the function file.

Oliver Heimlich <oheim>
Wed 21 Jun 2017 12:51:52 PM UTC, comment #3: 

Matlab seems to always prefer the first one:

>> 1./max(-0, 0)
ans =
  -Inf
>> 1./max(0, -0)
ans =
   Inf
>> 1./max([-0, -0], [0, 0])
ans =
  -Inf  -Inf
>> 1./max([0, 0], [-0, -0])
ans =
   Inf   Inf

Though I'm not sure if this is intended or not, might just be that their code happens to check the arguments in the same order for scalars and vectors.

In most cases this of course makes no difference, the reason I noticed it is that in the interval package we have

>sin (infsup (0))
ans = [0]
> sin (infsup ([0, 0]))
ans = 1×2 interval vector
   [0, -0]   [0, -0]

and I were able to track it back to the max function.

Joel Dahne <urathai>
Wed 21 Jun 2017 12:49:09 PM UTC, comment #2: 

Also, the behavior for -0 and 0 is supposed to be that the sign is ignored in comparisons, so -0 should compare equal to 0.  That implies that it is neither less than nor greater 0.  So I think for sorting, they should just be considered the same.

John W. Eaton <jwe>
Group administrator
Wed 21 Jun 2017 12:43:34 PM UTC, comment #1: 

Could someone check what Matlab does?  It doesn't display the '-' for negative zero, but it can produce and preserve it.  You can check for it by doing some other operation like x = -0; 1/x ==> -Inf.

So, for whoever wants to check this out, try some things like this:


x = max ([-0, 0])
1/x


John W. Eaton <jwe>
Group administrator
Wed 21 Jun 2017 08:56:37 AM UTC, original submission:  

The min/max functions does not seem to be aware of the difference between -0 and 0. We have (-0 == 0) = 1 and (-0 < 0) = 0, so it is not clear how to handle this case. The main problem however lies in that the result is different when comparing scalars versus when comparing vectors. For max we have

> max (-0, 0)
ans = 0
>max (0, -0)
ans = -0
>max ([-0, -0], [0, 0])
ans = [-0, -0]
>max ([0, 0], [-0, -0])
ans = [0, 0]

It looks like the function make no difference between -0 and 0, it probably just returns the first (or last?) of them it encounters. But as can be seen the order differs between scalars and vectors.

I think it would be more natural to return 0 in all cases, but at least be consistent with it.

Joel Dahne <urathai>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by oheim (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by urathai (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code