From 2b1b9e64110c16a154ec200ae5ec82e315248b3f Mon Sep 17 00:00:00 2001 From: Mohan Yelugoti Date: Fri, 11 Oct 2024 19:11:42 -0400 Subject: [PATCH] i.gensigset: fix possible pole and divide by zero errors in regroup 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 --- imagery/i.gensigset/subcluster.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/imagery/i.gensigset/subcluster.c b/imagery/i.gensigset/subcluster.c index d45515f5672..0cb2c22ea94 100644 --- a/imagery/i.gensigset/subcluster.c +++ b/imagery/i.gensigset/subcluster.c @@ -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);