Jump to content

Round with a tolerance and group numbers


Recommended Posts

Posted

I have two problems:

1. Is there a code by autolisp that can help me round a list of numbers with a certain specified tolerance to the nearest even number?

2. I have a series of numbers that differ by a few values. I want to group the numbers with a difference of less than a certain range into a list, and then group these groups into a list.

Ex for 1 :

I have a list of number '(3998,3999, 4010, 3995, 4005, 4003)

With tolerance = 5, i will get a list (4000,4000,4010,4000,4000,4000)

tolerance = 10, i will get a list (4010,4010,4010,4010,4010,4010)

Ex for 2:

I have a list of number '(3998,3999, 4005, 3995, 4020, 4045)

With tolerance = 5, i will get a list ((4000 4000,4000,4000) (4020) (4050))

tolerance = 20, i will get a list ((4020,4020,4020,4020, 4020) (4050))

Anyone could help me by autolisp, please. Thank so much

 

 

 

Posted
8 minutes ago, hosneyalaa said:

Thanks. I know that article by Lee-Mac but I think Lee's rounding functions don't have the tolerance to round as I want

Posted (edited)
11 hours ago, quangcongx3 said:

Thanks. I know that article by Lee-Mac but I think Lee's rounding functions don't have the tolerance to round as I want

@quangcongx3

Did you really look into Lee's functions?

;; Round Multiple  -  Lee Mac
;; Rounds 'n' to the nearest multiple of 'm'
(defun LM:roundm ( n m )
    (* m (atoi (rtos (/ n (float m)) 2 0)))
)

(setq s '(3998 3999 4010 3995 4005 4003))

(defun test (l p)
   (mapcar
      '(lambda (x)
          (LM:roundm x p)
       )
       l
   )
)

Result:

Command: (test s 5)
(4000 4000 4010 3995 4005 4005)

Command: (test s 10)
(4000 4000 4010 4000 4010 4000)

Your logic is mathematically flawed. If you round a number like 3995 by a tolerance of 5, it is going to return 3995, not 4000. If you round a number like 3998 by 10, your going to get 4000, not 4010. 

 

As for the grouping part, I am not great at list functions but I used another Lee Mac function to make it work:

;; Count Items  -  Lee Mac
;; Returns a list of dotted pairs detailing the number of
;; occurrences of each item in a supplied list.
(defun LM:CountItems ( l / c l r x )
    (while l
        (setq x (car l)
              c (length l)
              l (vl-remove x (cdr l))
              r (cons (cons x (- c (length l))) r)
        )
    )
    (reverse r)
)

;; Groups duplicate items into sublists.
(defun Groupn (l / c r x)
  (setq l (LM:CountItems l))
  (foreach n l
     (if (> (setq c (cdr n)) 1)
       (progn
          (repeat c (setq x (cons (car n) x)))
          (setq r (cons x r) x nil)
       )
       (setq r (cons (car n) r) x nil)
    )
  )
  (reverse r) 
)

 

Example:

Command: (setq l1 (list 1 1 1 3 3 5 6 2 8 8 8))
(1 1 1 3 3 5 6 2 8 8 8)
Command: (groupn l1)
((1 1 1) (3 3) 5 6 2 (8 8 8))

 

Edited by pkenewell
  • Like 1
Posted
12 hours ago, pkenewell said:

@quangcongx3 I edited the above reply to add a number grouping function.

Well, firstly, so many thanks to you because of the help of you. I really looked in the code of  Lee-Mac, but maybe  i didn't understand clearly the code of Lee. I will  look it again. Thanks again.

  • Like 1

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