Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gQJpC-0004rA-Qz for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Nov 2018 22:21:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gQJpB-00056k-1C for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Nov 2018 22:21:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gQJpA-00056d-NI for pgsql-bugs@lists.postgresql.org; Fri, 23 Nov 2018 22:21:20 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gQJp3-0002mG-3Q for pgsql-bugs@lists.postgresql.org; Fri, 23 Nov 2018 22:21:19 +0000 Received: from [192.168.127.1] (port=21897 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gQJoy-000Czm-Hs; Fri, 23 Nov 2018 22:21:08 +0000 Received: from [127.0.0.1] (port=47793 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gQJox-0000LL-RH; Fri, 23 Nov 2018 22:21:07 +0000 From: Andrew Gierth To: victor@magic.io Cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error In-Reply-To: <15519-4fc785b483201ff1@postgresql.org> (PG Bug reporting form's message of "Fri, 23 Nov 2018 22:03:56 +0000") Message-ID: <874lc7sb1a.fsf@news-spur.riddles.org.uk> References: <15519-4fc785b483201ff1@postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 23 Nov 2018 22:21:07 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form writes: PG> The following bug has been logged on the website: PG> Bug reference: 15519 PG> Logged by: Victor Petrovykh PG> Email address: victor@magic.io PG> PostgreSQL version: 10.5 PG> Operating system: Gentoo Linux 4.14.20 PG> Description: PG> Offending examples: PG> SELECT ((2147483647::float4) - 1.0::float4)::int4; PG> SELECT ((2147483590::float4) - 1.0::float4)::int4; PG> SELECT ((2147483647::float4) + 1.0::float4)::int4; PG> They all produce the same result: -2147483648 The bug here is that the ftoi4 conversion function thinks it can do this: if (num < INT_MIN || num > INT_MAX || isnan(num)) but this causes INT_MIN and INT_MAX to be implicitly converted to floats (not doubles), which can't represent their values exactly and ends up with the conditions being false in the edge cases. (Specifically, (float)INT_MAX is a bit larger than INT_MAX.) Probably this should be changed to use ((float8) INT_MAX) etc. instead? PG> I understand that a float4 cannot represent large integers with the PG> same precision as int4, that's OK. What surprised me is that PG> instead of getting an "overflow error" or "integer out of range" I PG> simply got a negative result for a value that is actually close to PG> maximum int4. To contrast this, the query: PG> SELECT ((2147483647::float4) + 200.0::float4)::int4; PG> The above produces the expected "ERROR: integer out of range" Likewise you also get that error if you cast the float4 to a float8 before casting to integer. -- Andrew (irc:RhodiumToad)