Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 527775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:55:07+00:00 2026-05-13T08:55:07+00:00

Edit: Since it appears nobody is reading the original question this links to, let

  • 0

Edit: Since it appears nobody is reading the original question this links to, let me bring in a synopsis of it here.

The original problem, as asked by someone else, was that, given a large number of values, where the sum would exceed what a data type of Double would hold, how can one calculate the average of those values.

There was several answers that said to calculate in sets, like taking 50 and 50 numbers, and calculating the average inside those sets, and then finally take the average of all those sets and combine those to get the final average value.

My position was that unless you can guarantee that all those values can be split into a number of equally sized sets, you cannot use this approach. Someone dared me to ask the question here, in order to provide the answer, so here it is.

Basically, given an arbitrary number of values, where:

  • I know the number of values beforehand (but again, how would your answer change if you didn’t?`)
  • I cannot gather up all the numbers, nor can I sum them (the sum will be too big for a normal data type in your programming language)

how can I calculate the average?

The rest of the question here outlines how, and the problems with, the approach to split into equally sized sets, but I’d really just like to know how you can do it.

Note that I know perfectly well enough math to know that in math theory terms, calculating the sum of A[1..N]/N will give me the average, let’s assume that there are reasons that it isn’t just as simple, and I need to split up the workload, and that the number of values isn’t necessarily going to be divisable by 3, 7, 50, 1000 or whatever.

In other words, the solution I’m after will have to be general.


From this question:

  • What is a good solution for calculating an average where the sum of all values exceeds a double’s limits?

my position was that splitting the workload up into sets is no good, unless you can ensure that the size of those sets are equal.


Edit: The original question was about the upper limit that a particular data type could hold, and since he was summing up a lot of numbers (count that was given as example was 10^9), the data type could not hold the sum. Since this was a problem in the original solution, I’m assuming (and this is a prerequisite for my question, sorry for missing that) that the numbers are too big to give any meaningful answers.

So, dividing by the total number of values directly is out. The original reason for why a normal SUM/COUNT solution was out was that SUM would overflow, but let’s assume, for this question that SET-SET/SET-SIZE will underflow, or whatever.

The important part is that I cannot simply sum, I cannot simply divide by the number of total values. If I cannot do that, will my approach work, or not, and what can I do to fix it?


Let me outline the problem.

Let’s assume you’re going to calculate the average of the numbers 1 through 6, but you cannot (for whatever reason) do so by summing the numbers, counting the numbers, and then dividing the sum by the count. In other words, you cannot simply do (1+2+3+4+5+6)/6.

In other words, SUM(1..6)/COUNT(1..6) is out. We’re not considering NULL’s (as in database NULL’s) here.

Several of the answers to that question alluded to being able to split the numbers being averaged into sets, say 3 or 50 or 1000 numbers, then calculating some number for that, and then finally combining those values to get the final average.

My position is that this is not possible in the general case, since this will make some numbers, the ones appearing in the final set, more or less valuable than all the ones in the previous sets, unless you can split all the numbers into equally sized sets.

For instance, to calculate the average of 1-6, you can split it up into sets of 3 numbers like this:

/ 1   2   3 \   / 4   5   6 \
| - + - + - | + | - + - + - |
\ 3   3   3 /   \ 3   3   3 /  <-- 3 because 3 numbers in the set
 ----------      -----------
      2               2        <-- 2 because 2 equally sized groups

Which gives you this:

      2               5
      -       +       - = 3.5
      2               2

(note: (1+2+3+4+5+6)/6 = 3.5, so this is correct here)

However, my point is that once the number of values cannot be split into a number of equally sized sets, this method falls apart. For instance, what about the sequence 1-7, which contains a prime number of values.

Can a similar approach, that won’t sum all the values, and count all the values, in one go, work?

So, is there such an approach? How do I calculate the average of an arbitrary number of values in which the following holds true:

  1. I cannot do a normal sum/count approach, for whatever reason
  2. I know the number of values beforehand (what if I don’t, will that change the answer?)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:55:07+00:00Added an answer on May 13, 2026 at 8:55 am

    Well, suppose you added three numbers and divided by three, and then added two numbers and divided by two. Can you get the average from these?

    x = (a + b + c) / 3
    y = (d + e) / 2
    z = (f + g) / 2
    

    And you want

    r = (a + b + c + d + e + f + g) / 7
    

    That is equal to

    r = (3 * (a + b + c) / 3 + 2 * (d + e) / 2 + 2 * (f + g) / 2) / 7
    r = (3 * x + 2 * y + 2 * z) / 7
    

    Both lines above overflow, of course, but since division is distributive, we do

    r = (3.0 / 7.0) * x + (2.0 / 7.0) * y + (2.0 / 7.0) * z
    

    Which guarantees that you won’t overflow, as I’m multiplying x, y and z by fractions less than one.

    This is the fundamental point here. Neither I’m dividing all numbers beforehand by the total count, nor am I ever exceeding the overflow.

    So… if you you keep adding to an accumulator, keep track of how many numbers you have added, and always test if the next number will cause an overflow, you can then get partial averages, and compute the final average.

    And no, if you don’t know the values beforehand, it doesn’t change anything (provided that you can count them as you sum them).

    Here is a Scala function that does it. It’s not idiomatic Scala, so that it can be more easily understood:

    def avg(input: List[Double]): Double = {
      var partialAverages: List[(Double, Int)] = Nil
      var inputLength = 0
      var currentSum = 0.0
      var currentCount = 0
      var numbers = input
    
      while (numbers.nonEmpty) {
        val number = numbers.head
        val rest = numbers.tail
        if (number > 0 && currentSum > 0 && Double.MaxValue - currentSum < number) {
          partialAverages = (currentSum / currentCount, currentCount) :: partialAverages
          currentSum = 0
          currentCount = 0
        } else if (number < 0 && currentSum < 0 && Double.MinValue - currentSum > number) {
          partialAverages = (currentSum / currentCount, currentCount) :: partialAverages
          currentSum = 0
          currentCount = 0
        }
        currentSum += number
        currentCount += 1
        inputLength += 1
        numbers = rest
      }
      partialAverages = (currentSum / currentCount, currentCount) :: partialAverages
    
      var result = 0.0
      while (partialAverages.nonEmpty) {
        val ((partialSum, partialCount) :: rest) = partialAverages
        result += partialSum * (partialCount.toDouble / inputLength)
        partialAverages = rest
      }
    
      result
    }
    

    EDIT:
    Won’t multiplying with 2, and 3, get me back into the range of “not supporter by the data type?”

    No. If you were diving by 7 at the end, absolutely. But here you are dividing at each step of the sum. Even in your real case the weights (2/7 and 3/7) would be in the range of manageble numbers (e.g. 1/10 ~ 1/10000) which wouldn’t make a big difference compared to your weight (i.e. 1).

    PS: I wonder why I’m working on this answer instead of writing mine where I can earn my rep 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit (2012-04-12): Since this question was asked it is now possible (as of jQuery
Edit: I've learned much since the posting of this question, and so I have
EDIT: This question is now redundant since Twitter no longer supports basic auth. I've
EDIT : I completely re-wrote the question since it seems like I was not
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Crossposted with STATS.se since this problem could straddle both STATs.se/SO https://stats.stackexchange.com/questions/17712/parallelize-solve-for-ax-b I have some
[EDIT: FIXED! The problem appears to be having an invisible VideoView overlaying my GLSurfaceView
Edit: This has since been solved. Thanks to everyone who helped. Invoking the method
This is the first Facebook App i've created since the Timeline changes. Everything appears
Edit: since I was appending via text the file was not being saved properly,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.