List Manipulation

List Manipulation

avinash00002002
Collaborator Collaborator
126 Views
12 Replies
Message 1 of 13

List Manipulation

avinash00002002
Collaborator
Collaborator

Hi!

 

I have some lists one of them I am putting here for help.

 

original

(25 299.4 100.3 100.8 399.3 118.9 25)

 

convert to 0.5  precision

(25.0 299.5 100.5 101.0 399.5 119.0 25.0)

 

I want to convert to 0.5 but the total of all members in a list is 1069.5 but I need to convert in 1070.

final summation of list members shall be 0.0 not .5 

 

Thanks,

avinash

0 Likes
127 Views
12 Replies
Replies (12)
Message 2 of 13

ВeekeeCZ
Consultant
Consultant

So add 0.5 to the first or last 25. That way, you'll the minimum overall error. 

However, without more context, why 1070, it's a bit of a guess...
0 Likes
Message 3 of 13

komondormrex
Mentor
Mentor

hey,

check the following

(defun round_0_5 (number)
	(if (< (rem number 1) 0.5)
	  (float (+ (fix number) 0.5))
	  (fix (+ 0.5 number))
	)
)

(fix (+ 0.5 (apply '+ (mapcar 'round_0_5 '(25 299.4 100.3 100.8 399.3 118.9 25))))) ->1071

0 Likes
Message 4 of 13

Moshe-A
Mentor
Mentor

@avinash00002002  hi,

 

check this function, it will convert numbers to 0.5

i leave the sum up for you and convert it to integer with (rtos)

 

enjoy,

Moshe

 

 

 

 

(defun prec05 (lst / b)
  (mapcar
   '(lambda (n)
     (setq b (rem n 1))
      
     (cond
      ((= b 0.0) (atof (rtos n 2 1)))
      ((< b 0.29999)
       (atof (rtos (fix n) 2 1))
      )
      ((< b 0.69999)
       (+ (fix n) 0.5)
      )
      ( t
       (atof (rtos (+ (fix n) 1) 2 1))
      )
     ); cond
      
    ); lambda
   lst
  )
)

 

 

0 Likes
Message 5 of 13

avinash00002002
Collaborator
Collaborator

Thtanks for prompt reply,

 

I need +0.5/-0.5 in list members also after sumation of list member required rounding number

0 Likes
Message 6 of 13

avinash00002002
Collaborator
Collaborator

HI!

I need summation of list members in rouning 0 to next + member like 1069.5 to 1070.

needs to be adjust 0.5 to somewhere where in member which has max. of decimal value

0 Likes
Message 7 of 13

Moshe-A
Mentor
Mentor

@avinash00002002 ,

 

how about this?

 

 

(defun prec05 (lst / b)
  (atoi
    (rtos
      (apply
       '+
       (mapcar
         '(lambda (n)
           (setq b (rem n 1))
      
           (cond
            ((= b 0.0) (atof (rtos n 2 1)))
            ((< b 0.29999)
             (atof (rtos (fix n) 2 1))
            )
            ((< b 0.69999)
             (+ (fix n) 0.5)
            )
            ( t
             (atof (rtos (+ (fix n) 1) 2 1))
            )
           ); cond
      
          ); lambda
         lst
       ); mapcar
      ); apply
      2 0
    ); rtos
  ); atoi
)

 

 

 

0 Likes
Message 8 of 13

avinash00002002
Collaborator
Collaborator

Hi!

thanks for your prompt reply,

 

1. I need total of list members are to be rounded value like 1070

2. I need also change the list member value also to get the value of 1070.

0 Likes
Message 9 of 13

Moshe-A
Mentor
Mentor

@avinash00002002 ,

 

from your replay i conclude that you did not run (prec05) function - why?!

 

this is what i get when i run it:

(prec05 '(25 299.4 100.3 100.8 399.3 118.9 25)) return 1070

 

0 Likes
Message 10 of 13

avinash00002002
Collaborator
Collaborator

Thanks for your reply,

 

I ran your function, I need to change in list members value also, I need invidudial members value also.

is it possible?

0 Likes
Message 11 of 13

komondormrex
Mentor
Mentor

@avinash00002002 

1. updated

2. updated for negative having numbers in list

3. updated for negative sum

(defun round_0_5 (number / sign)
  (setq sign (/ number (abs number))
	number (abs number) 
  )
  (cond
    ((zerop (rem number 1)) (* sign (float number))) 
    ((< (rem number 1) 0.5) (* sign (float (+ (fix number) 0.5))))
    (t (* sign (float (fix (+ 0.5 number)))))
  )
)
(defun round_up (number / sign)
  (setq sign (/ number (abs number))
	number (abs number)
	number (* sign (fix (+ 0.5 number))) 
  )
)

(mapcar 'round_0_5 '(25 299.4 100.3 100.8 399.3 118.9 25)) -> (25.0 299.5 100.5 101.0 399.5 119.0 25.0)

(round_up (apply '+ (mapcar 'round_0_5 '(25 299.4 100.3 100.8 399.3 118.9 25)))))-> 1070.0

0 Likes
Message 12 of 13

Moshe-A
Mentor
Mentor

@avinash00002002 

 

i split it to two functions:

first (prec05) return the list rounded 0.5

second (lst->int) return the sum of list of numbers rounded to integer.

 

(prec05 '(25 299.4 100.3 100.8 399.3 118.9 25)) =>=>  (25.0 299.5 100.5 101.0 399.5 119.0 25.0)

 

(lst->int '(25 299.4 100.3 100.8 399.3 118.9 25)) =>=> 1070

 

what next?  😀

 

 

(defun prec05 (lst / b)
 (mapcar
  '(lambda (n)
    (setq b (rem n 1))
    
    (cond
     ((= b 0.0) (atof (rtos n 2 1)))
     ((< b 0.29999)
      (atof (rtos (fix n) 2 1))
     )
     ((< b 0.69999)
      (+ (fix n) 0.5)
     )
     ( t
      (atof (rtos (+ (fix n) 1) 2 1))
     )
    ); cond
      
   ); lambda
   lst
 ); mapcar
)


(defun lst->int (lst)
 (atoi (rtos (apply '+ (prec05 lst)) 2 0))
)

 

 

 

 

0 Likes
Message 13 of 13

ВeekeeCZ
Consultant
Consultant

Here's my take

 

(defun :roundup_memb-to-0.5_sum-to-1 (lst / LM:roundup ret dif idx)
  
  ;; Round Up  -  Lee Mac ;; Rounds 'n' up to the nearest 'm'
  (defun LM:roundup ( n m ) ((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r)) ((+ n (- m r))))) (rem n m)))
  
  (setq lst (mapcar 'float lst)
	ret (mapcar '(lambda (x) (LM:roundup x 0.5)) lst))

  (if (> (LM:roundup (apply '+ ret) 1.) (apply '+ ret))
    (setq dif (mapcar '- ret lst)
	  pos (vl-position (apply 'min dif) dif)
	  idx -1
	  ret (mapcar '(lambda (x) (if (= pos (setq idx (1+ idx))) (+ x 0.5) x)) ret))
    ret))

;>> (:roundup_memb-to-0.5_sum-to-1 '(25 299.4 100.3 100.8 399.3 118.9 25))
;>> (25.5 299.5 100.5 101.0 399.5 119.0 25.0)

 

0 Likes