bugGNU Octave - Bugs: bug #63713, Feature request: NaN should be...

 
 

bug #63713: Feature request: NaN should be able to take the same class input options as ones and zeros

Submitter:  Nicholas Jankowski <nrjank>
Submitted:  Thu 26 Jan 2023 08:01:07 PM UTC
   
 
Category:  Octave Function Severity:  1 - Wish
Priority:  3 - Low Item Group:  Feature Request
Status:  Wont Fix Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Mon 30 Jan 2023 03:54:48 PM UTC, comment #4: 

At least with Matlab, "cast (NaN, 'logical')" also throws an error.  So, this is a corner case and Octave is Matlab-compatible so I think we can just leave it alone.

Rik <rik5>
Group administrator
Mon 30 Jan 2023 12:01:11 AM UTC, comment #3: 

OK, i recognize the concerns about changing nan. 

Curious if there is the same objection to the separate noted issue where 'cast (nan, int8)' = 0, but 'cast (nan, logical)' produces an error.

Nicholas Jankowski <nrjank>
Group Member
Sat 28 Jan 2023 07:37:19 PM UTC, comment #2: 

I also don't think this is a good idea.  NaN is a feature of floating point number systems such as IEEE double or single.  It is not a part of integer number representations or logical representations.  There is a subtle difference between creating a value which the programmer believes represents the underlying value/concept and cast() in which the programmer (even if only implicitly) accepts that there may be information loss when converting from one representation to another.  For example, "cast (1000, 'uint8')" returns 255.  The number 1,000 isn't representable in uint8 and some information was lost.  Similarly, "cast (NaN, 'uint8')" loses information and returns 0.  As a small aside, this would also introduce an incompatibility with Matlab.  I tried this code, "NaN (5, 'uint8')" and Matlab throws an error.  Also, to be consistent we would need to make all of the numeric functions which accept a class (pi(), e(), I(), NaN(), Inf()).

Rik <rik5>
Group administrator
Sat 28 Jan 2023 12:44:32 PM UTC, comment #1: 

I can understand the difference here


>> median(single([]))
ans =
  single
   NaN

>> median(int8([]))
ans =
  int8
   0


happening because there is no int8 NaN value.  But I'm not sure that it would be good for the NaN function itself to return something other than an actual NaN value.  So I'd favor only defining NaN for single and double.

If we do decide to make this change, do we also change Inf to return the same as intmin/intmax for integers?  I also don't think that's the right thing to do.

John W. Eaton <jwe>
Group administrator
Thu 26 Jan 2023 08:01:07 PM UTC, original submission:  

NaN can take an optional input to specify whether the output type should be double or single.  Ones and zeros take a similar optional input, but they can take any class with a valid type conversion. This report is to request the classes that can be input to NaN be expanded to the same set as for ones and zeros.  As built-in functions, it should have much less overhead than calls to 'cast' (which seems to come up a lot as we update functions to match Matlab's outputs for empty inputs if they aren't limited to single and double).

e.g.:

>> class(ones(2,'single'))
ans = single
>> class(ones(2,'uint8'))
ans = uint8
>> class(ones(2,'logical'))
ans = logical

>> class(NaN(2,'single'))
ans = single

>> class(NaN(2,'uint8'))
error: NaN: invalid class name

>> cast(NaN(2),'uint8')
ans =

  0  0
  0  0

>> class(NaN(2,'logical'))
error: NaN: invalid class name

>> cast(NaN(2),'logical')
error: invalid conversion from NaN to logical
error: called from
    cast at line 118 column 5


noting above that cast(NaN, 'logical') fails in both Octave and Matlab. I see that many functions in Matlab that would output a NaN for supported numeric types, output a 0 for non-supported types like int8, uint8, etc., and that includes logical:

Matlab 2022b:

>> median(single([]))
ans =
  single
   NaN

>> median(int8([]))
ans =
  int8
   0

>> median(logical([]))
ans =
  logical
   0


I would propose also (perhaps should be a separate feature request) that cast (NaN, 'logical') should output zero just as the int class does.

Nicholas Jankowski <nrjank>
Group Member

 

(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 rik5 (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by nrjank (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.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-30 rik5 StatusNone Wont Fix
        Open/ClosedOpen Closed
    2023-01-26 nrjank Priority5 - Normal 3 - Low
    2023-01-26 nrjank SummaryNaN should be able to take class input options like ones and zeros Feature request: NaN should be able to take the same class input options as ones and zeros

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code