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 23, 2024
1 parent cbc3ff4 commit 2b1b9e6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions imagery/i.gensigset/subcluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,18 @@ 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;

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

return (likelihood);
Expand Down

0 comments on commit 2b1b9e6

Please sign in to comment.