Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YaymF-0004b4-Aj for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Mar 2015 03:48:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YaymE-0005IT-2z for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Mar 2015 03:48:14 +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 1Yao4N-0000xQ-LE for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 16:22:15 +0000 Received: from formauri.es ([78.47.205.180]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yao4J-000377-BN for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 16:22:13 +0000 Received: from [0.0.0.0] (formauri.es [78.47.205.180]) by formauri.es (Postfix) with ESMTPS id CB919804A2; Wed, 25 Mar 2015 17:22:08 +0100 (CET) Message-ID: <5512E0AF.9050103@personal.formauri.es> Date: Wed, 25 Mar 2015 17:22:07 +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: Tom Lane CC: Michael Paquier , rschaaf@commoninf.com, pgsql-bugs@postgresql.org Subject: Re: 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> <55129971.2070208@personal.formauri.es> <3736.1427294258@sss.pgh.pa.us> In-Reply-To: <3736.1427294258@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------070806000806080000060404" 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. --------------070806000806080000060404 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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. --------------070806000806080000060404 Content-Type: text/x-csrc; name="nearest_or_even_d3.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="nearest_or_even_d3.c" /* By Pedro Gimeno Fortea, donated to the public domain */ #include #include #include 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; } --------------070806000806080000060404 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 --------------070806000806080000060404--