Integer range?

Поиск
Список
Период
Сортировка
Искать

Integer range?

От:
Scott Ribe <scott_ribe@killerbytes.com>
Дата:
The range of a twos-complement 32-bit integer is -2147483648 through
2147483647. Yet in Postgres:


# select -2147483647::int4;
  ?column?   
-------------
 -2147483647
(1 row)

# select -2147483648::int4;
ERROR:  integer out of range


Is this a bug? Or something required by the SQL standard?

(8.3.7, OS X 10.5.8, 32-bit build)

-- 
Scott Ribe
scott_ribe@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice


Re: Integer range?

От:
Steve Atkins <steve@blighty.com>
Дата:

On Oct 9, 2009, at 9:46 AM, Scott Ribe wrote:

> The range of a twos-complement 32-bit integer is -2147483648 through
> 2147483647. Yet in Postgres:
>
>
> # select -2147483647::int4;
>  ?column?
> -------------
> -2147483647
> (1 row)
>
> # select -2147483648::int4;
> ERROR:  integer out of range
>
>
> Is this a bug? Or something required by the SQL standard?

Neither, really. The cast shortcut you're using is binding to the  
digits more tightly than the minus prefix.

So what you end up with is the integer 2147483648, with a unary minus  
in front of it, pretty much like this:

# select -(2147483648::int4);
ERROR:  integer out of range

While what you want is more like this:

# select '-2147483648'::int4;
     int4
-------------
  -2147483648
(1 row)

# select cast(-2147483648 as int4);
     int4
-------------
  -2147483648
(1 row)

Cheers,
   Steve

Re: Integer range?

От:
Scott Ribe <scott_ribe@killerbytes.com>
Дата:
> Neither, really. The cast shortcut you're using is binding to the
> digits more tightly than the minus prefix.

I see, thanks.

-- 
Scott Ribe
scott_ribe@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice


FAQ