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

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB while loop that produces an unexpected output. Participants are exploring the reasons behind the output of 9.007199254740992e+15 and the termination of the loop, focusing on concepts related to floating-point arithmetic and potential cancellation errors.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses confusion about the output of the loop and questions whether cancellation errors are involved.
  • Another participant provides the MATLAB code and asks for an explanation of why it terminates and outputs a specific large number.
  • It is noted that (x+1)-x equals 1 until the addition of one to x fails to change its value due to limitations in floating-point representation.
  • Discussion includes the properties of double-precision numbers, specifically the significance of the mantissa and its bit representation.
  • One participant mentions that the largest double-precision number that can be represented is 2^53, which relates to the output observed.
  • Another participant explains that addition and subtraction of floating-point numbers require equalizing exponents, which can lead to loss of significance in smaller numbers during arithmetic operations.

Areas of Agreement / Disagreement

Participants appear to agree on the mechanics of floating-point arithmetic and the implications for the loop's behavior, but there is no consensus on the exact nature of the cancellation errors or the specifics of the output.

Contextual Notes

The discussion highlights the limitations of floating-point representation in MATLAB, particularly regarding the precision of large numbers and the conditions under which arithmetic operations may yield unexpected results.

ver_mathstats
Messages
258
Reaction score
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
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   Reactions: ver_mathstats
(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   Reactions: ver_mathstats
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
 
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
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
2
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K