Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YanWh-0003Wz-PG for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Mar 2015 15:47:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YanWg-0006iv-RL for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Mar 2015 15:47:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Yaic2-0005mQ-PS for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 10:32:38 +0000 Received: from formauri.es ([78.47.205.180]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yaibv-0002qB-Ut for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 10:32:34 +0000 Received: from [0.0.0.0] (formauri.es [78.47.205.180]) by formauri.es (Postfix) with ESMTPS id 1E4AC80653; Wed, 25 Mar 2015 11:32:28 +0100 (CET) Message-ID: <55128EBB.4030008@personal.formauri.es> Date: Wed, 25 Mar 2015 11:32:27 +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> <55121967.90308@personal.formauri.es> In-Reply-To: Content-Type: multipart/mixed; boundary="------------010106070500030704050704" 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. --------------010106070500030704050704 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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. --------------010106070500030704050704 Content-Type: text/x-csrc; name="nearest_or_even_d.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="nearest_or_even_d.c" /* By Pedro Gimeno Fortea, donated to the public domain */ #include #include 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; } --------------010106070500030704050704 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 --------------010106070500030704050704--