even or odd...

Discussion in 'AutoCAD' started by aaron weissner, Jan 20, 2004.

  1. is there a way to determine if a number is even or odd... i want to have my
    routine do one thing if the number is even and another if it is odd...
     
    aaron weissner, Jan 20, 2004
    #1
  2. aaron weissner

    Tom Smith Guest

    (zerop (rem n 2)) ?
     
    Tom Smith, Jan 20, 2004
    #2
  3. Take your pick:

    (defun IsEven ( x )
    (zerop (rem x 2))
    )

    (defun IsEven ( x )
    (/= 1 (logand x 1))
    )

    etc ...

    is there a way to determine if a number is even or odd... i want to have my
    routine do one thing if the number is even and another if it is odd...
     
    michael puckett, Jan 20, 2004
    #3
  4. thanks...
     
    aaron weissner, Jan 20, 2004
    #4
  5. You're welcome. If you search the ngs I think you'll find versions more
    elegant than these two ... Tony, Doug etc. usually come up with ridiculously
    elegant / succinct functions.

    thanks...
     
    michael puckett, Jan 20, 2004
    #5
  6. aaron weissner

    Tom Smith Guest

    Well, you could do

    (defun evenp (num)
    (zerop (logand 1 num)))

    but that's no more succint than the first test suggested.. You can get a 1
    or 0 test result from boole, rem, or logand, but I don't know a way other
    than zerop to translate that to the desired t or nil return.
     
    Tom Smith, Jan 20, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.