Jump to content

How can I round UP numbers instead of using FIX to round down?


Recommended Posts

Posted

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.

Posted

For positive numbers maybe:

 

(if (/= (fix n) n)
   (setq n (1+ (fix n))))

 

-David

Posted
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))

Posted

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

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