pixlepark96@gmail.com Posted November 12, 2021 Posted November 12, 2021 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 Quote
mhupp Posted November 12, 2021 Posted November 12, 2021 (edited) 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 November 12, 2021 by mhupp 1 Quote
Grrr Posted November 12, 2021 Posted November 12, 2021 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 1 Quote
Lee Mac Posted November 12, 2021 Posted November 12, 2021 (edited) 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 November 12, 2021 by Lee Mac Quote
pixlepark96@gmail.com Posted November 12, 2021 Author Posted November 12, 2021 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 Quote
eldon Posted November 12, 2021 Posted November 12, 2021 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 1 Quote
pixlepark96@gmail.com Posted November 12, 2021 Author Posted November 12, 2021 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)) Quote
Steven P Posted November 12, 2021 Posted November 12, 2021 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. 2 Quote
pixlepark96@gmail.com Posted November 13, 2021 Author Posted November 13, 2021 I got the solution, I will test several situations and post it. Quote
eldon Posted November 13, 2021 Posted November 13, 2021 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!! Quote
Recommended Posts
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.