bugGNU Octave - Bugs: bug #50238, sum, cumsum, etc. mishandle...


bug #50238: sum, cumsum, etc. mishandle integer inputs

Submitter:  None
Submitted:  Mon 06 Feb 2017 12:08:57 PM UTC
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Luke Trowbridge Originator Email:  -email is unavailable-
Open/Closed:  * Open Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Tue 03 Aug 2021 11:31:27 AM UTC, comment #4: 

IIUC, the OP was surprised by the result of `cumsum(a)` (without the 'native' argument).

This is still the same as it was when it was reported.

It is still different from Matlab's `cumsum` which always returns in native type. But it's more consistent with how `sum` in Octave and in Matlab behaves.

I guess we could leave it as it is if we agree that this is the "better" behavior.

As it is, one could write `cumsum(double(a))` or `cast(cumsum(a), class(a))` to get the same result in Octave and in Matlab. The latter might not work correctly for 64 bit integers in Octave.

If we feel strongly about compatibility, we could change the default in `cumsum` to 'native' instead of 'double'. (Causing an inconsistency between the defaults of `sum` and `cumsum`.)

I slightly tend towards leaving it as it is. But I don't have strong feelings about that.

Re-categorizing as compatibility difference.

Markus Mützel <mmuetzel>
Group administrator
Tue 03 Aug 2021 09:32:38 AM UTC, comment #3: 

Using 6.1.0

The error for cumsum(a,'native') is gone

ans =

  150  255

Mon 06 Feb 2017 04:14:01 PM UTC, comment #2: 

The issue indeed seems to be with cumsum: by default, Octave performs the operation as double while Matlab uses the same class as the input and does not provide a mechanism to change this behavior:

>> a = uint8([150 150]);
>> sum(a)

ans =


>> sum(a,'native')

ans =



>> cumsum(a)

ans =

  1×2 uint8 row vector

   150   255

>> cumsum(a,'native')
Error using cumsum
Invalid option. Option must be 'forward', 'reverse', 'omitnan' or 'includenan'.

Guillaume <gyom>
Mon 06 Feb 2017 01:16:58 PM UTC, comment #1: 

Confirmed.  It looks like we have some issues with the optional argument specifying how the operation should be performed.  Matlab doesn't seem to have this argument for the cumulative functions.  And Octave is returning double for cumsum by default, same as for sum.  I think we need to start with some tests to see exactly what we are aiming for.

John W. Eaton <jwe>
Group administrator
Mon 06 Feb 2017 12:08:57 PM UTC, original submission:  

I execute the following code:

a = uint8([150 150]);

% Calulate sum
s1 = sum(a);
s2 = a(1) + a(2);

% Calculate cumulative sum
cs1 = cumsum(a);
cs2(1) = a(1);
cs2(2) = a(1) + a(2);

and I get the following output:

s1 =  300
s2 = 255
cs1 =

   150   300

cs2 =

  150  255

I expected s1 to be equivalent to s2, and cs1 to be equivalent to cs2.



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

Attach Files:

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 mmuetzel (Posted a comment)
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by None (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 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-08-03 mmuetzel Item GroupIncorrect Result Matlab Compatibility
        Release4.2.0 dev
    2017-02-06 jwe StatusNone Confirmed
        Operating SystemMicrosoft Windows Any
        SummaryFunctions &quot;sum&quot; &amp; &quot;cumsum&quot; handle data types differently than &quot;+&quot; operator sum, cumsum, etc. mishandle integer inputs

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code