mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
Upgrade src/port/rint.c to be POSIX-compliant.
The POSIX spec says that rint() rounds halfway cases to nearest even. Our substitute implementation failed to do that, rather rounding halfway cases away from zero; and it also got some other cases (such as minus zero) wrong. This led to observable cross-platform differences, as reported in bug #12885 from Rich Schaaf; in particular, casting from float to int didn't honor round-to-nearest-even on builds using rint.c. Implement something that attempts to cover all cases per spec, and add some simple regression tests so that we'll notice if any platforms still get this wrong. Although this is a bug fix, no back-patch, as a behavioral change in the back branches was agreed not to be a good idea. Pedro Gimeno Fortea, reviewed by Michael Paquier and myself
This commit is contained in:
@ -266,3 +266,23 @@ SELECT (-32768)::int2 % (-1)::int2;
|
||||
0
|
||||
(1 row)
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int2 AS int2_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
x | int2_value
|
||||
------+------------
|
||||
-2.5 | -2
|
||||
-1.5 | -2
|
||||
-0.5 | 0
|
||||
0 | 0
|
||||
0.5 | 0
|
||||
1.5 | 2
|
||||
2.5 | 2
|
||||
(7 rows)
|
||||
|
||||
|
@ -363,3 +363,23 @@ SELECT (-2147483648)::int4 % (-1)::int2;
|
||||
0
|
||||
(1 row)
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int4 AS int4_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
x | int4_value
|
||||
------+------------
|
||||
-2.5 | -2
|
||||
-1.5 | -2
|
||||
-0.5 | 0
|
||||
0 | 0
|
||||
0.5 | 0
|
||||
1.5 | 2
|
||||
2.5 | 2
|
||||
(7 rows)
|
||||
|
||||
|
@ -846,3 +846,23 @@ SELECT (-9223372036854775808)::int8 % (-1)::int2;
|
||||
0
|
||||
(1 row)
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int8 AS int8_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
x | int8_value
|
||||
------+------------
|
||||
-2.5 | -2
|
||||
-1.5 | -2
|
||||
-0.5 | 0
|
||||
0 | 0
|
||||
0.5 | 0
|
||||
1.5 | 2
|
||||
2.5 | 2
|
||||
(7 rows)
|
||||
|
||||
|
@ -846,3 +846,23 @@ SELECT (-9223372036854775808)::int8 % (-1)::int2;
|
||||
0
|
||||
(1 row)
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int8 AS int8_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
x | int8_value
|
||||
------+------------
|
||||
-2.5 | -2
|
||||
-1.5 | -2
|
||||
-0.5 | 0
|
||||
0 | 0
|
||||
0.5 | 0
|
||||
1.5 | 2
|
||||
2.5 | 2
|
||||
(7 rows)
|
||||
|
||||
|
@ -92,3 +92,13 @@ SELECT ((-1::int2<<15)+1::int2)::text;
|
||||
SELECT (-32768)::int2 * (-1)::int2;
|
||||
SELECT (-32768)::int2 / (-1)::int2;
|
||||
SELECT (-32768)::int2 % (-1)::int2;
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int2 AS int2_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
|
@ -135,3 +135,13 @@ SELECT (-2147483648)::int4 % (-1)::int4;
|
||||
SELECT (-2147483648)::int4 * (-1)::int2;
|
||||
SELECT (-2147483648)::int4 / (-1)::int2;
|
||||
SELECT (-2147483648)::int4 % (-1)::int2;
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int4 AS int4_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
|
@ -205,3 +205,13 @@ SELECT (-9223372036854775808)::int8 % (-1)::int4;
|
||||
SELECT (-9223372036854775808)::int8 * (-1)::int2;
|
||||
SELECT (-9223372036854775808)::int8 / (-1)::int2;
|
||||
SELECT (-9223372036854775808)::int8 % (-1)::int2;
|
||||
|
||||
-- check rounding when casting from float
|
||||
SELECT x, x::int8 AS int8_value
|
||||
FROM (VALUES (-2.5::float8),
|
||||
(-1.5::float8),
|
||||
(-0.5::float8),
|
||||
(0.0::float8),
|
||||
(0.5::float8),
|
||||
(1.5::float8),
|
||||
(2.5::float8)) t(x);
|
||||
|
Reference in New Issue
Block a user