while loop not working, basic
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 defunand 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 defunOnly 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!! 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.
( uupppsss! I see your point, yes! I will correct it inmediately
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. 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??? 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 approximately0.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. 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
页:
[1]