From: Teodor Sigaev Date: Wed, 1 Jun 2005 11:45:03 +0000 (+0000) Subject: Prevent to divide by zero and range out of 0..1 X-Git-Tag: REL8_1_0BETA1~681 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7148de1fa89c1a7ef4613ddfca730711335132ef;p=postgresql Prevent to divide by zero and range out of 0..1 --- diff --git a/contrib/tsearch2/rank.c b/contrib/tsearch2/rank.c index a58986e474..046b062b97 100644 --- a/contrib/tsearch2/rank.c +++ b/contrib/tsearch2/rank.c @@ -288,6 +288,7 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method) { ITEM *item = GETQUERY(q); float res = 0.0; + int len; if (!t->size || !q->size) return 0.0; @@ -303,10 +304,11 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method) case 0: break; case 1: - res /= log((float) cnt_length(t)); + res /= log( (float)(cnt_length(t)+1) ) / log(2.0); break; case 2: - res /= (float) cnt_length(t); + len = cnt_length(t); + if ( len > 0 ) res /= (float)len; break; default: /* internal error */ @@ -609,10 +611,11 @@ rank_cd(PG_FUNCTION_ARGS) case 0: break; case 1: - res /= log((float) cnt_length(txt)); + res /= log( (float)(cnt_length(txt)+1) ); break; case 2: - res /= (float) cnt_length(txt); + len = cnt_length(txt); + if ( len > 0 ) res /= (float)len; break; default: /* internal error */