diff --git a/src/vector.c b/src/vector.c index 8fada6b..f6c5abc 100644 --- a/src/vector.c +++ b/src/vector.c @@ -754,7 +754,8 @@ normalize_l2(PG_FUNCTION_ARGS) { Vector *a = PG_GETARG_VECTOR_P(0); float *ax = a->x; - float norm = 0.0; + double norm = 0.0; + float normf; Vector *result; float *rx; @@ -763,15 +764,16 @@ normalize_l2(PG_FUNCTION_ARGS) /* Auto-vectorized */ for (int i = 0; i < a->dim; i++) - norm += ax[i] * ax[i]; + norm += (double) ax[i] * (double) ax[i]; - norm = sqrtf(norm); + norm = sqrt(norm); + normf = (float) norm; - if (norm > 0) + if (normf > 0) { /* Auto-vectorized */ for (int i = 0, imax = a->dim; i < imax; i++) - rx[i] = ax[i] / norm; + rx[i] = ax[i] / normf; /* Check for overflow */ for (int i = 0, imax = a->dim; i < imax; i++) diff --git a/test/expected/functions.out b/test/expected/functions.out index aa014ba..12a575b 100644 --- a/test/expected/functions.out +++ b/test/expected/functions.out @@ -72,6 +72,12 @@ SELECT normalize_l2('[0,0]'); [0,0] (1 row) +SELECT normalize_l2('[3e38]'); + normalize_l2 +-------------- + [1] +(1 row) + SELECT l2_distance('[0,0]', '[3,4]'); l2_distance ------------- diff --git a/test/sql/functions.sql b/test/sql/functions.sql index 56f896b..24e0ee0 100644 --- a/test/sql/functions.sql +++ b/test/sql/functions.sql @@ -16,6 +16,7 @@ SELECT normalize_l2('[3,4]'); SELECT normalize_l2('[3,0]'); SELECT normalize_l2('[0,0.1]'); SELECT normalize_l2('[0,0]'); +SELECT normalize_l2('[3e38]'); SELECT l2_distance('[0,0]', '[3,4]'); SELECT l2_distance('[0,0]', '[0,1]');