Thread: 8.1.2 -32768::smallint

8.1.2 -32768::smallint

From
Jean-Gérard Pailloncy
Date:
Hi,

On PostgreSQL 8.1.2
select -32768::smallint
throws the error
ERROR:  smallint out of range

select -32767::smallint
is OK.

The documentation states that -32768 is OK.
http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-NUMERIC

Cordialement,
Jean-Gérard Pailloncy



Re: 8.1.2 -32768::smallint

From
Michael Fuhr
Date:
On Wed, Dec 13, 2006 at 03:03:43PM -0000, Jean-Gérard Pailloncy wrote:
> On PostgreSQL 8.1.2
> select -32768::smallint
> throws the error
> ERROR:  smallint out of range

I think the cast is binding tighter than the unary minus, so the
above is equivalent to

select -(32768::smallint)

which is why you're getting "smallint out of range."  This should work:

select (-32768)::smallint

--
Michael Fuhr

Re: 8.1.2 -32768::smallint

From
Michael Fuhr
Date:
On Wed, Dec 13, 2006 at 08:34:38AM -0700, Michael Fuhr wrote:
> On Wed, Dec 13, 2006 at 03:03:43PM -0000, Jean-Gérard Pailloncy wrote:
> > On PostgreSQL 8.1.2
> > select -32768::smallint
> > throws the error
> > ERROR:  smallint out of range
>
> I think the cast is binding tighter than the unary minus,

Indeed it is; see the Operator Precedence table:

http://www.postgresql.org/docs/8.1/interactive/sql-syntax.html#SQL-PRECEDENCE

--
Michael Fuhr

Re: 8.1.2 -32768::smallint

From
Jean-Gérard Pailloncy
Date:
> select -(32768::smallint)
>
> which is why you're getting "smallint out of range."  This should work:
>
> select (-32768)::smallint
Exact. Thank you for the answer.
But really conter-intuitive.

Cordialement,
Jean-Gérard Pailloncy