乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 60|回复: 4

[编程交流] while loop not working, basic

[复制链接]

10

主题

39

帖子

29

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-5 22:20:03 | 显示全部楼层 |阅读模式
Hi, maybe I'm doing a stupid mistake and can't see it, but could you please tell me why this works:
 
  1. (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)
 
  1. (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!!
回复

使用道具 举报

1

主题

475

帖子

481

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-5 22:39:18 | 显示全部楼层
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.

[code](
回复

使用道具 举报

10

主题

39

帖子

29

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-5 22:55:44 | 显示全部楼层
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:
  1. (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???
回复

使用道具 举报

lrm

1

主题

257

帖子

282

银币

限制会员

铜币
-13
发表于 2022-7-5 23:08:33 | 显示全部楼层
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.
回复

使用道具 举报

10

主题

39

帖子

29

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-5 23:24:44 | 显示全部楼层
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
 
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-11 06:10 , Processed in 1.018643 second(s), 62 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表