List Manipulation

List Manipulation

avinash00002002
Collaborator Collaborator
255 Views
20 Replies
Message 1 of 21

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
256 Views
20 Replies
Replies (20)
Message 2 of 21

В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 21

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 21

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 21

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 21

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 21

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 21

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 21

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 21

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 21

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 21

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 21

В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
Message 14 of 21

avinash00002002
Collaborator
Collaborator

Thanks for your prompt reply,

 

summation of list is 1068.7

(25 299.4 100.3 100.8 399.3 118.9 25)

 I need the sumation of list next roundup is 1069. 

the summation of more than 0.6 it give 1069 and less than 0.6 than 1068 

0 Likes
Message 15 of 21

Moshe-A
Mentor
Mentor

@avinash00002002 ,

 

(atoi (rtos (apply '+ '(25 299.4 100.3 100.8 399.3 118.9 25)) 2 0)) => 1069

 

you started with rounding numbers to 0.5 (and now rounding is not needed?)

you been giving solutions and did not respond

 

it's about time you explain what is your goal here?

(use google translate so we can understand)

 

 

 

 

0 Likes
Message 16 of 21

avinash00002002
Collaborator
Collaborator

No, 

I have a list for rounding (25 299.4 100.3 100.8 399.3 118.9 25) 

after rouning summation of above list = 1068.7 ok

I need rouning 0/0.5 to a list of all members. after rouning the list of member, the summation of list must be like 1069.0 ok,

to conver 1068.7 to 1069 of summation result must be list members need summation = 1069.0

0 Likes
Message 17 of 21

ВeekeeCZ
Consultant
Consultant

You start with list:

(25 299.4 100.3 100.8 399.3 118.9 25). The sum of all members is 1068.7.

 

Then you need to round (not round-up!) all members to 0.5, 

(25 299.5 100.5 101.0 399.5 119.0 25). Now the sum of all members is 1069.5.

 

But then you say that 1069.5 is not good, that you need to have 1069.0 (round to integers)

 

So what is the result you are expecting to get??

 

There are two ways I see...

One: (25 299.4 100.4 100.9 399.4 118.9 25)

Two: (25 299.5 100.0 101.0 399.5 119.0 25)

 

 

0 Likes
Message 18 of 21

Moshe-A
Mentor
Mentor

@avinash00002002 

 

seems you forgot your first request? 

$ (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

 

now it is 1069, where 1070 gone???

 

check this one:

(atof (rtos (apply '+ '(25 299.4 100.3 100.8 399.3 118.9 25)) 2 0)) => 1069.0

 

 

here is an update to the lisp, now support negative numbers

 

(defun prec05 (lst / b)
 (mapcar
  '(lambda (n)
    (if (< n 0.0)
     (setq n (abs n) x -1)
     (setq x 1)
    )
     
    (setq b (rem n 1))
    
    (cond
     ((= b 0.0)
      (* x (atof (rtos n 2 1)))
     )
     ((< b (- 0.25 1e-6))
      (* x (atof (rtos (fix n) 2 1)))
     )
     ((< b (- 0.75 1e-6))
      (* x (+ (fix n) 0.5))
     )
     ( t
      (* x (atof (rtos (+ (fix n) 1) 2 1)))
     )
    ); cond
      
   ); lambda
   lst
 ); mapcar
)

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

(defun lst->float (lst)
 (atof (rtos (apply '+ (prec05 lst)) 2 0))
)

 

0 Likes
Message 19 of 21

avinash00002002
Collaborator
Collaborator

yes, I always need summation of list of members are 0.0 it will also adjust members value.

0 Likes
Message 20 of 21

ВeekeeCZ
Consultant
Consultant

Good. So I gave you two examples of how the result could look. Do you prefer one of them, or do you just not care.

 

One: (25 299.4 100.4 100.9 399.4 118.9 25)

Two: (25 299.5 100.0 101.0 399.5 119.0 25)

 

The sum of both is 1069.0.

0 Likes