Why did the loop terminate so early?

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?

 Respuesta aceptada

the cyclist
the cyclist el 2 de Abr. de 2015
Editada: the cyclist el 2 de Abr. de 2015

0 votos

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.

1 comentario

YEN
YEN el 2 de Abr. de 2015
The variable i is actually a "double" so it should not be limited to 2^31-1. So strange. Thanks anyway.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

YEN
el 2 de Abr. de 2015

Editada:

el 2 de Abr. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by