List Manipulation

List Manipulation

avinash00002002
Collaborator Collaborator
293 Views
21 Replies
Message 1 of 22

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
294 Views
21 Replies
Replies (21)
Message 21 of 22

Kent1Cooper
Consultant
Consultant

@avinash00002002 wrote:

... summation of list of members are 0.0 it will also adjust members value.


If the members of the list are all rounded to the nearest 0.5, and if the summation of those members comes to a xx.5 value, by what standard should it decide which of the members to adjust to make the summation to the nearest 0.0 [whole number]?  Presumably it would adjust only one, whether up or down, but there must be a determination of which one.

Kent Cooper, AIA
0 Likes
Message 22 of 22

calderg1000
Mentor
Mentor

Regards @avinash00002002 

Try this code.

;;;___
(defun ssum (lst / j n dj nlst st)
  (foreach j lst
    (setq dj (- j (fix j)))
    (cond ((= dj 0.) (setq n j))
          ((< dj 0.5) (setq n (+ (fix j) 0.5)))
          ((> dj 0.5) (setq n (+ (fix j) 1.0)))
          (t nil)
    )
    (setq nlst (cons n nlst))
  )
  (setq st  (apply '+ nlst)
        ndj (- st (fix st))
  )
  (if (= ndj 0.5)
    (setq st (+ st 0.5))
  )
  (print st)
  (print (reverse nlst))
)
;;;___
(setq lst '(25 299.4 100.3 100.8 399.3 118.9 25))
(ssum lst)
;1070.0 
;(25 299.5 100.5 101.0 399.5 119.0 25)

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes