Efficient way to compute geometric mean of many numbers

前端 未结 7 1076
栀梦
栀梦 2020-12-14 07:12

I need to compute the geometric mean of a large set of numbers, whose values are not a priori limited. The naive way would be

double geometric_mean(std::vect         


        
7条回答
  •  渐次进展
    2020-12-14 07:45

    I think I figured out a way to do it, it combined the two routines in the question, similar to Peter's idea. Here is an example code.

    double geometric_mean(std::vector const&data)
    {
        const double too_large = 1.e64;
        const double too_small = 1.e-64;
        double sum_log = 0.0;
        double product = 1.0;
        for(auto x:data) {
            product *= x;
            if(product > too_large || product < too_small) {
                sum_log+= std::log(product);
                product = 1;      
            }
        }
        return std::exp((sum_log + std::log(product))/data.size());
    }
    

    The bad news is: this comes with a branch. The good news: the branch predictor is likely to get this almost always right (the branch should only rarely be triggered).

    The branch could be avoided using Peter's idea of a constant number of terms in the product. The problem with that is that overflow/underflow may still occur within only a few terms, depending on the values.

提交回复
热议问题