diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2001-01-26 22:50:26 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2001-01-26 22:50:26 +0000 |
commit | b78d1bed07d343542a4d295c3113c73a3379ed93 (patch) | |
tree | 6ea9034397632b7bb2496a51968372fde1a7c1d2 /src/backend/utils/adt/int8.c | |
parent | 5a832218fd24e659826a8e5ca6cdafbdba1dde4b (diff) | |
download | postgresql-b78d1bed07d343542a4d295c3113c73a3379ed93.tar.gz postgresql-b78d1bed07d343542a4d295c3113c73a3379ed93.zip |
Change float8-to-int8 conversion to round to nearest, rather than
truncating to integer. Remove regress test that checks whether
4567890123456789 can be converted to float without loss; since that's
52 bits, it's on the hairy edge of failing with IEEE float8s, and indeed
rint seems to give platform-dependent results for it.
Diffstat (limited to 'src/backend/utils/adt/int8.c')
-rw-r--r-- | src/backend/utils/adt/int8.c | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index c02ada56ba4..a7df878c65b 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.27 2001/01/24 19:43:14 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.28 2001/01/26 22:50:26 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -693,13 +693,6 @@ i8tod(PG_FUNCTION_ARGS) /* dtoi8() * Convert double float to 8-byte integer. - * Do a range check before the conversion. - * Note that the comparison probably isn't quite right - * since we only have ~52 bits of precision in a double float - * and so subtracting one from a large number gives the large - * number exactly. However, for some reason the comparison below - * does the right thing on my i686/linux-rh4.2 box. - * - thomas 1998-06-16 */ Datum dtoi8(PG_FUNCTION_ARGS) @@ -707,11 +700,18 @@ dtoi8(PG_FUNCTION_ARGS) float8 val = PG_GETARG_FLOAT8(0); int64 result; - if ((val < (-pow(2.0, 63.0) + 1)) || (val > (pow(2.0, 63.0) - 1))) - elog(ERROR, "Floating point conversion to int64 is out of range"); - + /* Round val to nearest integer (but it's still in float form) */ + val = rint(val); + /* + * Does it fit in an int64? Avoid assuming that we have handy constants + * defined for the range boundaries, instead test for overflow by + * reverse-conversion. + */ result = (int64) val; + if ((float8) result != val) + elog(ERROR, "Floating point conversion to int8 is out of range"); + PG_RETURN_INT64(result); } |