Jump to content

how CAD knows a number is singular or plural


Recommended Posts

Posted

Dear all,

 

I would like to set up a lisp that when input number is singular, do routine A, and do routine B while input number is plural....how CAD knows the input number is singular or plural ?

 

Thank you

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • LauKwokFai

    4

  • pBe

    4

  • Lee Mac

    3

  • LibertyOne

    3

Popular Days

Top Posters In This Topic

Posted

You mean odd and even?

 

(= (- (/ theNumber 2.0) (/ theNumber 2)) 0)

 

Presuming that your argument is an integer.

 

Regards,

Mircea

Posted
Dear all,

 

I would like to set up a lisp that when input number is singular, do routine A, and do routine B while input number is plural....how CAD knows the input number is singular or plural ?

 

Thank you

 

if you use getstring (string)

 

 
(setq number (getstring "\nEnter Number:"))
(if ( = (strlen number) 1) (routine A)(Routine B))

 

if you're using getint: (integer)

 
(setq number (getint "\nEnter Number:"))
(if (< number 10)(routine A)(Routine B))

Posted (edited)
You mean odd and even?

 

(= (- (/ theNumber 2.0) (/ theNumber 2)) 0)

 

Presuming that your argument is an integer.

 

Regards,

Mircea

 

I guess you're right.. the OP might meant to say Odd / Even :)

 

or

(equal  (fix (/ number 2.00))(/ number 2.00))

Edited by pBe
Posted
You mean odd and even?

 

(= (- (/ theNumber 2.0) (/ theNumber 2)) 0)

 

Presuming that your argument is an integer.

 

Sorry, I meant odd and even !!

 

This is a very interesting solution, thank you so much.

Posted

You're welcome!

And please check the solution proposed by pBe in his/here second post - is more compact than mine.

 

Regards,

Mircea

Posted
Dear all,

 

I would like to set up a lisp that when input number is singular, do routine A, and do routine B while input number is plural....how CAD knows the input number is singular or plural ?

 

Thank you

 

Why not define two functions EVENP and ODDP?

 

EVENP returns T when even and nil when odd.

ODDP returns T when odd and nil when even.

 

(defun evenp (i) (= (rem i 2) 0))
(defun oddp (i) (/= (rem i 2) 0))

 

Uses:

 

(evenp 2)
;returns => T
(evenp -5)
;returns => nil

(if
 (evenp 4) ;<--substitute any integer where the 4 is
 (routineA)
 (routineB)
)

Posted

It is getting too deep for my level of autolisp knowledge, thank you so much anyway :thumbsup:

Posted
Another two:

 

(defun evenp ( x ) (zerop (logand 1 x)))

(defun oddp ( x ) (= 1 (logand 1 x)))

:?

 

 

Been trying to figiure that (logand) a while ago..

 

Thanks for the snippet Lee

Posted

Thank you to everyone, been learning a lot today :D

Posted
Skinnin' Cats...
Keep your after-hours activities to yourself. :P
Posted
Keep your after-hours activities to yourself. :P

:shock::lol::lol::lol:

Posted
Another two:

 

(defun evenp ( x ) (zerop (logand 1 x)))

(defun oddp ( x ) (= 1 (logand 1 x)))

 

interesting method in solving it this way...

Posted
interesting method in solving it this way...

 

There's probably many more ways to do it...

 

(defun oddp ( n ) (and (member (rem n 10) '(1 3 5 7 9))))

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...