Why Doesn't My MATLAB While Loop Output the Expected Number?

  • Thread starter ver_mathstats
  • Start date
  • Tags
    Loop Matlab
In summary, x is a double-precision number that might have a number that is too large to be represented and when it is added to 1 it doesn't change the value.
  • #1
ver_mathstats
260
21
Homework Statement
Here is some Matlab code:
x = 1;
while (x+1)-x == 1
x = 2*x;
end
x
Explain why it terminates and explain why it outputs: 9.007199254740992e+15.
Relevant Equations
matlab, loops
I am confused why this is the case, why would it output that number? When I go through the code, there is (x+1)-1 which does equal 1, then x = 2*x is 2? So why is the output not 2? How should I proceed in coming up with a solution? Does this have something to do with cancellation errors somewhere?
 
Physics news on Phys.org
  • #2
ver_mathstats said:
Homework Statement:: Here is some Matlab code:
x = 1;
while (x+1)-x == 1
x = 2*x;
end
x
Explain why it terminates and explain why it outputs: 9.007199254740992e+15.
Relevant Equations:: matlab, loops

I am confused why this is the case, why would it output that number? When I go through the code, there is (x+1)-1 which does equal 1, then x = 2*x is 2? So why is the output not 2? How should I proceed in coming up with a solution? Does this have something to do with cancellation errors somewhere?
In your code, x is evidently a double. What's the largest double-precision number that can be represented? What would that number + 1 be?
 
  • Like
Likes ver_mathstats
  • #3
(x+1)-x will be equal to 1, until the addition of one to x fails to make a change.
The mantissa of a double has 52 bits plus one implied set bit.
2^(52+1) = ?
 
  • Like
Likes ver_mathstats
  • #4
Baluncore said:
(x+1)-x will be equal to 1, until the addition of one to x fails to make a change.
The mantissa of a double has 52 bits plus one implied set bit.
2^(52+1) = ?
Okay, I see, I think I'm understanding much better
 
  • #5
Mark44 said:
In your code, x is evidently a double. What's the largest double-precision number that can be represented? What would that number + 1 be?
Because 2^53=9.007199254740992e+15 and (2^53)+1=9.007199254740992e+15
 
  • #6
Addition and subtraction of floating point numbers requires the exponents first be made equal. The smaller number may become zero during the justification shift of the mantissa, before the arithmetic step. The arithmetic will then make no change to the bigger number.
 

FAQ: Why Doesn't My MATLAB While Loop Output the Expected Number?

What is a while loop in Matlab?

A while loop in Matlab is a programming construct that allows a set of instructions to be repeated as long as a certain condition is true. It is useful for automating repetitive tasks and for controlling the flow of a program.

How do I use a while loop in Matlab?

To use a while loop in Matlab, you need to first define the condition that will be evaluated at the beginning of each loop. Then, you need to write the code that will be executed as long as the condition is true. Finally, you need to make sure that the condition will eventually become false, otherwise the loop will run indefinitely.

What are the advantages of using a while loop in Matlab?

One of the main advantages of using a while loop in Matlab is that it allows for more flexibility and control compared to other types of loops. It also allows for the repetition of a set of instructions until a certain condition is met, which can be useful for solving complex problems.

What are some common mistakes when using while loops in Matlab?

One common mistake when using while loops in Matlab is forgetting to update the condition within the loop, which can result in an infinite loop. Another mistake is not defining a condition that will eventually become false, causing the loop to run indefinitely. It is also important to make sure that the loop does not get stuck in an endless cycle, as this can lead to errors or crashes.

How do I debug a while loop in Matlab?

To debug a while loop in Matlab, you can use the "pause" function to pause the execution of the program and check the values of variables at different points in the loop. You can also use the "disp" function to display the values of variables or print out messages to help you understand the flow of the loop. Additionally, using the "break" statement can help you exit the loop if necessary.

Similar threads

Replies
1
Views
1K
Replies
3
Views
1K
Replies
10
Views
2K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Back
Top