]> granicus.if.org Git - postgresql/commitdiff
Prevent to divide by zero and range out of 0..1
authorTeodor Sigaev <teodor@sigaev.ru>
Wed, 1 Jun 2005 11:45:03 +0000 (11:45 +0000)
committerTeodor Sigaev <teodor@sigaev.ru>
Wed, 1 Jun 2005 11:45:03 +0000 (11:45 +0000)
contrib/tsearch2/rank.c

index a58986e474118fd8f04936a0afd00b923aa12ed2..046b062b97d7ea45e6369fbea0f03003cc6dfddc 100644 (file)
@@ -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 */