Jump to content

Recommended Posts

Posted

HELP!.

I have a value past from a line length. I want to round the number up to the next even whole number if it's greater than, say...8' long. What is best to use - the FIX or ROUND function?

 

Thanks for the help.

Mike in Big-D

Posted

Up to my knowledge, the ROUND function isn't available (built-in) in AutoLISP.

You may solve your problem using FIX; take also a look to REM function, it will allow you to decide if the number need to be rounded.

Posted

Hey Mircea,

Thanks for your quick response. Here's a snipit of my code. When I run it, it rounds my lengths up to the next whole number ok, but if the length is greater than 8' (96") it still rounds up to the next whole, not the next even whole. What am I doing wrong?

 

.................

(setq mpt (polar pt1a aten mpt1))

(setq tenl (+ (distance pt1a pt2a) 4.0))

(setq tenl2 (/ tenl 12.0))

(setq tend tenl2)

(setq CNT (1+ CNT))

 

 

(if (>= tend 96.01)

(setq tenda (* (fix (/ tend 2.0)) 2.0))

(setq tenda tend))

 

(if (

(setq tenda (* (fix (+ (/ tend 1.0) 0.5)) 1.0))

(setq tenda tend))

 

................

Posted

Try the following function:

;; Round Up  -  Lee Mac
;; Rounds 'n' up to the nearest 'm'

(defun roundup ( n m )
   (cond
       ((equal 0.0 (rem n m) 1e- n)
       ((< n 0) (- n (rem n m)))
       ((+ n (- m (rem n m))))
   )
)

 

e.g.:

_$ (roundup 96.5 2)
98.0

Posted

Good afternoon Lee,

Thanks so much for your help. It works great. Learning more and more every time I write code.

 

Mike

Posted

Good morning Lee,

Your code works great. I wrote a short TEST to make sure I get the correct values. Most numbers work, but a few are acting strange and I can't understand why. 1's and 6's. Can you take a look at this code (mostly yours) and tell me what I'm doing wrong.

 

 

Thanks again,

Mike

 

(defun C:TEST (/ roundup)

(setvar "LUNITS" 3)

(defun roundup ( n m )
   (cond
       ((equal 0.0 (rem n m) 1e- n)
       ((< n 0) (- n (rem n m)))
       ((+ n (- m (rem n m))))
   )
)
   (setq num (getreal "\nType number? "))

   (cond (and (> num  (< num 10)
       (setq numa (* (float (/ num 2.0)) 2.0))
       (setq numa numa)
       (roundup numa 2))

     (and (<= num 
       (setq numa (* (fix (+ (/ num 1.0) 0.5)) 1.0))
       (setq numa numa))
   )
)

Posted

Hi Mike,

 

Firstly, I see quite a few redundant & erroneous expressions in your code:

 

(setvar "LUNITS" 3)

The LUNITS system variable has no effect on any operations performed by your code.

 

You are missing several parentheses within your cond expression:

(cond
   [color=red]([/color]   (and (> num  (< num 10)[color=red])[/color]
       (setq numa (* (float (/ num 2.0)) 2.0))
       (setq numa numa)
       (roundup numa 2)
   [color=red])[/color]
   [color=red]([/color]   (and (<= num [color=red])[/color]
       (setq numa (* (fix (+ (/ num 1.0) 0.5)) 1.0))
       (setq numa numa)
   [color=red])[/color]
)

This:

(and (> num  (< num 10))

is equivalent to:

(< 8 num 10)

(* (float (/ num 2.0)) 2.0)

In the above expression you are dividing the number by 2, and then multiplying the result by 2, returning the original number... the float expression is also redundant since the variable 'num' is already a real (double).

 

(setq numa numa)

This is redundant; you are assigning the symbol 'numa' with the value it already holds.

 

(and (<= num )

The

 

(/ num 1.0)

Dividing a number by 1.0 will return the same number.

 

(* (fix (+ (/ num 1.0) 0.5)) 1.0)

Multiplying a number by 1.0 will return the same number.

 

(setq numa numa)

Again, redundant.

 

I'm unsure of the desired behaviour of the function if the entered number is less than 8 or greater than 10, but I would suggest:

(defun c:test ( / num )
   (if (setq num (getreal "\nNumber: "))
       (cond
           (   (< 8.0 num 10.0)
               (roundup num 2)
           )
           (   (<= num 8.0)
               (roundup num 1)
           )
           (   num   )
       )
   )
)

;; Round Up  -  Lee Mac
;; Rounds 'n' up to the nearest 'm'

(defun roundup ( n m )
   (cond
       ((equal 0.0 (rem n m) 1e- n)
       ((< n 0) (- n (rem n m)))
       ((+ n (- m (rem n m))))
   )
)

Finally, I would appreciate if you could retain my code headers when using my functions; this is common coding etiquette, to provide accreditation to the original author.

Posted

Hi Lee,

I'm new to this Forum. I absolutely agree with giving credit where credit is due. I will make sure your code header is included in mine. And in my future postings. I don't want to **** anyone off on here.

 

The routine I'm trying to get working is labeling rafters in a polyline boundary. In the construction industry, our customers want to see the lumber lengths in sizes that are available. Anything under 8', we simply put the rounded up number. They will cut the length needed from a 2x4 closest to that length. Little or no scrap. Anything over 8', and we give them the rounded up even number to purchase. That would explain the greater than 10 part.

Your explanation of each line is SO appreciated. It helps me understand better. As you can see, I'm a bit of a hack with LISP, but want to learn more and better ways. It's amazing that your code is so much more compressed than what I had.

Again, thanks for all your expertise.

 

Mike

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