Why did the loop terminate so early?
2 views (last 30 days)
Show older comments
When I ran the following loop:
for i = 1 : 5^15
%
end
i
Matlab(R2007b) terminated when i equals 2^31-1 (2147483647). That's way too early. Do anyone have an explanation?
0 Comments
Accepted Answer
the cyclist
on 2 Apr 2015
Edited: the cyclist
on 2 Apr 2015
That is of course a quite ancient version. In MATLAB 2015a, the loop continues all the way to 5^15.
2^31-1 is the maximum representable integer in a 32-bit version of MATLAB, which yours is. I don't know the specifics, but it is presumably related to that.
What class of variable is i? I actually would have expected it to be a double, and therefore you would not have this problem.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!