Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb78d1be

Browse files
committed
Change float8-to-int8 conversion to round to nearest, rather than
truncating to integer. Remove regress test that checks whether4567890123456789 can be converted to float without loss; since that's52 bits, it's on the hairy edge of failing with IEEE float8s, and indeedrint seems to give platform-dependent results for it.
1 parent5a83221 commitb78d1be

File tree

4 files changed

+11
-32
lines changed

4 files changed

+11
-32
lines changed

‎src/backend/utils/adt/int8.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.27 2001/01/24 19:43:14 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.28 2001/01/26 22:50:26 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -693,25 +693,25 @@ i8tod(PG_FUNCTION_ARGS)
693693

694694
/* dtoi8()
695695
* Convert double float to 8-byte integer.
696-
* Do a range check before the conversion.
697-
* Note that the comparison probably isn't quite right
698-
*since we only have ~52 bits of precision in a double float
699-
*and so subtracting one from a large number gives the large
700-
*number exactly. However, for some reason the comparison below
701-
*does the right thing on my i686/linux-rh4.2 box.
702-
* - thomas 1998-06-16
703696
*/
704697
Datum
705698
dtoi8(PG_FUNCTION_ARGS)
706699
{
707700
float8val=PG_GETARG_FLOAT8(0);
708701
int64result;
709702

710-
if ((val< (-pow(2.0,63.0)+1))|| (val> (pow(2.0,63.0)-1)))
711-
elog(ERROR,"Floating point conversion to int64 is out of range");
712-
703+
/* Round val to nearest integer (but it's still in float form) */
704+
val=rint(val);
705+
/*
706+
* Does it fit in an int64? Avoid assuming that we have handy constants
707+
* defined for the range boundaries, instead test for overflow by
708+
* reverse-conversion.
709+
*/
713710
result= (int64)val;
714711

712+
if ((float8)result!=val)
713+
elog(ERROR,"Floating point conversion to int8 is out of range");
714+
715715
PG_RETURN_INT64(result);
716716
}
717717

‎src/test/regress/expected/int8-exp-three-digits.out

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ SELECT '' AS five, q2, float8(q2) FROM INT8_TBL;
8787
| -4567890123456789 | -4.56789012345679e+015
8888
(5 rows)
8989

90-
SELECT '' AS five, q1, int8(float8(q1)) AS "two coercions" FROM INT8_TBL;
91-
five | q1 | two coercions
92-
------+------------------+------------------
93-
| 123 | 123
94-
| 123 | 123
95-
| 4567890123456789 | 4567890123456789
96-
| 4567890123456789 | 4567890123456789
97-
| 4567890123456789 | 4567890123456789
98-
(5 rows)
99-
10090
SELECT '' AS five, 2 * q1 AS "twice int4" FROM INT8_TBL;
10191
five | twice int4
10292
------+------------------

‎src/test/regress/expected/int8.out

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ SELECT '' AS five, q2, float8(q2) FROM INT8_TBL;
8787
| -4567890123456789 | -4.56789012345679e+15
8888
(5 rows)
8989

90-
SELECT '' AS five, q1, int8(float8(q1)) AS "two coercions" FROM INT8_TBL;
91-
five | q1 | two coercions
92-
------+------------------+------------------
93-
| 123 | 123
94-
| 123 | 123
95-
| 4567890123456789 | 4567890123456789
96-
| 4567890123456789 | 4567890123456789
97-
| 4567890123456789 | 4567890123456789
98-
(5 rows)
99-
10090
SELECT '' AS five, 2 * q1 AS "twice int4" FROM INT8_TBL;
10191
five | twice int4
10292
------+------------------

‎src/test/regress/sql/int8.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ SELECT '' AS five, q1, q2, q1 / q2 AS divide FROM INT8_TBL;
2222

2323
SELECT''AS five, q1, float8(q1)FROM INT8_TBL;
2424
SELECT''AS five, q2, float8(q2)FROM INT8_TBL;
25-
SELECT''AS five, q1, int8(float8(q1))AS"two coercions"FROM INT8_TBL;
2625

2726
SELECT''AS five,2* q1AS"twice int4"FROM INT8_TBL;
2827
SELECT''AS five, q1*2AS"twice int4"FROM INT8_TBL;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp