Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lisp - convert numbers to evens 1

Status
Not open for further replies.

danielhyde

Civil/Environmental
Feb 7, 2003
23
GB
Hi,
Can anyone tell me how to convert odd numbers in to even numbers in LISP?
I have a lisp routine which returns both even and odd numbers (0,1,2,3 up to 9) and I need to return and function that will round the figure down to the nearest even number, e.g.: 1 to 0, 3 to 2 etc.

Can anyone help?
Thanks in advance for any help
Dan
 
Replies continue below

Recommended for you

(* (fix (/ NUMBER 2)) 2)

"Whether you think that you can, or that you can't, you are usually right "
.. Henry Ford
 
HI borgunit,

I am getting an error:
; error: bad argument type: numberp:

I have been tring it as follows,

(XSTR (fix (/ XSTR 2)) 2)

I am tring to overwrite "XSTR" which is either odd or even, with a new "XSTR" value which is only even.
Can you tell me where I am going wrong.
 
(setq XSTR (*(fix(/ XSTR 2)2))

"Whether you think that you can, or that you can't, you are usually right "
.. Henry Ford
 
Oops, I did not look at your error closely. It looks like you are trying to do mathematical computation on a string and not a number. atof convert the string to a floating number.
Try this..

(setq XSTR (atof XSTR))
(setq XSTR (*(fix(/ XSTR 2))2))

"Whether you think that you can, or that you can't, you are usually right "
.. Henry Ford
 
hi borgunit,
It know works, it was a string!
Thanks for the help.
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top