Re: integer instead of 'double precision'? - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: integer instead of 'double precision'?
Date
Msg-id 20121113071240.GA32072@svana.org
Whole thread Raw
In response to Re: integer instead of 'double precision'?  (Willy-Bas Loos <willybas@gmail.com>)
List pgsql-general
On Mon, Nov 12, 2012 at 02:16:21PM +0100, Willy-Bas Loos wrote:
> On Fri, Sep 9, 2011 at 5:09 PM, Guillaume Lelarge <guillaume@lelarge.info>wrote:
>
> > You divide an integer with an integer, that should give you an integer.
> >
>
> Can you tell me the reasoning behind that idea?
> Is it a rule that the output type of an operator must equal the input type?
> In this case that doesn't seem locigal. I think that the "/" operator
> should return something that allows fractions, since the operator creates
> fractions so frequently.

The thing is, you often do need the version that truncates. It's
supported by the underlying system and if you want a float as output
you can cast one of the arguments to float to do that.  It's been like
this forever (C does it too for example).

For integers it may help if you think of it in combination with the
modulus operator (%).

Python 3 recently changed to give float output by default, but also
provides a // operator to access the truncated version.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> He who writes carelessly confesses thereby at the very outset that he does
> not attach much importance to his own thoughts.
   -- Arthur Schopenhauer

Attachment

pgsql-general by date:

Previous
From: Craig Ringer
Date:
Subject: Re: Enabling Autovacuum Postgres 9.1 (was Unexpectedly high disk space usage)
Next
From: "Albe Laurenz"
Date:
Subject: Re: Understanding streaming replication