Jump to content

while loop not working, basic question?


cletero

Recommended Posts

Hi, maybe I'm doing a stupid mistake and can't see it, but could you please tell me why this works:

 

(defun c:test (/ thisdrawing mspace ptx pty ptz newptx mylist line_pt1 line_pt2)
 (vl-load-com)
 (setq thisdrawing (vla-get-activedocument (vlax-get-acad-object)))
 (setq mspace (vla-get-modelspace thisdrawing))

 (setq ptx 0.0)
 (setq pty 3.0)
 (setq ptz 4.0)

 (setq newptx (+ ptx 1.4))

 (while (/= ptx newptx)
   (setq mylist (append mylist (list (list ptx pty ptz))))
   (setq ptx (+ ptx 0.2))
 ) ;_ end of while

 (setq line_pt1 (car mylist))
 (setq mylist (vl-remove line_pt1 mylist))
 (foreach line_pt2 mylist
   (vla-addline mspace (vlax-3d-point line_pt1) (vlax-3d-point line_pt2))
   (setq line_pt1 line_pt2)
 ) ;_ end of foreach

) ;_ end of defun

and this gets stuck in the while loop (won't exit)

 

(defun c:test (/ thisdrawing mspace ptx pty ptz newptx mylist line_pt1 line_pt2)
 (vl-load-com)
 (setq thisdrawing (vla-get-activedocument (vlax-get-acad-object)))
 (setq mspace (vla-get-modelspace thisdrawing))

 (setq ptx 0.0)
 (setq pty 3.0)
 (setq ptz 4.0)

 (setq newptx (+ ptx 1.5))

 (while (/= ptx newptx)
   (setq mylist (append mylist (list (list ptx pty ptz))))
   (setq ptx (+ ptx 0.2))
 ) ;_ end of while

 (setq line_pt1 (car mylist))
 (setq mylist (vl-remove line_pt1 mylist))
 (foreach line_pt2 mylist
   (vla-addline mspace (vlax-3d-point line_pt1) (vlax-3d-point line_pt2))
   (setq line_pt1 line_pt2)
 ) ;_ end of foreach

) ;_ end of defun

Only diference is changing this: "(setq newptx (+ ptx 1.4))" for this: "(setq newptx (+ ptx 1.5))"

I'm trying to learn how points are managed within AutoLISP but this seems pretty basic and I just can't see the difference!!

Link to comment
Share on other sites

Your test expression is while ptx is different from newptx, continues to evaluate the following expressions.

If the value for newptx is ptx plus 1.5, and in each loop you are adding 0.2 to ptx, it will never be equal to ptx + 1.5 and the loop will be endless...

Try testing while the value is less than or equal to newptx.

(<= ptx newptx)

Henrique

Link to comment
Share on other sites

uupppsss! I see your point, yes! I will correct it inmediately :D

Thank you very much Henrique

 

However, I'm still confused, because if I use "(setq newptx (+ ptx 1.6))" it doesn't exit the loop when ptx=1.6, in fact, any number under 1.4 (ex: 1.2, 1.0, 0.8) works, but over 1.4 (ex: 1.6, 1.8, 2.0) doesn't work :?:?

 

These while loops always confuse me, here's another case:

(setq ptx 0.0)
 (setq pty 3.0)
 (setq ptz 4.0)

 (setq newptx (+ ptx 1.4))

 (while (< ptx newptx)
   (setq mylist (append mylist (list (list ptx pty ptz))))
   (setq ptx (+ ptx 0.2))
 ) ;_ end of while

 

with "(+ ptx 1.4)" it exits the loop at 1.4, but with "(+ ptx 2.0)" it exits at 2.2, why???

Link to comment
Share on other sites

In general, you should be careful using real numbers in the condition component of a While statement. The value of 0.2 in binary is an irrational number. That is 0.2 base ten is approximately 0.00110011001100110011001100110011001100110011001100110011001100... base two. Either include a tolerance when making the comparison or use an integer format. This may not be the problem with your code but you should always consider the possibility.

Link to comment
Share on other sites

Thank you very much Henrique and Irm, I guessed there was something underneath the issue ;)

 

I was having the same problem with a program yesterday and ended up using a tolerance of +0.00001 so it would work :)

 

Now I know it's not my programming but just a glitch in the system hehehehe

 

:D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...