Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YacaY-00050V-2v for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Mar 2015 04:06:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YacaX-0003mp-I9 for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Mar 2015 04:06:41 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Yaanf-0001pk-O5 for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 02:12:07 +0000 Received: from formauri.es ([78.47.205.180]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yaanb-0000GU-9E for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 02:12:05 +0000 Received: from [0.0.0.0] (formauri.es [78.47.205.180]) by formauri.es (Postfix) with ESMTPS id 89C3580653; Wed, 25 Mar 2015 03:11:59 +0100 (CET) Message-ID: <55121967.90308@personal.formauri.es> Date: Wed, 25 Mar 2015 03:11:51 +0100 From: Pedro Gimeno User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Icedove/24.7.0 MIME-Version: 1.0 To: Michael Paquier CC: Tom Lane , rschaaf@commoninf.com, pgsql-bugs@postgresql.org Subject: Re: BUG #12885: The result of casting a double to an integer depends on the database version References: <20150320194337.2573.72944@wrigleys.postgresql.org> <32478.1427122083@sss.pgh.pa.us> <26862.1427166720@sss.pgh.pa.us> <13096.1427204266@sss.pgh.pa.us> In-Reply-To: Content-Type: multipart/mixed; boundary="------------010503050708040000050906" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------010503050708040000050906 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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. --------------010503050708040000050906 Content-Type: text/x-csrc; name="nearest_or_even.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="nearest_or_even.c" /* By Pedro Gimeno Fortea, donated to the public domain */ #include #include 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; } --------------010503050708040000050906 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------010503050708040000050906--