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 1gQL6H-0000NW-Oe for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Nov 2018 23:43:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gQL6G-0002N4-Dg for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Nov 2018 23:43:04 +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 1gQL6G-0002Mx-3w for pgsql-bugs@lists.postgresql.org; Fri, 23 Nov 2018 23:43:04 +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 1gQL6C-0004U6-7e for pgsql-bugs@lists.postgresql.org; Fri, 23 Nov 2018 23:43:02 +0000 Received: from [192.168.127.1] (port=53630 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 1gQL63-000D8J-Mv; Fri, 23 Nov 2018 23:42:51 +0000 Received: from [127.0.0.1] (port=20875 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gQL63-0000hy-13; Fri, 23 Nov 2018 23:42:51 +0000 From: Andrew Gierth To: Tom Lane Cc: victor@magic.io, 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: <13162.1543014227@sss.pgh.pa.us> (Tom Lane's message of "Fri, 23 Nov 2018 18:03:47 -0500") Message-ID: <87zhtzqs9o.fsf@news-spur.riddles.org.uk> References: <15519-4fc785b483201ff1@postgresql.org> <874lc7sb1a.fsf@news-spur.riddles.org.uk> <13162.1543014227@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 23 Nov 2018 23:42:50 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Tom" == Tom Lane writes: Tom> /* Tom> * Range check. We must be careful here that the boundary values are Tom> * expressed exactly in the appropriate float domain; we assume float4 Tom> * is going to round off INT_MAX to a power of 2. Also note assumption Tom> * that rint() will pass through a NaN or Inf unchanged. Tom> */ Tom> if (unlikely(num < (float4) INT_MIN || num >= (float4) INT_MAX || isnan(num))) Tom> ereport(...); Looking around, another approach I've seen (which I like better) is to use if (num < (float4)INT_MIN || num >= -(float4)INT_MIN || ... which doesn't rely on assumptions about how the compiler is going to round INT_MAX. (It does rely on two's complement representation of ints, but that assumption is known to be safe.) -- Andrew (irc:RhodiumToad)