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

Поиск
Список
Период
Сортировка
От Andrew Gierth
Тема Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Дата
Msg-id 87zhtzqs9o.fsf@news-spur.riddles.org.uk
обсуждение исходный текст
Ответ на 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>)
Ответы 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>)
Список 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 по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Следующее
От: Tom Lane
Дата:
Сообщение: Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error