bugGNU Octave - Bugs: bug #45143, infinite FOR loop limits fail

 
 

bug #45143: infinite FOR loop limits fail

Submitter:  Antonio Pino Robles <antoniopino>
Submitted:  Tue 19 May 2015 05:59:26 PM UTC
   
 
Category:  Octave Function Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Fixed 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
   

Jump to the original submission

Fri 24 Jul 2020 06:07:21 PM UTC, comment #13: 

I guess I was after something different.  I assume a lot of Octave users are not professional programmers, but rather newbies just trying to perform some calculation.  For them, detecting infinite loops is a help as it was probably a coding error rather than actual intent.  I thought it would be nice to warn about that, but accomplished programmers could simply kick away the training wheels by disabling the warning in their .octaverc.

Rik <rik5>
Group administrator
Fri 24 Jul 2020 05:16:18 PM UTC, comment #12: 

Thanks for making those changes.

In Matlab, there is no warning about "while (true)" and I don't see why there should be.

The FOR loop warning is not there because the loop is infinite, but because it is not, and we can't continue changing the loop variable forever.

Since we have to assign a value to the loop variable and there is an assumption that it will be changing with each iteration (not strictly true with floating point values, see below) it will definitely stop changing once there is an overflow to Inf or make a discontinuous jump if the internal loop counter that is used to compute the next value in the range is a finite-precision integer that wraps back to zero (or some max negative value).  So we set the max value for that internal counter to 1 less than the max value for the integer used for array dimensions and indexing (octave_idx_type).  Then the internal C++ loop will eventually stop after performing the maximum number of iterations possible. 

However, since the loop variable is a floating point number in this case, its value will be unreliable.  But that's generally true anyway.  When floating point values are used for the loop variable, then we can't guarantee that each value will be different from the previous one.  It depends on the value of the index and the increment.  For example,


format long
for i = flintmax:flintmax+3
  i
end


displays


i =  9007199254740992
i =  9007199254740992
i =  9007199254740994
i =  9007199254740996
i =  9007199254740996


John W. Eaton <jwe>
Group administrator
Fri 24 Jul 2020 04:56:16 PM UTC, comment #11: 

I added a new warning ID, "Octave:infinite-loop", in case programmers intentionally want to use this construct (it is valid, and it would be annoying to use it and every warning for every m-file invocation).  See changeset https://hg.savannah.gnu.org/hgweb/octave/rev/a9f1a922367b.

In the documentation, I made the description general to any infinite loop, not just


for i = 1:Inf


I haven't looked at it, but it is my hope that the interpreter could detect a static while loop or until loop condition as well and also call warning_with_id().  For example,


while (1)


Even better would be detecting a static expression such as


while (true == true)


but most people intending to create an infinite loop will probably use the the simple constructs outlined above.

Rik <rik5>
Group administrator
Fri 24 Jul 2020 05:28:57 AM UTC, comment #10: 

I pushed the following changeset for improved compatibility:

http://hg.savannah.gnu.org/hgweb/octave/rev/e70c859c4bff

John W. Eaton <jwe>
Group administrator
Thu 29 Dec 2016 05:07:09 PM UTC, comment #9: 

I wanted to know whether ranges could be of different types.

In Octave, there is a range type that stores only the base, limit, and increment as double precision floating point values.

I think that in Matlab, the range syntax is just a way of creating a matrix and that there is no compact internal storage for them, though ranges used in for loops may get special treatment.

It doesn't appear to me that the issue is fixed because


for i = 1:inf
  ...
end


"works" in Matlab by setting the upper limit of the loop range to the maximum value for an int64 value.  In Octave, it is still an error.

John W. Eaton <jwe>
Group administrator
Wed 28 Dec 2016 10:41:48 PM UTC, comment #8: 

Is this issue still present in Octave 4.2.0?

When I understand comment #3 correctly, then the original (Matlab incompatibility) issue is already "fixed", because recent Matlab versions (R2015a) behave the same as Octave does in this respect.

But I have not understood the reasoning behind comment #6 and comment #7. Is this about the same issue?

Hartmut <hardy>
Wed 24 Jun 2015 07:43:37 PM UTC, comment #7: 


>> hi = intmax ('int64');
>>
>> r1 = (hi-1000):hi;
>> size (r1)

ans =

           1        1001

>> class (r1)

ans =

int64

>>
>> hi = 2^53;
>>
>> r2 = (hi-1000):hi;
>> size (r2)

ans =

           1        1001

>> class (r2)

ans =

double


Carnë Draug <carandraug>
Group Member
Wed 24 Jun 2015 06:59:34 PM UTC, comment #6: 

What happens in Matlab for the following?


hi = intmax ('int64');

r1 = (hi-1000):hi;
size (r1)
class (r1)

hi = 2^53;

r2 = (hi-1000):hi;
size (r2)
class (r2)


John W. Eaton <jwe>
Group administrator
Tue 26 May 2015 03:10:39 PM UTC, comment #5: 

All right it's been a week now, so I'll change the code to use a +verbatim+ while (1) -verbatim- cycle.


Antonio Pino Robles <antoniopino>
Wed 20 May 2015 09:45:42 AM UTC, comment #4: 

Oops, I meant leaving Octave as it is, not Matlab.

Michael Godfrey <godfrey>
Group Member
Wed 20 May 2015 08:50:36 AM UTC, comment #3: 

Right. In Matlab R2015a for 1:Inf is not
equivalent to while (1). So, it would
appear that leaving Matlab as it is is the
better choice.

Michael Godfrey <godfrey>
Group Member
Wed 20 May 2015 01:00:50 AM UTC, comment #2: 

I got someone to confirm this with a recent version of Matlab.

Assignment to a variable does fail with an error about it being too large to be allocated.

Looping over the range does work, but it also prints a warning about the range being truncated to 9223372036854775807 (which is intmax("int64")).

Mike Miller <mtmiller>
Group Member
Tue 19 May 2015 07:19:05 PM UTC, comment #1: 

Confirmed, although since there are easy workarounds like using while(1) I've lowered the priority on this bug.

I assume Matlab only accepts Inf in ranges in this one special case.  Does this fail?


x = 1:Inf;




Rik <rik5>
Group administrator
Tue 19 May 2015 05:59:26 PM UTC, original submission:  

The Inf function in Matlab can be used to generate infinite ranges and these are used to create infinite loops. That is


  for i=1:Inf
    #do something
  end


is equivalent to


  while (1)
    #do something
  end


This form of looping is currently not allowed in Octave.

Antonio Pino Robles <antoniopino>

 

(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

Digest:
   bug dependencies.

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by godfrey (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by antoniopino (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 11 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-07-24 rik5 StatusReady For Test Fixed
        Open/ClosedOpen Closed
    2020-07-24 jwe StatusConfirmed Ready For Test
        Summary&quot;Infinite ranges&quot; 1:Inf in Matlab, not allowed in Octave infinite FOR loop limits fail
    2016-02-16 lachlan Dependencies- bugs #47174 is dependent
    2015-06-24 rik5 Carbon-CopyRemoved 72865 -
    2015-05-20 mtmiller Release4.0.0-rc3 dev
        Operating SystemGNU/Linux Any
    2015-05-19 rik5 Severity3 - Normal 2 - Minor
        Priority5 - Normal 3 - Low
        StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code