MATLAB Matlab Infinite Loop: Understanding N < N+1 and Double Precision Variables"

AI Thread Summary
The discussion centers on a code snippet that raises the question of whether it will result in an infinite loop. The code initializes N to 1 and enters a while loop that continues as long as N is less than N + 1. The key point is that, due to IEEE double precision limitations, when N approaches its maximum value, adding 1 may not yield a value greater than N, causing the loop to continue indefinitely. This behavior is attributed to how numbers are represented in floating-point format under the IEEE 754 standard, which defines the precision and range of floating-point numbers. Understanding these limitations is crucial for determining when the loop might exit, as it hinges on the properties of double precision representation.
darthxepher
Messages
56
Reaction score
0
I can't seem to figure out this question:

N = 1;
while N < N + 1
N = N*10
end

All variables are IEEE double precision. Will the code above result in an infinite loop?

I believe this will be a infinite loop because the value N is always going to be compared to a value 1 unit greater than itself, but what does that its double precision have to do with anything?

Thanks,
Darthxepher
 
Physics news on Phys.org
What if N happens to become equal to 253 - 1?
 
I am not sure what you mean by that. I am having a big misunderstanding with the concept of double precision and the IEEE rules. Please explain. I must understand!
 
when do you expect the code to get out of the while loop?

which number satisfies your constraint?
 
darthxepher said:
I can't seem to figure out this question:

N = 1;
while N < N + 1
N = N*10
end

All variables are IEEE double precision. Will the code above result in an infinite loop?

I believe this will be a infinite loop because the value N is always going to be compared to a value 1 unit greater than itself, but what does that its double precision have to do with anything?
Numbers that follow the IEEE specifications are stored in specific amounts of memory, which means that there is a smallest possible value and a largest possible value. When N is at or near the largest possible value, and you add 1 to it, you don't get the next larger value. "IEEE double precision" refers to the IEEE 754 standard for floating point arithmetic. Here's a link to a wiki article on it - http://en.wikipedia.org/wiki/IEEE_754.
 

Similar threads

Replies
1
Views
2K
Replies
4
Views
2K
Replies
1
Views
4K
Replies
4
Views
3K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
8
Views
4K
Back
Top