0

I am pretty new in C and doesn't have a good understanding of the different data types I can use.

I have an array (foo) of half a million doubles bounded between 0 (exclusive) and 1 (inclusive), where about one third of the values are at the order of 0.01. I am trying to calculate the geometric mean of this array.

I am doing something like

double prod = 1;
for (int i = 0; i < arraylength; i++) prod *= foo[i];
double mean = pow(prod, 1.0/arraylength);

The issue is that prod "should become" really tiny, maybe something at the order of $10^{-10^{6}}$.

How can I solve this problem and calculate the geometric mean of foo?

Remi.b
  • 17,389
  • 28
  • 87
  • 168
  • 2
    If I understand the question, you are stating that your results are beyond the accuracy of a double. If so, then you need to either re-normalize your data such that double is still workable or else make use of a BigNumber package (which will be slower, but maintain your accuracy.) – Jiminion Jun 08 '15 at 20:23
  • @Jiminion the result shouldn't be the problem (the geometric mean of reasonable numbers should be reasonable as well), but the intermediate results when calculated naively like to quickly go out of range. – harold Jun 08 '15 at 20:26
  • My question is indeed a duplicate, I got the solution from the other post. I am voting to close. Thank you – Remi.b Jun 08 '15 at 20:26
  • @Remi.b Why not delete your question? Saves people time reading it, and saves you (self-)respect. – meaning-matters Jun 08 '15 at 20:35
  • Because I thought it may help future users to find out what their looking for if there are several formulations of their questions. But... I guess I could have deleted my question as well. – Remi.b Jun 08 '15 at 20:52

0 Answers0