Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error - Mailing list pgsql-bugs

From Andrew Gierth
Subject Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Date
Msg-id 87zhtzqs9o.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> 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)


pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Next
From: Tom Lane
Date:
Subject: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error