Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YaymP-0004bS-Jj for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Mar 2015 03:48:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YaymP-0005SP-21 for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Mar 2015 03:48:25 +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 1YapGz-0003sk-Ce for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 17:39:21 +0000 Received: from formauri.es ([78.47.205.180]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YapGu-0003zZ-J5 for pgsql-bugs@postgresql.org; Wed, 25 Mar 2015 17:39:18 +0000 Received: from [0.0.0.0] (formauri.es [78.47.205.180]) by formauri.es (Postfix) with ESMTPS id 2F2DD804A2; Wed, 25 Mar 2015 18:39:13 +0100 (CET) Message-ID: <5512F2C0.9080007@personal.formauri.es> Date: Wed, 25 Mar 2015 18:39:12 +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 , Michael Paquier CC: 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> <5512E0AF.9050103@personal.formauri.es> In-Reply-To: <5512E0AF.9050103@personal.formauri.es> Content-Type: multipart/mixed; boundary="------------040702030003010404070504" 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. --------------040702030003010404070504 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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. --------------040702030003010404070504 Content-Type: text/x-csrc; name="nearest_or_even_d4.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="nearest_or_even_d4.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. * * 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; } --------------040702030003010404070504 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 --------------040702030003010404070504--