pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pedro Gimeno <pgsql-004@personal.formauri.es>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: rschaaf@commoninf.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
Date: Wed, 25 Mar 2015 19:27:35 +0100
Message-ID: <5512FE17.6020702@personal.formauri.es> (raw)
In-Reply-To: <8192.1427306273@sss.pgh.pa.us>
References: <20150320194337.2573.72944@wrigleys.postgresql.org>
	<32478.1427122083@sss.pgh.pa.us>
	<CAB7nPqQjby+5wAc-gcD77HZRA44CJpbxrD9t83s8C7cT7D5o9g@mail.gmail.com>
	<CAB7nPqRHostL3woG9uV5ZJDH5NwvOcNiU91LQ13W5emL4iRcaw@mail.gmail.com>
	<26862.1427166720@sss.pgh.pa.us>
	<CAB7nPqST=EUbvsx-mapemgA0jD9e9sgVezaV-UvtkeMfXyrigw@mail.gmail.com>
	<13096.1427204266@sss.pgh.pa.us>
	<CAB7nPqSkh+Kma8+oGd5w67qZzSaVtkFo-2dgHwf5MAsr+enk4g@mail.gmail.com>
	<55121967.90308@personal.formauri.es>
	<CAB7nPqRRyhDtNMKC=vuTpO8XRwMEigyFh2VttnkC_eE7LbTAXQ@mail.gmail.com>
	<55129971.2070208@personal.formauri.es>
	<CAB7nPqQ=as+tSE6kA36RLpbueb__Zj_=L+wWonKMH5-nczj3yg@mail.gmail.com>
	<3736.1427294258@sss.pgh.pa.us>
	<5512E0AF.9050103@personal.formauri.es>
	<5512F2C0.9080007@personal.formauri.es>
	<8192.1427306273@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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; }

view thread (31+ messages)  latest in thread

Message-ID: <5512FE17.6020702@personal.formauri.es>
Permalink:  ../5512FE17.6020702@personal.formauri.es/
Also on:    postgresql.org/message-id/5512FE17.6020702@personal.formauri.es

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: pgsql-004@personal.formauri.es, tgl@sss.pgh.pa.us, michael.paquier@gmail.com, rschaaf@commoninf.com
  Subject: Re: BUG #12885: The result of casting a double to an integer depends on the database version
  In-Reply-To: <5512FE17.6020702@personal.formauri.es>
  References: <20150320194337.2573.72944@wrigleys.postgresql.org> <32478.1427122083@sss.pgh.pa.us> <CAB7nPqQjby+5wAc-gcD77HZRA44CJpbxrD9t83s8C7cT7D5o9g@mail.gmail.com> <CAB7nPqRHostL3woG9uV5ZJDH5NwvOcNiU91LQ13W5emL4iRcaw@mail.gmail.com> <26862.1427166720@sss.pgh.pa.us> <CAB7nPqST=EUbvsx-mapemgA0jD9e9sgVezaV-UvtkeMfXyrigw@mail.gmail.com> <13096.1427204266@sss.pgh.pa.us> <CAB7nPqSkh+Kma8+oGd5w67qZzSaVtkFo-2dgHwf5MAsr+enk4g@mail.gmail.com> <55121967.90308@personal.formauri.es> <CAB7nPqRRyhDtNMKC=vuTpO8XRwMEigyFh2VttnkC_eE7LbTAXQ@mail.gmail.com> <55129971.2070208@personal.formauri.es> <CAB7nPqQ=as+tSE6kA36RLpbueb__Zj_=L+wWonKMH5-nczj3yg@mail.gmail.com> <3736.1427294258@sss.pgh.pa.us> <5512E0AF.9050103@personal.formauri.es> <5512F2C0.9080007@personal.formauri.es> <8192.1427306273@sss.pgh.pa.us> <5512FE17.6020702@personal.formauri.es>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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