Jump to content

sum of list values as per total value


pixlepark96@gmail.com

Recommended Posts

I have a lists that some values and required total is = 7390.0

 

(43.0 75.325 36.5707 42.0 558.104 900.0 533.429 40.0 36.5707 78.0 64.2525 37.4287 45.0 65.3188 900.0 900.0 179.571 30.0 78.4287 73.0 34.0 505.0 800.0 162.351 26.0 50.0 35.0 35.0 591.649 70.0 130.0 130.0 105.0 0.0) which sum is = 7390.00

 

but after fixing to integer and real it is coming 7391.0 value 

 

(43.0 75.5 36.5 42.0 558.0 900.5 533.5 40.0 36.5 78.0 64.5 37.5 45.0 65.5 900.0 900.0 179.5 30.0 78.5 73.0 34.0 505.0 800.0 162.5 26.0 50.0 35.0 35.0 591.5 70.0 130.0 130.0 105.0 0.0)

 

now, how to get my total value = 7390.0

 

any suggestion.

Thanks,

 

avinash

Link to comment
Share on other sites

Its actually 7389.9991

 

(defun SumLst (lst / sum)
  (setq sum 0)
  (if (listp lst)
    (foreach num lst
      (if (numberp num)
        (setq sum (+ sum num))
      )
    )
  )
  sum
)

 

--Edit---

Examples

: (SumLst '(1 5 3 10.312 15 -2.35))
31.962

or

: (setq num '(1 5 3 10.312 15 -2.35))

: (SumLst num)
: 31.962

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

Two ways:

(setq L '(43.0 75.325 36.5707 42.0 558.104 900.0 533.429 40.0 36.5707 78.0 64.2525 37.4287 45.0 65.3188 900.0 900.0 179.571 30.0 78.4287 73.0 34.0 505.0 800.0 162.351 26.0 50.0 35.0 35.0 591.649 70.0 130.0 130.0 105.0 0.0))

_$ (apply '+ L)
7390.0
_$ (eval (cons + L))
7390.0

 

  • Like 1
Link to comment
Share on other sites

If you require the result to be rounded to the nearest integer, you can use a combination of rtos & atoi, e.g.:

(defun f ( l ) (atoi (rtos (apply '+ l) 2 0)))

 

_$ (f '(43.0 75.325 36.5707 42.0 558.104 900.0 533.429 40.0 36.5707 78.0 64.2525 37.4287 45.0 65.3188 900.0 900.0 179.571 30.0 78.4287 73.0 34.0 505.0 800.0 162.351 26.0 50.0 35.0 35.0 591.649 70.0 130.0 130.0 105.0 0.0))
7390

 

Edited by Lee Mac
Link to comment
Share on other sites

Please see my question.

I said after rounded values list

(43.0 75.5 36.5 42.0 558.0 900.5 533.5 40.0 36.5 78.0 64.5 37.5 45.0 65.5 900.0 900.0 179.5 30.0 78.5 73.0 34.0 505.0 800.0 162.5 26.0 50.0 35.0 35.0 591.5 70.0 130.0 130.0 105.0 0.0) = 7391

it is not coming to the sum of below list

(43.0 75.5 36.5 42.0 558.0 900.5 533.5 40.0 36.5 78.0 64.5 37.5 45.0 65.5 900.0 900.0 179.5 30.0 78.5 73.0 34.0 505.0 800.0 162.5 26.0 50.0 35.0 35.0 591.5 70.0 130.0 130.0 105.0 0.0) = 7390.0

Link to comment
Share on other sites

I am not surprised that the sums are different. It is nothing to do with lists but the way you are abusing mathematical operations.

 

1 hour ago, pixlepark96@gmail.com said:

......

it is not coming to the sum of below list

(43.0 75.5 36.5 42.0 558.0 900.5 533.5 40.0 36.5 78.0 64.5 37.5 45.0 65.5 900.0 900.0 179.5 30.0 78.5 73.0 34.0 505.0 800.0 162.5 26.0 50.0 35.0 35.0 591.5 70.0 130.0 130.0 105.0 0.0) = 7390.0

 

The sum of your list is 7391

  • Like 1
Link to comment
Share on other sites

I have used  this function for rounding. I am trying for solution that after rounding 7390.0 is not coming but before rounding it is coming.

Code:

(setq H2HRnd (mapcar '(lambda (x) (roundto x 0.5)) h2hdist))

Link to comment
Share on other sites

So, if I read this correctly, you have 2 lists in the example.

List 1 is the raw numbers

List 2 are the numbers rounded to the nearest 0.5?.

 

If you add together all List 1 you get a different answer to adding all of list 2. Your routine is probably doing exactly what you have told it to do.

 

I think you need to sum together all the raw numbers and then round the result to get an accurate answer.

Imagine a shorter list:

2.25

2.25

2.25

2.25

2.25

 

2.25 + 2.25 + 2.25 + 2.25 + 2.25  = 11.25

Go with your method and sum up the original list that's been rounded to the nearest 0.5?

2.5 + 2.5 + 2.5 + 2.5 + 2.5 = 12.5

Suppose you want the answer to the nearest whole number? Lets round the results....11.25-->11 and 12.5-->13

However what happens if you want then to the nearest 5? lets round the results....11.25-->10 and 12.5-->15

or nearest 10, could do that by rounding the last set of results, 10-->10 and 15-->20 (showing that rounded a rounded number can create a larger error)

 

Hoping this shows that you are going to get errors adding together rounded numbers rather than going back to the originals values, summing them all up and then rounding to the precision you need from the original unrounded sum. Each time you repeat the process you are potentially adding in more errors.

 

  • Agree 2
Link to comment
Share on other sites

On 11/12/2021 at 7:44 AM, pixlepark96@gmail.com said:

.......

(43.0 75.325 36.5707 42.0 558.104 900.0 533.429 40.0 36.5707 78.0 64.2525 37.4287 45.0 65.3188 900.0 900.0 179.571 30.0 78.4287 73.0 34.0 505.0 800.0 162.351 26.0 50.0 35.0 35.0 591.649 70.0 130.0 130.0 105.0 0.0) which sum is = 7390.00

 

but after fixing to integer and real it is coming 7391.0 value 

 

(43.0 75.5 36.5 42.0 558.0 900.5 533.5 40.0 36.5 78.0 64.5 37.5 45.0 65.5 900.0 900.0 179.5 30.0 78.5 73.0 34.0 505.0 800.0 162.5 26.0 50.0 35.0 35.0 591.5 70.0 130.0 130.0 105.0 0.0)

 

.......

 

Something is wrong with your workings. The sixth item is an integer in the first list, but has changed to a non integer in the second list!!!!

 

You have 8 items rounding up and 5 items rounding down, so no wonder the sums of each set are not equal!!

Link to comment
Share on other sites

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