octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #45143] infinite FOR loop limits fail


From: John W. Eaton
Subject: [Octave-bug-tracker] [bug #45143] infinite FOR loop limits fail
Date: Fri, 24 Jul 2020 13:16:18 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0

Follow-up Comment #12, bug #45143 (project octave):

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



    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?45143>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

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