msirois Posted February 5, 2016 Posted February 5, 2016 I need to ALWAYS round a number up in my LISP routine. The fix command seems to only round down. I need all numbers with any amount of decimal points to always round up to next even number. For example: 4.1 - round up to 5 5.8 - Round up to 6 3.00000000000001 - Round up to 4 2.00 - Stays the same; does not round up. How can I do this easily? Thanks in advance for the help. A bit more info if needed: I'm doing a stair LISP where I take the floor heights and divide it by 7 3/4" max riser height. Then that gives me a number of risers which needs to be round up to the next even number. I then take that number and divide up the difference in floor heights to create my risers. Quote
David Bethel Posted February 5, 2016 Posted February 5, 2016 For positive numbers maybe: (if (/= (fix n) n) (setq n (1+ (fix n)))) -David Quote
msirois Posted February 5, 2016 Author Posted February 5, 2016 For positive numbers maybe: (if (/= (fix n) n) (setq n (1+ (fix n)))) -David Thank you. I however ended up using code from a post from LEE MAC. Here is what I used: ;;; Q is my variable that I needed round up (defun ROUND ( n m ) (cond ((equal 0.0 (rem n m) 1e- n) ((< n 0) (- n (rem n m))) ((+ n (- m (rem n m)))) ) ) (setq B (ROUND Q 1)) Quote
Lee Mac Posted February 5, 2016 Posted February 5, 2016 Just for variation, here is another for positive integers only: (defun LM:int:roundup1 ( n ) (fix (+ n 1 -1e-)) _$ (LM:int:roundup1 2.1) 3 And for rounding up to any multiple: (defun LM:int:roundup ( n m ) (* m (fix (+ (/ n (float m)) 1 -1e-))) _$ (LM:int:roundup 4.3 2) 6 Quote
Recommended Posts
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.