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

In summary, the conversation discusses the code N = 1; while N < N + 1 N = N*10 end, and whether it will result in an infinite loop. The speaker believes it will because the value of N will always be compared to a value 1 unit greater than itself. The concept of "IEEE double precision" is brought up, which refers to the IEEE 754 standard for floating point arithmetic and is related to the code's potential for an infinite loop.
  • #1
darthxepher
56
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
  • #2
What if N happens to become equal to 253 - 1?
 
  • #3
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!
 
  • #4
when do you expect the code to get out of the while loop?

which number satisfies your constraint?
 
  • #5
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.
 

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

1. What is an infinite loop in Matlab?

An infinite loop in Matlab is a situation where a program continues to run without ever stopping or reaching an end point. This can happen when a condition in the loop is always true, causing the loop to repeat endlessly.

2. How does the condition "N < N+1" create an infinite loop?

In Matlab, the condition "N < N+1" will always evaluate to true because the value of N will always be less than the value of N+1. This means that the loop will continue to run endlessly, causing an infinite loop.

3. How can I fix an infinite loop in Matlab?

To fix an infinite loop in Matlab, you can add a break statement or modify the condition in the loop so that it eventually becomes false and the loop can exit. Another option is to use a counter variable with a maximum value and increment it each time the loop runs, and have the loop exit once the counter reaches the maximum value.

4. What are double precision variables in Matlab?

Double precision variables in Matlab are data types that can store numbers with decimal values. They are represented by the keyword "double" and have a higher precision than single precision variables, meaning they can store more precise values.

5. How does the use of double precision variables affect infinite loops?

The use of double precision variables in Matlab can affect infinite loops by causing unexpected results. This is because double precision variables have a limited number of decimal places they can represent, so when used in a condition for a loop, the value may never reach the exact value needed to exit the loop, causing it to run infinitely.

Similar threads

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