Author Topic: while loop issue  (Read 1740 times)

0 Members and 1 Guest are viewing this topic.

AutoKAD01

  • Guest
while loop issue
« on: November 22, 2010, 10:51:37 AM »
First time posting, so hello to the community!

I'm a bit stumped here and hopefully this isn't a forehead palm question, but the following code just runs in a continuous loop and I don't know why:

Code: [Select]
(setq a 12.0
      b 5.05)
(while (not (zerop (rem a b)))
       (setq b (+ b 0.05))
       )

Can someone enlighten me as to why this runs continuously?  I would think that once it hits 6.0 it should break the loop.   :?

Thanks!

-Kevin

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: while loop issue
« Reply #1 on: November 22, 2010, 10:54:55 AM »
Welcome to theSwamp  :-)

To answer your question, perhaps account for the rounding of doubles:

Code: [Select]
(setq a 12.0 b 5.05)

(while (not (equal 0.0 (rem a b) 1e-8))
  (setq b (+ b 0.05))
)

AutoKAD01

  • Guest
Re: while loop issue
« Reply #2 on: November 22, 2010, 11:10:55 AM »
Welcome to theSwamp  :-)

To answer your question, perhaps account for the rounding of doubles:

Code: [Select]
(setq a 12.0 b 5.05)

(while (not (equal 0.0 (rem a b) 1e-8))
  (setq b (+ b 0.05))
)

Thanks Lee!  That is an excellent way to handle this situation.

I checked out your website and tried out some of the lisps.  Very impressive stuff!

-Kevin

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: while loop issue
« Reply #3 on: November 22, 2010, 11:20:11 AM »
Thanks Kevin!  :-)