pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #12885: The result of casting a double to an integer depends on the database version
31+ messages / 6 participants
[nested] [flat]
* BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-20 19:43 rschaaf@commoninf.com
0 siblings, 1 reply; 31+ messages in thread
From: rschaaf@commoninf.com @ 2015-03-20 19:43 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 12885
Logged by: Rich Schaaf
Email address: rschaaf@commoninf.com
PostgreSQL version: 9.4.1
Operating system: Windows 7
Description:
The result returned by the following query appears to depend on the
PostgreSQL database version.
The query is:
select column1 AS double_value, cast(column1 AS INT) AS int_value
from (VALUES (-2.5::double precision),
(-1.5::double precision),
(-0.5::double precision),
(0.5::double precision),
(1.5::double precision),
(2.5::double precision)) t;
In PostgreSQL 9.3.5, the query returns:
double_value, int_value
-2.5, -3
-1.5, -2
-0.5, -1
0.5, 1
1.5, 2
2.5, 3
PostgreSQL 9.4.1, the query returns:
-2.5, -2
-1.5, -2
-0.5, 0
0.5, 0
1.5, 2
2.5, 2
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-23 14:48 Tom Lane <tgl@sss.pgh.pa.us>
parent: rschaaf@commoninf.com
0 siblings, 1 reply; 31+ messages in thread
From: Tom Lane @ 2015-03-23 14:48 UTC (permalink / raw)
To: rschaaf@commoninf.com; +Cc: pgsql-bugs
rschaaf@commoninf.com writes:
> The result returned by the following query appears to depend on the
> PostgreSQL database version.
> The query is:
> select column1 AS double_value, cast(column1 AS INT) AS int_value
> from (VALUES (-2.5::double precision),
> (-1.5::double precision),
> (-0.5::double precision),
> (0.5::double precision),
> (1.5::double precision),
> (2.5::double precision)) t;
> In PostgreSQL 9.3.5, the query returns:
> double_value, int_value
> -2.5, -3
> -1.5, -2
> -0.5, -1
> 0.5, 1
> 1.5, 2
> 2.5, 3
> PostgreSQL 9.4.1, the query returns:
> -2.5, -2
> -1.5, -2
> -0.5, 0
> 0.5, 0
> 1.5, 2
> 2.5, 2
FWIW, I get the latter behavior (round to nearest even) in all release
branches, and I would say that one is correct. Not real sure why your
9.3 installation is misbehaving.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-24 02:27 Michael Paquier <michael.paquier@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 31+ messages in thread
From: Michael Paquier @ 2015-03-24 02:27 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: rschaaf@commoninf.com; pgsql-bugs
On Mon, Mar 23, 2015 at 7:48 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> rschaaf@commoninf.com writes:
>> The result returned by the following query appears to depend on the
>> PostgreSQL database version.
>
>> The query is:
>> select column1 AS double_value, cast(column1 AS INT) AS int_value
>> from (VALUES (-2.5::double precision),
>> (-1.5::double precision),
>> (-0.5::double precision),
>> (0.5::double precision),
>> (1.5::double precision),
>> (2.5::double precision)) t;
>
>> In PostgreSQL 9.3.5, the query returns:
>> double_value, int_value
>> -2.5, -3
>> -1.5, -2
>> -0.5, -1
>> 0.5, 1
>> 1.5, 2
>> 2.5, 3
>
>> PostgreSQL 9.4.1, the query returns:
>> -2.5, -2
>> -1.5, -2
>> -0.5, 0
>> 0.5, 0
>> 1.5, 2
>> 2.5, 2
>
> FWIW, I get the latter behavior (round to nearest even) in all release
> branches, and I would say that one is correct. Not real sure why your
> 9.3 installation is misbehaving.
On a Windows 7 box with code compiled with MSVC 2010 I am seeing the
same behavior as Rich. This looks like a bug in ~9.3 that meritates
some attention assuming that the latter behavior is legit.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-24 02:42 Michael Paquier <michael.paquier@gmail.com>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 31+ messages in thread
From: Michael Paquier @ 2015-03-24 02:42 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: rschaaf@commoninf.com; pgsql-bugs
On Tue, Mar 24, 2015 at 11:27 AM, Michael Paquier wrote:
> On a Windows 7 box with code compiled with MSVC 2010 I am seeing the
> same behavior as Rich. This looks like a bug in ~9.3 that meritates
> some attention assuming that the latter behavior is legit.
And MinGW outputs the latter, similarly to other platforms... I'll investigate.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-24 03:12 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 31+ messages in thread
From: Tom Lane @ 2015-03-24 03:12 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: rschaaf@commoninf.com; pgsql-bugs
Michael Paquier <michael.paquier@gmail.com> writes:
> On Tue, Mar 24, 2015 at 11:27 AM, Michael Paquier wrote:
>> On a Windows 7 box with code compiled with MSVC 2010 I am seeing the
>> same behavior as Rich. This looks like a bug in ~9.3 that meritates
>> some attention assuming that the latter behavior is legit.
> And MinGW outputs the latter, similarly to other platforms... I'll investigate.
Look for something about setting the IEEE float rounding mode. "Round to
nearest even" is standard in most places, but it would not astonish me
to hear that Microsoft got that wrong.
If it is wrong I don't know that we'd want to back-patch a behavioral
change, but I'd definitely vote for conforming to the norm in 9.5
and later.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-24 06:25 Michael Paquier <michael.paquier@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 31+ messages in thread
From: Michael Paquier @ 2015-03-24 06:25 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: rschaaf@commoninf.com; pgsql-bugs
On Mon, Mar 23, 2015 at 8:12 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> On Tue, Mar 24, 2015 at 11:27 AM, Michael Paquier wrote:
>>> On a Windows 7 box with code compiled with MSVC 2010 I am seeing the
>>> same behavior as Rich. This looks like a bug in ~9.3 that meritates
>>> some attention assuming that the latter behavior is legit.
>
>> And MinGW outputs the latter, similarly to other platforms... I'll investigate.
>
> Look for something about setting the IEEE float rounding mode. "Round to
> nearest even" is standard in most places, but it would not astonish me
> to hear that Microsoft got that wrong.
>
> If it is wrong I don't know that we'd want to back-patch a behavioral
> change, but I'd definitely vote for conforming to the norm in 9.5
> and later.
I have done more testing (done only 9.3 with MinGW and MSVC before)
and the failure can be reproduced on master and REL9_4_STABLE as well
because visibly with MSVC 2010 the version of rint used is the one of
src/port, which is defined like that:
return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
For example by passing 2.5, we would get 3.0, and not 2.0 (nearest
even number). So isn't the problem here?
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-24 13:37 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 31+ messages in thread
From: Tom Lane @ 2015-03-24 13:37 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: rschaaf@commoninf.com; pgsql-bugs
Michael Paquier <michael.paquier@gmail.com> writes:
> On Mon, Mar 23, 2015 at 8:12 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Look for something about setting the IEEE float rounding mode. "Round to
>> nearest even" is standard in most places, but it would not astonish me
>> to hear that Microsoft got that wrong.
> I have done more testing (done only 9.3 with MinGW and MSVC before)
> and the failure can be reproduced on master and REL9_4_STABLE as well
> because visibly with MSVC 2010 the version of rint used is the one of
> src/port, which is defined like that:
> return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
> For example by passing 2.5, we would get 3.0, and not 2.0 (nearest
> even number). So isn't the problem here?
Hmm ... why are we using src/port's version? rint() has been required
by POSIX for decades, surely MSVC has got it?
(IOW, I'd rather fix this by removing src/port's version than by
trying to upgrade it to full IEEE spec.)
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 00:19 Michael Paquier <michael.paquier@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 31+ messages in thread
From: Michael Paquier @ 2015-03-25 00:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: rschaaf@commoninf.com; pgsql-bugs
On Tue, Mar 24, 2015 at 10:37 PM, Tom Lane wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> For example by passing 2.5, we would get 3.0, and not 2.0 (nearest
>> even number). So isn't the problem here?
>
> Hmm ... why are we using src/port's version? rint() has been required
> by POSIX for decades, surely MSVC has got it?
>
> (IOW, I'd rather fix this by removing src/port's version than by
> trying to upgrade it to full IEEE spec.)
rint() has been added in MSVC 2013, per se for example this thing
introduced by cec8394:
src/tools/msvc/Mkvcbuild.pm: push(@pgportfiles, 'rint.c') if
($vsVersion < '12.00');
So I would imagine that in a majority of cases the version of rint()
used by an MSVC build on Windows is src/port/rint.c, not to mention
that on platforms where rint() is not available (the world is full of
surprises), the behavior would be incorrect. It is possible to blame
Redmond's folks about missing rint() from MSVC for more than 10 years,
but the blame could be put on PG-side as well.
Hence I'd rather think that patching src/port is the way to go, with
for example something like the patch attached. That's a behavioral
change, so maybe a backpatch is not welcome. See for example Rich's
email meaning that 9.4 behavior is broken as I understand it, but
that's actually the other way around, and I suspect that the version
of 9.4 Rich has used was built with at least MSVC 2013, while the
version of 9.3 he used was built with src/port/rint.c.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
20150325_port_rint_fix.patch (text/x-diff, 389B) download
diff --git a/src/port/rint.c b/src/port/rint.c
index 9c4d775..34ad0a0 100644
--- a/src/port/rint.c
+++ b/src/port/rint.c
@@ -15,5 +15,13 @@
double
rint(double x)
{
+ double f, n = 0.0;
+
+ f = modf(x, &n);
+ if (((int) n % 2) == 0 && f == 0.5)
+ return floor(x);
+ else if (((int) n % 2) == 0 && f == -0.5)
+ return ceil(x);
+
return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 02:11 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-25 02:11 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; rschaaf@commoninf.com; pgsql-bugs
Michael Paquier wrote, On 2015-03-25 01:19:
> Hence I'd rather think that patching src/port is the way to go, with
> for example something like the patch attached.
There are some corner cases that that patch does not handle properly,
most notably doubles that would overflow an integer. I suggest the
attached implementation (it's not as a patch, though). I've included a
test suite. The function can deal with non-IEEE floats too, but some of
the tests assume IEEE doubles.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
nearest_or_even.c (text/x-csrc, 1.9K) download
/* By Pedro Gimeno Fortea, donated to the public domain */
#include <assert.h>
#include <math.h>
double
rint(double x)
{
double r;
if (copysign(1.0, x) < 0.0)
{
r = floor(x += 0.5);
if (r != x || x == 0.0)
return x >= 0.0 ? -0.0 : r;
return floor(x * 0.5) * 2.0;
}
r = ceil(x -= 0.5);
if (r != x)
return x <= 0.0 ? 0.0 : r;
return ceil(x * 0.5) * 2.0;
}
int test_equal_z(double a, double b)
{
return a == b && copysign(1.0, a) == copysign(1.0, b);
}
int main()
{
assert(rint( 2.5) == 2.0);
assert(rint( 2.4) == 2.0);
assert(rint( 2.6) == 3.0);
assert(rint( 1.5) == 2.0);
assert(rint( 1.4) == 1.0);
assert(rint( 1.6) == 2.0);
assert(rint(-2.5) == -2.0);
assert(rint(-2.4) == -2.0);
assert(rint(-2.6) == -3.0);
assert(rint(-1.5) == -2.0);
assert(rint(-1.4) == -1.0);
assert(rint(-1.6) == -2.0);
/* Test minus zero */
assert(test_equal_z(rint( 0.0), 0.0));
assert(test_equal_z(rint(-0.0), -0.0));
assert(test_equal_z(rint( 0.5), 0.0));
assert(test_equal_z(rint(-0.5), -0.0));
/* Corner cases for IEEE double */
/* Last float having fractional part */
assert(rint(-4503599627370495.5) == -4503599627370496.0);
assert(rint( 4503599627370495.5) == 4503599627370496.0);
assert(rint(-4503599627370494.5) == -4503599627370494.0);
assert(rint( 4503599627370494.5) == 4503599627370494.0);
/* abs value strictly greater than 0.5 should be rounded away from 0 */
/* (these numbers are 0.5 + 1 ulp) */
assert(rint( 0.5000000000000001) == 1.0);
assert(rint(-0.5000000000000001) == -1.0);
/* abs value strictly less than 0.5 should be rounded towards 0 */
/* (these values plus 1 ulp equal 0.5) */
assert(test_equal_z(rint( 0.49999999999999994), 0.0));
assert(test_equal_z(rint(-0.49999999999999994), -0.0));
/* denormals */
assert(test_equal_z(rint( 5e-324), 0.0));
assert(test_equal_z(rint(-5e-324), -0.0));
assert(test_equal_z(rint( 1e-322), 0.0));
assert(test_equal_z(rint(-1e-322), -0.0));
return 0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 03:26 Michael Paquier <michael.paquier@gmail.com>
parent: Pedro Gimeno <pgsql-004@personal.formauri.es>
0 siblings, 2 replies; 31+ messages in thread
From: Michael Paquier @ 2015-03-25 03:26 UTC (permalink / raw)
To: Pedro Gimeno <pgsql-004@personal.formauri.es>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; rschaaf@commoninf.com; pgsql-bugs
On Tue, Mar 24, 2015 at 7:11 PM, Pedro Gimeno
<pgsql-004@personal.formauri.es> wrote:
> Michael Paquier wrote, On 2015-03-25 01:19:
>> Hence I'd rather think that patching src/port is the way to go, with
>> for example something like the patch attached.
>
> There are some corner cases that that patch does not handle properly,
> most notably doubles that would overflow an integer. I suggest the
> attached implementation (it's not as a patch, though). I've included a
> test suite. The function can deal with non-IEEE floats too, but some of
> the tests assume IEEE doubles.
copysign is not that portable, at least it is not in the MSVC world.
So as a patch you would get something like the attached with what you
wrote.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
20150325_port_rint_fix_v2.patch (text/x-diff, 614B) download
diff --git a/src/port/rint.c b/src/port/rint.c
index 9c4d775..7e1a317 100644
--- a/src/port/rint.c
+++ b/src/port/rint.c
@@ -12,8 +12,25 @@
#include <math.h>
+static double
+copysign(double x, double y)
+{
+ return y == 0.0 ? fabs(x) : fabs(x) * y / fabs(y);
+}
+
double
rint(double x)
{
- return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
+ double r;
+ if (copysign(1.0, x) < 0.0)
+ {
+ r = floor(x += 0.5);
+ if (r != x || x == 0.0)
+ return x >= 0.0 ? -0.0 : r;
+ return floor(x * 0.5) * 2.0;
+ }
+ r = ceil(x -= 0.5);
+ if (r != x)
+ return x <= 0.0 ? 0.0 : r;
+ return ceil(x * 0.5) * 2.0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 10:32 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Michael Paquier <michael.paquier@gmail.com>
1 sibling, 0 replies; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-25 10:32 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; rschaaf@commoninf.com; pgsql-bugs
Michael Paquier wrote, On 2015-03-25 04:26:
> copysign is not that portable, at least it is not in the MSVC world.
> So as a patch you would get something like the attached with what you
> wrote.
Here's a version without copysign.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
nearest_or_even_d.c (text/x-csrc, 1.9K) download
/* By Pedro Gimeno Fortea, donated to the public domain */
#include <assert.h>
#include <math.h>
double
rint(double x)
{
double r;
if (x <= 0.0)
{
if (x == 0.0)
return x;
r = floor(x += 0.5);
if (r != x || x == 0.0)
return x >= 0.0 ? -0.0 : r;
return floor(x * 0.5) * 2.0;
}
r = ceil(x -= 0.5);
if (r != x)
return x <= 0.0 ? 0.0 : r;
return ceil(x * 0.5) * 2.0;
}
int test_equal_z(double a, double b)
{
return a == b && copysign(1.0, a) == copysign(1.0, b);
}
int main()
{
assert(rint( 2.5) == 2.0);
assert(rint( 2.4) == 2.0);
assert(rint( 2.6) == 3.0);
assert(rint( 1.5) == 2.0);
assert(rint( 1.4) == 1.0);
assert(rint( 1.6) == 2.0);
assert(rint(-2.5) == -2.0);
assert(rint(-2.4) == -2.0);
assert(rint(-2.6) == -3.0);
assert(rint(-1.5) == -2.0);
assert(rint(-1.4) == -1.0);
assert(rint(-1.6) == -2.0);
/* Test minus zero */
assert(test_equal_z(rint( 0.0), 0.0));
assert(test_equal_z(rint(-0.0), -0.0));
assert(test_equal_z(rint( 0.5), 0.0));
assert(test_equal_z(rint(-0.5), -0.0));
/* Corner cases for IEEE double */
/* Last float having fractional part */
assert(rint(-4503599627370495.5) == -4503599627370496.0);
assert(rint( 4503599627370495.5) == 4503599627370496.0);
assert(rint(-4503599627370494.5) == -4503599627370494.0);
assert(rint( 4503599627370494.5) == 4503599627370494.0);
/* abs value strictly greater than 0.5 should be rounded away from 0 */
/* (these numbers are 0.5 + 1 ulp) */
assert(rint( 0.5000000000000001) == 1.0);
assert(rint(-0.5000000000000001) == -1.0);
/* abs value strictly less than 0.5 should be rounded towards 0 */
/* (these values plus 1 ulp equal 0.5) */
assert(test_equal_z(rint( 0.49999999999999994), 0.0));
assert(test_equal_z(rint(-0.49999999999999994), -0.0));
/* denormals */
assert(test_equal_z(rint( 5e-324), 0.0));
assert(test_equal_z(rint(-5e-324), -0.0));
assert(test_equal_z(rint( 1e-322), 0.0));
assert(test_equal_z(rint(-1e-322), -0.0));
return 0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 11:18 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-25 11:18 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; rschaaf@commoninf.com; pgsql-bugs
Michael Paquier wrote, On 2015-03-25 04:26:
> copysign is not that portable, at least it is not in the MSVC world.
> So as a patch you would get something like the attached with what you
> wrote.
The only point of using copysign there was to deal with a negative zero
corner case. The attached removes copysign from the test program too,
relying instead on sprintf outputting the sign of minus zero when available.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
nearest_or_even_d2.c (text/x-csrc, 2.0K) download
/* By Pedro Gimeno Fortea, donated to the public domain */
#include <assert.h>
#include <math.h>
#include <stdio.h>
double
rint(double x)
{
double r;
if (x <= 0.0)
{
if (x == 0.0)
return x;
r = floor(x += 0.5);
if (r != x || x == 0.0)
return x >= 0.0 ? -0.0 : r;
return floor(x * 0.5) * 2.0;
}
r = ceil(x -= 0.5);
if (r != x)
return x <= 0.0 ? 0.0 : r;
return ceil(x * 0.5) * 2.0;
}
int test_equal_z(double a, double b)
{
char a1[20];
char b1[20];
sprintf(a1, "%+g", a);
sprintf(b1, "%+g", b);
return a == b && a1[0] == b1[0];
}
int main()
{
assert(rint( 2.5) == 2.0);
assert(rint( 2.4) == 2.0);
assert(rint( 2.6) == 3.0);
assert(rint( 1.5) == 2.0);
assert(rint( 1.4) == 1.0);
assert(rint( 1.6) == 2.0);
assert(rint(-2.5) == -2.0);
assert(rint(-2.4) == -2.0);
assert(rint(-2.6) == -3.0);
assert(rint(-1.5) == -2.0);
assert(rint(-1.4) == -1.0);
assert(rint(-1.6) == -2.0);
/* Test minus zero */
assert(test_equal_z(rint( 0.0), 0.0));
assert(test_equal_z(rint(-0.0), -0.0));
assert(test_equal_z(rint( 0.5), 0.0));
assert(test_equal_z(rint(-0.5), -0.0));
/* Corner cases for IEEE double */
/* Last float having fractional part */
assert(rint(-4503599627370495.5) == -4503599627370496.0);
assert(rint( 4503599627370495.5) == 4503599627370496.0);
assert(rint(-4503599627370494.5) == -4503599627370494.0);
assert(rint( 4503599627370494.5) == 4503599627370494.0);
/* abs value strictly greater than 0.5 should be rounded away from 0 */
/* (these numbers are 0.5 + 1 ulp) */
assert(rint( 0.5000000000000001) == 1.0);
assert(rint(-0.5000000000000001) == -1.0);
/* abs value strictly less than 0.5 should be rounded towards 0 */
/* (these values plus 1 ulp equal 0.5) */
assert(test_equal_z(rint( 0.49999999999999994), 0.0));
assert(test_equal_z(rint(-0.49999999999999994), -0.0));
/* denormals */
assert(test_equal_z(rint( 5e-324), 0.0));
assert(test_equal_z(rint(-5e-324), -0.0));
assert(test_equal_z(rint( 1e-322), 0.0));
assert(test_equal_z(rint(-1e-322), -0.0));
return 0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 11:46 Michael Paquier <michael.paquier@gmail.com>
parent: Pedro Gimeno <pgsql-004@personal.formauri.es>
0 siblings, 1 reply; 31+ messages in thread
From: Michael Paquier @ 2015-03-25 11:46 UTC (permalink / raw)
To: Pedro Gimeno <pgsql-004@personal.formauri.es>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; rschaaf@commoninf.com; pgsql-bugs
On Wed, Mar 25, 2015 at 8:18 PM, Pedro Gimeno
<pgsql-004@personal.formauri.es> wrote:
> Michael Paquier wrote, On 2015-03-25 04:26:
>> copysign is not that portable, at least it is not in the MSVC world.
>> So as a patch you would get something like the attached with what you
>> wrote.
>
> The only point of using copysign there was to deal with a negative zero
> corner case. The attached removes copysign from the test program too,
> relying instead on sprintf outputting the sign of minus zero when available.
Cool, thanks. Applied to the code tree of Postgres, it gives the patch attached.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
20150325_rint_portfix.patch (application/x-patch, 506B) download
diff --git a/src/port/rint.c b/src/port/rint.c
index 9c4d775..1349317 100644
--- a/src/port/rint.c
+++ b/src/port/rint.c
@@ -15,5 +15,20 @@
double
rint(double x)
{
- return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
+ double r;
+ if (x <= 0.0)
+ {
+ if (x == 0.0)
+ return x;
+ r = floor(x += 0.5);
+ if (r != x || x == 0.0)
+ return x >= 0.0 ? -0.0 : r;
+ return floor(x * 0.5) * 2.0;
+ }
+
+ r = ceil(x -= 0.5);
+ if (r != x)
+ return x <= 0.0 ? 0.0 : r;
+
+ return ceil(x * 0.5) * 2.0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 14:37 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 31+ messages in thread
From: Tom Lane @ 2015-03-25 14:37 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
Michael Paquier <michael.paquier@gmail.com> writes:
> On Wed, Mar 25, 2015 at 8:18 PM, Pedro Gimeno
> <pgsql-004@personal.formauri.es> wrote:
>> The only point of using copysign there was to deal with a negative zero
>> corner case. The attached removes copysign from the test program too,
>> relying instead on sprintf outputting the sign of minus zero when available.
> Cool, thanks. Applied to the code tree of Postgres, it gives the patch attached.
This is pretty desperately in need of some comments, but I'll see what
I can do with it.
I assume the consensus is that we should not back-patch this? It seems
like the kind of behavioral change that people might get annoyed about.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 16:22 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-25 16:22 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; rschaaf@commoninf.com; pgsql-bugs
Tom Lane wrote, On 2015-03-25 15:37:
> This is pretty desperately in need of some comments, but I'll see what
> I can do with it.
Sorry about that. I hope the attached helps.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
nearest_or_even_d3.c (text/x-csrc, 3.2K) download
/* By Pedro Gimeno Fortea, donated to the public domain */
#include <assert.h>
#include <math.h>
#include <stdio.h>
double
rint(double x)
{
/* Perform nearest-or-even rounding. */
double r;
if (x <= 0.0)
{
/* Positive/Negative zero should be returned unchanged. */
if (x == 0.0)
return x;
/*
* Subtracting 0.5 from a number too close to -0.5 can
* round up to exactly -1.0, producing incorrect results,
* so this takes the opposite approach: to add 0.5 to the
* negative number, so that it goes closer to zero (or at
* most to +0.5, which is dealt with later), avoiding the
* precision issue.
*
* r is the result of the rounding.
*/
r = floor(x += 0.5);
/*
* If the rounding did not produce exactly input+0.5 then
* we're mostly done. Just be careful to return minus zero
* when input+0.5 >= 0, as that's what rint should return
* with negative input.
*/
if (r != x || x == 0.0)
return x >= 0.0 ? -0.0 : r;
/*
* We're in the case where the original fractional part was
* exactly 0.5 (detected with floor(input+0.5) == input+0.5).
* We need to round to even. Dividing input+0.5 by 2, taking
* the floor and multiplying by 2 yields the closest even
* number. This part assumes that division by 2 is exact
* (no underflow possible here).
*/
return floor(x * 0.5) * 2.0;
}
/*
* The positive case is similar but using ceil instead of floor,
* except that the case input-0.5 == 0 does not need to be checked,
* as the correct sign is already produced.
*/
r = ceil(x -= 0.5);
if (r != x)
return x <= 0.0 ? 0.0 : r;
return ceil(x * 0.5) * 2.0;
}
int test_equal_z(double a, double b)
{
char a1[20];
char b1[20];
sprintf(a1, "%+g", a);
sprintf(b1, "%+g", b);
return a == b && a1[0] == b1[0];
}
int main()
{
assert(rint( 2.5) == 2.0);
assert(rint( 2.4) == 2.0);
assert(rint( 2.6) == 3.0);
assert(rint( 1.5) == 2.0);
assert(rint( 1.4) == 1.0);
assert(rint( 1.6) == 2.0);
assert(rint(-2.5) == -2.0);
assert(rint(-2.4) == -2.0);
assert(rint(-2.6) == -3.0);
assert(rint(-1.5) == -2.0);
assert(rint(-1.4) == -1.0);
assert(rint(-1.6) == -2.0);
/* Test minus zero */
assert(test_equal_z(rint( 0.0), 0.0));
assert(test_equal_z(rint(-0.0), -0.0));
assert(test_equal_z(rint( 0.5), 0.0));
assert(test_equal_z(rint(-0.5), -0.0));
/* Corner cases for IEEE double */
/* Last float having fractional part */
assert(rint(-4503599627370495.5) == -4503599627370496.0);
assert(rint( 4503599627370495.5) == 4503599627370496.0);
assert(rint(-4503599627370494.5) == -4503599627370494.0);
assert(rint( 4503599627370494.5) == 4503599627370494.0);
/* abs value strictly greater than 0.5 should be rounded away from 0 */
/* (these numbers are 0.5 + 1 ulp) */
assert(rint( 0.5000000000000001) == 1.0);
assert(rint(-0.5000000000000001) == -1.0);
/* abs value strictly less than 0.5 should be rounded towards 0 */
/* (these values plus 1 ulp equal 0.5) */
assert(test_equal_z(rint( 0.49999999999999994), 0.0));
assert(test_equal_z(rint(-0.49999999999999994), -0.0));
/* denormals */
assert(test_equal_z(rint( 5e-324), 0.0));
assert(test_equal_z(rint(-5e-324), -0.0));
assert(test_equal_z(rint( 1e-322), 0.0));
assert(test_equal_z(rint(-1e-322), -0.0));
return 0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 17:39 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Pedro Gimeno <pgsql-004@personal.formauri.es>
0 siblings, 1 reply; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-25 17:39 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Michael Paquier <michael.paquier@gmail.com>; +Cc: rschaaf@commoninf.com; pgsql-bugs
Pedro Gimeno wrote, On 2015-03-25 17:22:
> Sorry about that. I hope the attached helps.
The previous version left out a corner case. Attached is a function and
test suite to deal with it.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
nearest_or_even_d4.c (text/x-csrc, 5.0K) download
/* By Pedro Gimeno Fortea, donated to the public domain */
#include <assert.h>
#include <math.h>
#include <stdio.h>
double
rint(double x)
{
/* Perform nearest-or-even rounding. */
double r;
if (x <= 0.0)
{
/* Positive/Negative zero should be returned unchanged. */
if (x == 0.0)
return x;
/*
* Subtracting 0.5 from a number too close to -0.5 can
* round up to exactly -1.0, producing incorrect results,
* so this takes the opposite approach: to add 0.5 to the
* negative number, so that it goes closer to zero (or at
* most to +0.5, which is dealt with later), avoiding the
* precision issue.
*
* For too big numbers the float may have no decimals.
* That case is detected with x+1.0 == x+0.5; when that
* happens, the number is returned unchanged.
*
* r is the result of the rounding.
*/
r = x;
x += 0.5;
if (x == r + 1.0)
return r;
r = floor(x);
/*
* If the rounding did not produce exactly input+0.5 then
* we're mostly done. Just be careful to return minus zero
* when input+0.5 >= 0, as that's what rint should return
* with negative input.
*/
if (r != x || x == 0.0)
return x >= 0.0 ? -0.0 : r;
/*
* We're in the case where the original fractional part was
* exactly 0.5 (detected with floor(input+0.5) == input+0.5).
* We need to round to even. Dividing input+0.5 by 2, taking
* the floor and multiplying by 2 yields the closest even
* number. This part assumes that division by 2 is exact
* (no underflow possible here).
*/
return floor(x * 0.5) * 2.0;
}
/*
* The positive case is similar but with signs inverted and using
* ceil instead of floor, except that the case input-0.5 == 0 does
* not need to be checked, as the correct sign is already produced.
*/
r = x;
x -= 0.5;
if (x == r - 1.0)
return r;
r = ceil(x);
if (r != x)
return x <= 0.0 ? 0.0 : r;
return ceil(x * 0.5) * 2.0;
}
int test_equal_z(double a, double b)
{
char a1[20];
char b1[20];
sprintf(a1, "%+g", a);
sprintf(b1, "%+g", b);
return a == b && a1[0] == b1[0];
}
int main()
{
assert(rint( 2.5) == 2.0);
assert(rint( 2.4) == 2.0);
assert(rint( 2.6) == 3.0);
assert(rint( 1.5) == 2.0);
assert(rint( 1.4) == 1.0);
assert(rint( 1.6) == 2.0);
assert(rint(-2.5) == -2.0);
assert(rint(-2.4) == -2.0);
assert(rint(-2.6) == -3.0);
assert(rint(-1.5) == -2.0);
assert(rint(-1.4) == -1.0);
assert(rint(-1.6) == -2.0);
/* Test minus zero */
assert(test_equal_z(rint( 0.0), 0.0));
assert(test_equal_z(rint(-0.0), -0.0));
assert(test_equal_z(rint( 0.5), 0.0));
assert(test_equal_z(rint(-0.5), -0.0));
/* Corner cases for IEEE double */
/* Last float having fractional part */
assert(rint(-4503599627370495.5) == -4503599627370496.0);
assert(rint( 4503599627370495.5) == 4503599627370496.0);
assert(rint(-4503599627370494.5) == -4503599627370494.0);
assert(rint( 4503599627370494.5) == 4503599627370494.0);
/* abs value strictly greater than 0.5 should be rounded away from 0 */
/* (these numbers are 0.5 + 1 ulp) */
assert(rint( 0.5000000000000001) == 1.0);
assert(rint(-0.5000000000000001) == -1.0);
/* abs value strictly less than 0.5 should be rounded towards 0 */
/* (these values plus 1 ulp equal 0.5) */
assert(test_equal_z(rint( 0.49999999999999994), 0.0));
assert(test_equal_z(rint(-0.49999999999999994), -0.0));
/* denormals */
assert(test_equal_z(rint( 5e-324), 0.0));
assert(test_equal_z(rint(-5e-324), -0.0));
assert(test_equal_z(rint( 1e-322), 0.0));
assert(test_equal_z(rint(-1e-322), -0.0));
/* bigger numbers */
/* 2^52, +1, +2, +3 */
assert(rint( 4503599627370496.0) == 4503599627370496.0);
assert(rint(-4503599627370496.0) == -4503599627370496.0);
assert(rint( 4503599627370497.0) == 4503599627370497.0);
assert(rint(-4503599627370497.0) == -4503599627370497.0);
assert(rint( 4503599627370498.0) == 4503599627370498.0);
assert(rint(-4503599627370498.0) == -4503599627370498.0);
assert(rint( 4503599627370499.0) == 4503599627370499.0);
assert(rint(-4503599627370499.0) == -4503599627370499.0);
/* 2^53-2, -1, +0, +2 */
assert(rint( 9007199254740990.0) == 9007199254740990.0);
assert(rint(-9007199254740990.0) == -9007199254740990.0);
assert(rint( 9007199254740991.0) == 9007199254740991.0);
assert(rint(-9007199254740991.0) == -9007199254740991.0);
assert(rint( 9007199254740992.0) == 9007199254740992.0);
assert(rint(-9007199254740992.0) == -9007199254740992.0);
assert(rint( 9007199254740994.0) == 9007199254740994.0);
assert(rint(-9007199254740994.0) == -9007199254740994.0);
/* 2^54-2, +0, +4 */
assert(rint( 18014398509481982.0) == 18014398509481982.0);
assert(rint(-18014398509481982.0) == -18014398509481982.0);
assert(rint( 18014398509481984.0) == 18014398509481984.0);
assert(rint(-18014398509481984.0) == -18014398509481984.0);
assert(rint( 18014398509481988.0) == 18014398509481988.0);
assert(rint(-18014398509481988.0) == -18014398509481988.0);
/* 2^55-4 */
assert(rint( 36028797018963964.0) == 36028797018963964.0);
assert(rint(-36028797018963964.0) == -36028797018963964.0);
return 0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 17:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Pedro Gimeno <pgsql-004@personal.formauri.es>
0 siblings, 1 reply; 31+ messages in thread
From: Tom Lane @ 2015-03-25 17:57 UTC (permalink / raw)
To: Pedro Gimeno <pgsql-004@personal.formauri.es>; +Cc: Michael Paquier <michael.paquier@gmail.com>; rschaaf@commoninf.com; pgsql-bugs
Pedro Gimeno <pgsql-004@personal.formauri.es> writes:
> The previous version left out a corner case. Attached is a function and
> test suite to deal with it.
Hmm ... I'm thinking we probably should explicitly check for inf and NaN,
no?
if (isnan(x) || isinf(x))
return x;
It's possible the given coding would return this result anyway by
accident, but that seems rather fragile.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 18:27 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-25 18:27 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; rschaaf@commoninf.com; pgsql-bugs
Tom Lane wrote, On 2015-03-25 18:57:
> Hmm ... I'm thinking we probably should explicitly check for inf and NaN,
> no?
>
> if (isnan(x) || isinf(x))
> return x;
>
> It's possible the given coding would return this result anyway by
> accident, but that seems rather fragile.
I agree about NaN; it worked but mostly by accident. The big number
detection catches infinity unambiguously. Added to a comment in the
attached (plus corresponding tests).
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
nearest_or_even_d5.c (text/x-csrc, 5.2K) download
/* By Pedro Gimeno Fortea, donated to the public domain */
#include <assert.h>
#include <math.h>
#include <stdio.h>
double
rint(double x)
{
/* Perform nearest-or-even rounding. */
double r = x;
if (isnan(x))
return x;
if (x <= 0.0)
{
/* Positive/Negative zero should be returned unchanged. */
if (x == 0.0)
return x;
/*
* Subtracting 0.5 from a number too close to -0.5 can
* round up to exactly -1.0, producing incorrect results,
* so this takes the opposite approach: to add 0.5 to the
* negative number, so that it goes closer to zero (or at
* most to +0.5, which is dealt with later), avoiding the
* precision issue.
*
* For too big numbers the float may have no decimals.
* That case is detected with input+1.0 == input+0.5; when
* that happens, the number is returned unchanged. That
* includes infinity.
*
* r is the result of the rounding.
*/
x += 0.5;
if (x == r + 1.0)
return r;
r = floor(x);
/*
* If the rounding did not produce exactly input+0.5 then
* we're mostly done. Just be careful to return minus zero
* when input+0.5 >= 0, as that's what rint should return
* with negative input.
*/
if (r != x || x == 0.0)
return x >= 0.0 ? -0.0 : r;
/*
* We're in the case where the original fractional part was
* exactly 0.5 (detected with floor(input+0.5) == input+0.5).
* We need to round to even. Dividing input+0.5 by 2, taking
* the floor and multiplying by 2 yields the closest even
* number. This part assumes that division by 2 is exact
* (no underflow possible here).
*/
return floor(x * 0.5) * 2.0;
}
/*
* The positive case is similar but with signs inverted and using
* ceil instead of floor, except that the case input-0.5 == 0 does
* not need to be checked, as the correct sign is already produced.
*/
x -= 0.5;
if (x == r - 1.0)
return r;
r = ceil(x);
if (r != x)
return x <= 0.0 ? 0.0 : r;
return ceil(x * 0.5) * 2.0;
}
int test_equal_z(double a, double b)
{
char a1[20];
char b1[20];
sprintf(a1, "%+g", a);
sprintf(b1, "%+g", b);
return a == b && a1[0] == b1[0];
}
int main()
{
assert(rint( 2.5) == 2.0);
assert(rint( 2.4) == 2.0);
assert(rint( 2.6) == 3.0);
assert(rint( 1.5) == 2.0);
assert(rint( 1.4) == 1.0);
assert(rint( 1.6) == 2.0);
assert(rint(-2.5) == -2.0);
assert(rint(-2.4) == -2.0);
assert(rint(-2.6) == -3.0);
assert(rint(-1.5) == -2.0);
assert(rint(-1.4) == -1.0);
assert(rint(-1.6) == -2.0);
/* Test minus zero */
assert(test_equal_z(rint( 0.0), 0.0));
assert(test_equal_z(rint(-0.0), -0.0));
assert(test_equal_z(rint( 0.5), 0.0));
assert(test_equal_z(rint(-0.5), -0.0));
/* Corner cases for IEEE double */
/* Last float having fractional part */
assert(rint(-4503599627370495.5) == -4503599627370496.0);
assert(rint( 4503599627370495.5) == 4503599627370496.0);
assert(rint(-4503599627370494.5) == -4503599627370494.0);
assert(rint( 4503599627370494.5) == 4503599627370494.0);
/* abs value strictly greater than 0.5 should be rounded away from 0 */
/* (these numbers are 0.5 + 1 ulp) */
assert(rint( 0.5000000000000001) == 1.0);
assert(rint(-0.5000000000000001) == -1.0);
/* abs value strictly less than 0.5 should be rounded towards 0 */
/* (these values plus 1 ulp equal 0.5) */
assert(test_equal_z(rint( 0.49999999999999994), 0.0));
assert(test_equal_z(rint(-0.49999999999999994), -0.0));
/* denormals */
assert(test_equal_z(rint( 5e-324), 0.0));
assert(test_equal_z(rint(-5e-324), -0.0));
assert(test_equal_z(rint( 1e-322), 0.0));
assert(test_equal_z(rint(-1e-322), -0.0));
/* bigger numbers */
/* 2^52, +1, +2, +3 */
assert(rint( 4503599627370496.0) == 4503599627370496.0);
assert(rint(-4503599627370496.0) == -4503599627370496.0);
assert(rint( 4503599627370497.0) == 4503599627370497.0);
assert(rint(-4503599627370497.0) == -4503599627370497.0);
assert(rint( 4503599627370498.0) == 4503599627370498.0);
assert(rint(-4503599627370498.0) == -4503599627370498.0);
assert(rint( 4503599627370499.0) == 4503599627370499.0);
assert(rint(-4503599627370499.0) == -4503599627370499.0);
/* 2^53-2, -1, +0, +2 */
assert(rint( 9007199254740990.0) == 9007199254740990.0);
assert(rint(-9007199254740990.0) == -9007199254740990.0);
assert(rint( 9007199254740991.0) == 9007199254740991.0);
assert(rint(-9007199254740991.0) == -9007199254740991.0);
assert(rint( 9007199254740992.0) == 9007199254740992.0);
assert(rint(-9007199254740992.0) == -9007199254740992.0);
assert(rint( 9007199254740994.0) == 9007199254740994.0);
assert(rint(-9007199254740994.0) == -9007199254740994.0);
/* 2^54-2, +0, +4 */
assert(rint( 18014398509481982.0) == 18014398509481982.0);
assert(rint(-18014398509481982.0) == -18014398509481982.0);
assert(rint( 18014398509481984.0) == 18014398509481984.0);
assert(rint(-18014398509481984.0) == -18014398509481984.0);
assert(rint( 18014398509481988.0) == 18014398509481988.0);
assert(rint(-18014398509481988.0) == -18014398509481988.0);
/* 2^55-4 */
assert(rint( 36028797018963964.0) == 36028797018963964.0);
assert(rint(-36028797018963964.0) == -36028797018963964.0);
assert(rint(INFINITY) == INFINITY);
assert(rint(-INFINITY) == -INFINITY);
assert(isnan(rint(NAN)));
return 0;
}
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 19:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Pedro Gimeno <pgsql-004@personal.formauri.es>
0 siblings, 2 replies; 31+ messages in thread
From: Tom Lane @ 2015-03-25 19:57 UTC (permalink / raw)
To: Pedro Gimeno <pgsql-004@personal.formauri.es>; +Cc: Michael Paquier <michael.paquier@gmail.com>; rschaaf@commoninf.com; pgsql-bugs
Pedro Gimeno <pgsql-004@personal.formauri.es> writes:
> Tom Lane wrote, On 2015-03-25 18:57:
>> Hmm ... I'm thinking we probably should explicitly check for inf and NaN,
>> no?
> I agree about NaN; it worked but mostly by accident. The big number
> detection catches infinity unambiguously. Added to a comment in the
> attached (plus corresponding tests).
I fooled around with this some more for clarity and committed it.
Thanks for your work!
BTW, if memory serves we also have a pretty lazy rounding implementation
for the numeric datatype. I wonder if now would be a good time to upgrade
that to be round-to-nearest-even as well.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-25 23:14 Michael Paquier <michael.paquier@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 31+ messages in thread
From: Michael Paquier @ 2015-03-25 23:14 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
On Thu, Mar 26, 2015 at 4:57 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Pedro Gimeno <pgsql-004@personal.formauri.es> writes:
>> Tom Lane wrote, On 2015-03-25 18:57:
>>> Hmm ... I'm thinking we probably should explicitly check for inf and NaN,
>>> no?
>
>> I agree about NaN; it worked but mostly by accident. The big number
>> detection catches infinity unambiguously. Added to a comment in the
>> attached (plus corresponding tests).
>
> I fooled around with this some more for clarity and committed it.
> Thanks for your work!
Thanks Tom for wrapping up stuff, and Pedro for the patch.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-26 00:19 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 31+ messages in thread
From: Andrew Gierth @ 2015-03-26 00:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Pedro Gimeno <pgsql-004@personal.formauri.es>; Michael Paquier <michael.paquier@gmail.com>; rschaaf@commoninf.com; pgsql-bugs
>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
Tom> BTW, if memory serves we also have a pretty lazy rounding
Tom> implementation for the numeric datatype. I wonder if now would be
Tom> a good time to upgrade that to be round-to-nearest-even as well.
A data point: there have been occasional complaints on IRC about the
fact that numeric rounding is not round-to-even, but obviously it's
harder to tell if anyone is relying on the current behavior.
--
Andrew (irc:RhodiumToad)
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-26 01:27 Michael Paquier <michael.paquier@gmail.com>
parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 2 replies; 31+ messages in thread
From: Michael Paquier @ 2015-03-26 01:27 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
On Thu, Mar 26, 2015 at 9:19 AM, Andrew Gierth
<andrew@tao11.riddles.org.uk> wrote:
>>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
>
> Tom> BTW, if memory serves we also have a pretty lazy rounding
> Tom> implementation for the numeric datatype. I wonder if now would be
> Tom> a good time to upgrade that to be round-to-nearest-even as well.
>
> A data point: there have been occasional complaints on IRC about the
> fact that numeric rounding is not round-to-even, but obviously it's
> harder to tell if anyone is relying on the current behavior.
Indeed...
=# select column1 AS double_value, cast(column1 AS INT) AS int_value
FROM (VALUES (-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.5::numeric),
(1.5::numeric),
(2.5::numeric)) t;
double_value | int_value
--------------+-----------
-2.5 | -3
-1.5 | -2
-0.5 | -1
0.5 | 1
1.5 | 2
2.5 | 3
(6 rows)
Tom, if there is a patch showing up soon, would you integrate it? I
imagine that it would be good to have the same behavior for a maximum
of datatypes in 9.5 now that src/port/rint.c is more compliant.
Regards,
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-26 02:07 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Paquier <michael.paquier@gmail.com>
1 sibling, 0 replies; 31+ messages in thread
From: Tom Lane @ 2015-03-26 02:07 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
Michael Paquier <michael.paquier@gmail.com> writes:
> On Thu, Mar 26, 2015 at 9:19 AM, Andrew Gierth
> <andrew@tao11.riddles.org.uk> wrote:
> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
>>> Tom> BTW, if memory serves we also have a pretty lazy rounding
>>> Tom> implementation for the numeric datatype. I wonder if now would be
>>> Tom> a good time to upgrade that to be round-to-nearest-even as well.
>> A data point: there have been occasional complaints on IRC about the
>> fact that numeric rounding is not round-to-even, but obviously it's
>> harder to tell if anyone is relying on the current behavior.
> Tom, if there is a patch showing up soon, would you integrate it? I
> imagine that it would be good to have the same behavior for a maximum
> of datatypes in 9.5 now that src/port/rint.c is more compliant.
Yes, that's what I was thinking --- if we're changing this in 9.5 for
float (on Windows and whatever other platforms use rint.c) then it would
be sensible to fix numeric at the same time. I will commit a patch if
it shows up, but do not have time to write one myself.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-26 02:12 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 31+ messages in thread
From: Andrew Gierth @ 2015-03-26 02:12 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
>>>>> "Michael" == Michael Paquier <michael.paquier@gmail.com> writes:
Michael> Indeed...
Michael> =# select column1 AS double_value, cast(column1 AS INT) AS
Michael> int_value
The complication for numeric is that there's also the case of round(x,n)
and casting to numeric(m,n) to consider.
--
Andrew (irc:RhodiumToad)
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-26 02:15 Michael Paquier <michael.paquier@gmail.com>
parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 2 replies; 31+ messages in thread
From: Michael Paquier @ 2015-03-26 02:15 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
On Thu, Mar 26, 2015 at 11:12 AM, Andrew Gierth wrote:
>>>>>> "Michael" == Michael Paquier writes:
>
> Michael> Indeed...
> Michael> =# select column1 AS double_value, cast(column1 AS INT) AS
> Michael> int_value
>
> The complication for numeric is that there's also the case of round(x,n)
> and casting to numeric(m,n) to consider.
OK, thanks for the reminder... I forgot this case.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-27 12:32 Michael Paquier <michael.paquier@gmail.com>
parent: Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 31+ messages in thread
From: Michael Paquier @ 2015-03-27 12:32 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
On Thu, Mar 26, 2015 at 11:15 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> On Thu, Mar 26, 2015 at 11:12 AM, Andrew Gierth wrote:
>>>>>>> "Michael" == Michael Paquier writes:
>>
>> Michael> Indeed...
>> Michael> =# select column1 AS double_value, cast(column1 AS INT) AS
>> Michael> int_value
>>
>> The complication for numeric is that there's also the case of round(x,n)
>> and casting to numeric(m,n) to consider.
>
> OK, thanks for the reminder... I forgot this case.
Attached is a patch that adds nearest-to-even rounding for numeric
when dscale = 0. This gives the following results with the previous
query:
=# select column1 AS double_value, cast(column1 AS INT) AS int_value
FROM (VALUES
(-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.5::numeric),
(1.5::numeric),
2.5::numeric)) t;
double_value | int_value
--------------+-----------
-2.5 | -2
-1.5 | -2
-0.5 | 0
0.5 | 0
1.5 | 2
2.5 | 2
(6 rows)
For the case where dscale != 0 case, what would we actually want? I
have been staring at the code around round_var for some time but I
could not really get if we'd really want to change something there...
For example round(2.5::numeric, 1) should be 2.4 and not 2.5?
Regards.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
20150327_numeric_infinite_loop.patch (application/x-patch, 881B) download
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 0c4fcbe..9d8ffe9 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -6965,10 +6965,22 @@ round_var(NumericVar *var, int rscale)
#if DEC_DIGITS == 1
/* di must be zero */
- carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
+ carry = (digits[ndigits] > HALF_NBASE) ? 1 : 0;
#else
if (di == 0)
- carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
+ {
+ carry = 0;
+
+ /*
+ * Set of rules to decide if a digit is carried up using
+ * nearest-to-even approach.
+ */
+ if (ndigits == 0)
+ carry = (digits[ndigits] > HALF_NBASE) ? 1 : 0;
+ else if (ndigits > 0 &&
+ digits[ndigits - 1] % 2 == 1)
+ carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
+ }
else
{
/* Must round within last NBASE digit */
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-27 17:16 Dean Rasheed <dean.a.rasheed@gmail.com>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 2 replies; 31+ messages in thread
From: Dean Rasheed @ 2015-03-27 17:16 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
On 27 March 2015 at 12:32, Michael Paquier <michael.paquier@gmail.com> wrote:
> On Thu, Mar 26, 2015 at 11:15 AM, Michael Paquier
> <michael.paquier@gmail.com> wrote:
>> On Thu, Mar 26, 2015 at 11:12 AM, Andrew Gierth wrote:
>>>>>>>> "Michael" == Michael Paquier writes:
>>>
>>> Michael> Indeed...
>>> Michael> =# select column1 AS double_value, cast(column1 AS INT) AS
>>> Michael> int_value
>>>
>>> The complication for numeric is that there's also the case of round(x,n)
>>> and casting to numeric(m,n) to consider.
>>
>> OK, thanks for the reminder... I forgot this case.
>
> Attached is a patch that adds nearest-to-even rounding for numeric
> when dscale = 0.
I'm not convinced that it is a good idea to change the default
rounding mode for numeric.
For one thing, numeric is commonly used for monetary data, and
changing the default rounding mode might well break existing
applications where rounding is important, and which rely on the
current behaviour. Granted the current rounding mode doesn't appear to
be documented anywhere (which it probably should be), but still, it
seems like a risky thing to change.
I'd also argue that the current "round half away from zero" mode is
the most widely known rounding mode, since it is commonly taught at
school, so it seems like the more sensible default.
ISTM that there would have to be a good reason to change the default,
and compatibility with IEEE floats doesn't seem very convincing to me.
I would never assume numerics are meant to have anything in common
with IEEE floats.
Adding a version of round() with support for a choice of various
rounding modes might be a better way to go.
Also, if the default is being changed, I think that merits wider
discussion (on a thread with a more appropriate title) to see if it is
likely to be an issue for anyone.
Regards,
Dean
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-27 17:53 Pedro Gimeno <pgsql-004@personal.formauri.es>
parent: Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 31+ messages in thread
From: Pedro Gimeno @ 2015-03-27 17:53 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
Michael Paquier wrote, On 2015-03-27 13:32:
> Attached is a patch that adds nearest-to-even rounding for numeric
> when dscale = 0. This gives the following results with the previous
> query:
That produces some quite surprising results:
=# select '2.5'::numeric(9,0)::numeric(9,1),
'2.25'::numeric(9,1)::numeric(9,2),
'2.225'::numeric(9,2)::numeric(9,3),
'2.2225'::numeric(9,3)::numeric(9,4),
'2.22225'::numeric(9,4)::numeric(9,5);
numeric | numeric | numeric | numeric | numeric
---------+---------+---------+---------+---------
2.0 | 2.30 | 2.230 | 2.2230 | 2.22220
(1 row)
That rounding is inconsistent: since DEC_DIGITS is 4 by default, numbers
with a number of digits which is a multiple of 4 + 1 will be rounded to
nearest or even, and numbers with a different number of digits will be
rounded up on a tie. And I have not tested, but apparently when
DEC_DIGITS == 1 (array elements are single digits rather than packing
several) it always rounds down on a tie.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-27 20:55 Michael Paquier <michael.paquier@gmail.com>
parent: Pedro Gimeno <pgsql-004@personal.formauri.es>
0 siblings, 0 replies; 31+ messages in thread
From: Michael Paquier @ 2015-03-27 20:55 UTC (permalink / raw)
To: Pedro Gimeno <pgsql-004@personal.formauri.es>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Tom Lane <tgl@sss.pgh.pa.us>; rschaaf@commoninf.com; pgsql-bugs
On Sat, Mar 28, 2015 at 2:53 AM, Pedro Gimeno
<pgsql-004@personal.formauri.es> wrote:
> Michael Paquier wrote, On 2015-03-27 13:32:
>
>> Attached is a patch that adds nearest-to-even rounding for numeric
>> when dscale = 0. This gives the following results with the previous
>> query:
>
> That produces some quite surprising results:
>
> =# select '2.5'::numeric(9,0)::numeric(9,1),
> '2.25'::numeric(9,1)::numeric(9,2),
> '2.225'::numeric(9,2)::numeric(9,3),
> '2.2225'::numeric(9,3)::numeric(9,4),
> '2.22225'::numeric(9,4)::numeric(9,5);
> numeric | numeric | numeric | numeric | numeric
> ---------+---------+---------+---------+---------
> 2.0 | 2.30 | 2.230 | 2.2230 | 2.22220
> (1 row)
>
> That rounding is inconsistent: since DEC_DIGITS is 4 by default, numbers
> with a number of digits which is a multiple of 4 + 1 will be rounded to
> nearest or even, and numbers with a different number of digits will be
> rounded up on a tie. And I have not tested, but apparently when
> DEC_DIGITS == 1 (array elements are single digits rather than packing
> several) it always rounds down on a tie.
Yeah, I forgot a check on dscale = 0 when deciding to carry to the
upper digit or not.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-27 21:31 Michael Paquier <michael.paquier@gmail.com>
parent: Dean Rasheed <dean.a.rasheed@gmail.com>
1 sibling, 0 replies; 31+ messages in thread
From: Michael Paquier @ 2015-03-27 21:31 UTC (permalink / raw)
To: Dean Rasheed <dean.a.rasheed@gmail.com>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
On Sat, Mar 28, 2015 at 2:16 AM, Dean Rasheed wrote:
> On 27 March 2015 at 12:32, Michael Paquier wrote:
>> Attached is a patch that adds nearest-to-even rounding for numeric
>> when dscale = 0.
>
> I'm not convinced that it is a good idea to change the default
> rounding mode for numeric.
>
> For one thing, numeric is commonly used for monetary data, and
> changing the default rounding mode might well break existing
> applications where rounding is important, and which rely on the
> current behaviour. Granted the current rounding mode doesn't appear to
> be documented anywhere (which it probably should be), but still, it
> seems like a risky thing to change.
It is possible to argue the other way around here. For people
migrating to Postgres, perhaps the behavior that we have is surprising
as well. Perhaps other RDBMS behave using rounding-to-even.
> I'd also argue that the current "round half away from zero" mode is
> the most widely known rounding mode, since it is commonly taught at
> school, so it seems like the more sensible default.
>
> ISTM that there would have to be a good reason to change the default,
> and compatibility with IEEE floats doesn't seem very convincing to me.
> I would never assume numerics are meant to have anything in common
> with IEEE floats.
>
> Adding a version of round() with support for a choice of various
> rounding modes might be a better way to go.
Each argument has its strengths. Consistency with float and rint() is
appealing as well...
> Also, if the default is being changed, I think that merits wider
> discussion (on a thread with a more appropriate title) to see if it is
> likely to be an issue for anyone.
Yeah, that makes sense. I'll raise one on -hackers.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
* Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
@ 2015-03-27 23:55 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: Dean Rasheed <dean.a.rasheed@gmail.com>
1 sibling, 0 replies; 31+ messages in thread
From: Andrew Gierth @ 2015-03-27 23:55 UTC (permalink / raw)
To: Dean Rasheed <dean.a.rasheed@gmail.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Pedro Gimeno <pgsql-004@personal.formauri.es>; rschaaf@commoninf.com; pgsql-bugs
>>>>> "Dean" == Dean Rasheed <dean.a.rasheed@gmail.com> writes:
Dean> I'm not convinced that it is a good idea to change the default
Dean> rounding mode for numeric.
Dean> For one thing, numeric is commonly used for monetary data, and
Dean> changing the default rounding mode might well break existing
Dean> applications where rounding is important, and which rely on the
Dean> current behaviour.
On the other hand, we've had complaints (on IRC) from people doing
calculations with monetary data who complain that we don't do
round-to-even (which is widely known as "Banker's Rounding") as their
requirements demand.
To quote the last person who brought it up there:
>> We have strict accounting practice to follow -- and our CFO has
>> instructed to use even-rounding (aka bankers rounding)... and to not
>> use our typical rounding behavior we learned in 1st grade. :)
Dean> Adding a version of round() with support for a choice of various
Dean> rounding modes might be a better way to go.
There's still the issue of casting to ::numeric(m,n) to consider, and
whatever internal rounding is being done in division and numeric
functions.
Dean> Also, if the default is being changed, I think that merits wider
Dean> discussion (on a thread with a more appropriate title) to see if
Dean> it is likely to be an issue for anyone.
Absolutely.
--
Andrew (irc:RhodiumToad)
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 31+ messages in thread
end of thread, other threads:[~2015-03-27 23:55 UTC | newest]
Thread overview: 31+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-03-20 19:43 BUG #12885: The result of casting a double to an integer depends on the database version rschaaf@commoninf.com
2015-03-23 14:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-24 02:27 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-24 02:42 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-24 03:12 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-24 06:25 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-24 13:37 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-25 00:19 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-25 02:11 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-25 03:26 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-25 10:32 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-25 11:18 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-25 11:46 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-25 14:37 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-25 16:22 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-25 17:39 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-25 17:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-25 18:27 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-25 19:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-25 23:14 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-26 00:19 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2015-03-26 01:27 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-26 02:07 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-03-26 02:12 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2015-03-26 02:15 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-27 12:32 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-27 17:16 ` Dean Rasheed <dean.a.rasheed@gmail.com>
2015-03-27 21:31 ` Michael Paquier <michael.paquier@gmail.com>
2015-03-27 23:55 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2015-03-27 17:53 ` Pedro Gimeno <pgsql-004@personal.formauri.es>
2015-03-27 20:55 ` Michael Paquier <michael.paquier@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox