Commit 7148de1f authored by Teodor Sigaev's avatar Teodor Sigaev

Prevent to divide by zero and range out of 0..1

parent a91fa390
...@@ -288,6 +288,7 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method) ...@@ -288,6 +288,7 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method)
{ {
ITEM *item = GETQUERY(q); ITEM *item = GETQUERY(q);
float res = 0.0; float res = 0.0;
int len;
if (!t->size || !q->size) if (!t->size || !q->size)
return 0.0; return 0.0;
...@@ -303,10 +304,11 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method) ...@@ -303,10 +304,11 @@ calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method)
case 0: case 0:
break; break;
case 1: case 1:
res /= log((float) cnt_length(t)); res /= log( (float)(cnt_length(t)+1) ) / log(2.0);
break; break;
case 2: case 2:
res /= (float) cnt_length(t); len = cnt_length(t);
if ( len > 0 ) res /= (float)len;
break; break;
default: default:
/* internal error */ /* internal error */
...@@ -609,10 +611,11 @@ rank_cd(PG_FUNCTION_ARGS) ...@@ -609,10 +611,11 @@ rank_cd(PG_FUNCTION_ARGS)
case 0: case 0:
break; break;
case 1: case 1:
res /= log((float) cnt_length(txt)); res /= log( (float)(cnt_length(txt)+1) );
break; break;
case 2: case 2:
res /= (float) cnt_length(txt); len = cnt_length(txt);
if ( len > 0 ) res /= (float)len;
break; break;
default: default:
/* internal error */ /* internal error */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment