diff options
Diffstat (limited to 'src/backend/utils/adt/float.c')
-rw-r--r-- | src/backend/utils/adt/float.c | 44 |
1 files changed, 8 insertions, 36 deletions
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 00ae55106dd..a40c4d2197b 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -1157,15 +1157,8 @@ dtoi4(PG_FUNCTION_ARGS) */ num = rint(num); - /* - * Range check. We must be careful here that the boundary values are - * expressed exactly in the float domain. We expect PG_INT32_MIN to be an - * exact power of 2, so it will be represented exactly; but PG_INT32_MAX - * isn't, and might get rounded off, so avoid using it. - */ - if (num < (float8) PG_INT32_MIN || - num >= -((float8) PG_INT32_MIN) || - isnan(num)) + /* Range check */ + if (isnan(num) || !FLOAT8_FITS_IN_INT32(num)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1189,15 +1182,8 @@ dtoi2(PG_FUNCTION_ARGS) */ num = rint(num); - /* - * Range check. We must be careful here that the boundary values are - * expressed exactly in the float domain. We expect PG_INT16_MIN to be an - * exact power of 2, so it will be represented exactly; but PG_INT16_MAX - * isn't, and might get rounded off, so avoid using it. - */ - if (num < (float8) PG_INT16_MIN || - num >= -((float8) PG_INT16_MIN) || - isnan(num)) + /* Range check */ + if (isnan(num) || !FLOAT8_FITS_IN_INT16(num)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -1245,15 +1231,8 @@ ftoi4(PG_FUNCTION_ARGS) */ num = rint(num); - /* - * Range check. We must be careful here that the boundary values are - * expressed exactly in the float domain. We expect PG_INT32_MIN to be an - * exact power of 2, so it will be represented exactly; but PG_INT32_MAX - * isn't, and might get rounded off, so avoid using it. - */ - if (num < (float4) PG_INT32_MIN || - num >= -((float4) PG_INT32_MIN) || - isnan(num)) + /* Range check */ + if (isnan(num) || !FLOAT4_FITS_IN_INT32(num)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1277,15 +1256,8 @@ ftoi2(PG_FUNCTION_ARGS) */ num = rint(num); - /* - * Range check. We must be careful here that the boundary values are - * expressed exactly in the float domain. We expect PG_INT16_MIN to be an - * exact power of 2, so it will be represented exactly; but PG_INT16_MAX - * isn't, and might get rounded off, so avoid using it. - */ - if (num < (float4) PG_INT16_MIN || - num >= -((float4) PG_INT16_MIN) || - isnan(num)) + /* Range check */ + if (isnan(num) || !FLOAT4_FITS_IN_INT16(num)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); |