Given this loop. How can I make it stop.

Given this loop. How can I make it stop.

Anonymous
Not applicable
553 Views
2 Replies
Message 1 of 3

Given this loop. How can I make it stop.

Anonymous
Not applicable

I cant get my loop to stop.

My loop is a circular equation that does interations.
When the "previous" answer only differs from the "new" by .0000001 as it goes through the loop over and over, then I want the program to stop.
Unfortuntly it keeps going repeating 1.40274 which is the correct answer but I want it stop.  The correct answer given my current B1 and B2 stops on 1.40274 which is the correct answer.
I want it to stop though based on the difference of .0000001.   (My B1, and B2 numbers may change in the future.)
Unfortuantly it just keeps printing the answer over and over.
Anyone know how I can make it stop given my specification? (if you test my code the way it is you have to hit esc to make it stop)

I should clarify though that the answer is repeating itself because it it continuing to interate. So I need to make stop that when my specification become true namely the difference of .0000001.

--Thanks

ResetMaxFile #noPrompt
B1 = .205635
B2 = 1.2
B3 = 0

while B3 >= 0 do
( 
	B3 = B2+(B1 * sin (B3 * 180/pi))
	print B3
	if B3 < .0000001 then exit
)

 

0 Likes
Accepted solutions (1)
554 Views
2 Replies
Replies (2)
Message 2 of 3

Steve_Curley
Mentor
Mentor
Accepted solution
You're testing B3 not the difference between THIS result and the PREVIOUS result. B3 will be 1.40273798 at the time (iteration) you want the code to stop, so it will never be < 0.0000001 and the code runs forever.
The following prints all the variables each time through the loop so you can really see what's happening with the numbers - print doesn't display enough decimal places, hence the function to show more of them
(
fn F val = (formattedPrint val format:"10.8f")

clearListener()
B1 = 0.205635
B2 = 1.2
B3 = 0.0
PA = 0.0 --previous answer
CD = 0.0 --current difference

do
   ( 
   B3 = B2+(B1 * sin (B3 * 180/pi))
   CD = abs (PA - B3)
   format "B3 = %, PA = %, CD = %\n" (F B3) (F PA) (F CD)
   PA = B3
   )
while not (CD <= 0.0 and CD >= 0.0000001f)

B3 -- the final answer, so you don't get an undefined result
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 3

Anonymous
Not applicable
Great! Thanks!
0 Likes