Re: integer instead of 'double precision'?

From: Guillaume Lelarge <guillaume(at)lelarge(dot)info>
To: Henry Drexler <alonup8tb(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: integer instead of 'double precision'?
Date: 2011-09-09 15:09:58
Message-ID: 1315580999.2456.3.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 2011-09-09 at 10:42 -0400, Henry Drexler wrote:
> take any table and run
>
> Query
> ---------------------
> select
> 1/3
> from
> storage
> limit 1
>
>
> Result
> ---------------------
> ?column?
> integer
> 0
>
>
> Expected Result
> ---------------------
> ?column?
> double precision
> 0.33333...
>
>
>
> Question
> ---------------------
> Since there is no column type to begin with as this is a made-up column,
> shouldn't postgres know it is double precision due to the remainder?
>

You divide an integer with an integer, that should give you an integer.
And that's exactly what it does.

> I thought perhaps I could cast it as double precision as noted on
> http://www.postgresql.org/docs/8.3/interactive/sql-expressions.html
>
> though doing the following:
>
> select
> float8(1/3)
> from
> storage
> limit 1
>
> results in:
>
> float8
> double precision
> 0
>

You still divide an integer with an integer. 1/3 as integers has a
result of 0. You then cast it to float which gives you the value 0 in
double precision.

> any ideas on how to get this type of a manufactured column (not sure the
> right term for it) to show the double precision result?

Sure, do select 1./3 from... or select float8(1)/3...

--
Guillaume
http://blog.guillaume.lelarge.info
http://www.dalibo.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2011-09-09 15:12:41 Re: integer instead of 'double precision'?
Previous Message Henry Drexler 2011-09-09 15:07:48 Re: integer instead of 'double precision'?