I think a cap is a good thing if the error is relatively small ( < 1e-4 or
so).
Betraying my age, I usually rewrite this as this:
for (int element : elements) {
if (element > 0) {
result += element * (Math.log(element));
}
}
result -= elements.size() * Math.log(sum)
But presumably the compiler will notice this change and lift it out of the
inner loop
On Thu, Apr 29, 2010 at 9:33 AM, Sean Owen <[email protected]> wrote:
> double logSum = Math.log(sum);
> for (int element : elements) {
> if (element > 0) {
> result += element * (Math.log(element) - logSum);
> }
> }
>