Skip to content

Commit

Permalink
i.gensigset: fix possible pole and divide by zero errors in regroup
Browse files Browse the repository at this point in the history
Using logarithm function call with zero argument will lead to
a pole error, which occurs if the mathematical function has
an exact infinite result.

Check if the argument value is zero before passing that to
the log function to avoid such errors. I also added check
for negative numbers just to make sure the argument is in
the right domain for the log function as well.

There was also a possible divide by zero scenario when we
were dividing the class data by subsum, which can be zero.
Added a conditional check which avoids going to that stage.

Signed-off-by: Mohan Yelugoti <ymdatta.work@gmail.com>
  • Loading branch information
ymdatta committed Oct 11, 2024
1 parent cbc3ff4 commit 3f08d03
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions imagery/i.gensigset/subcluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,14 @@ static double regroup(struct ClassSig *Sig, int nbands)
subsum += tmp;
Data->p[s][i] = tmp;
}
likelihood += log(subsum) + maxlike;

for (i = 0; i < Sig->nsubclasses; i++)
Data->p[s][i] /= subsum;
if (subsum > 0)
likelihood += log(subsum) + maxlike;

if (subsum) {
for (i = 0; i < Sig->nsubclasses; i++)
Data->p[s][i] /= subsum;
}
}

return (likelihood);
Expand Down

0 comments on commit 3f08d03

Please sign in to comment.