Re: integer instead of 'double precision'?

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Willy-Bas Loos <willybas(at)gmail(dot)com>
Cc: Guillaume Lelarge <guillaume(at)lelarge(dot)info>, Henry Drexler <alonup8tb(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: integer instead of 'double precision'?
Date: 2012-11-13 07:12:40
Message-ID: 20121113071240.GA32072@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)lelarge(dot)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(at)svana(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2012-11-13 08:31:24 Re: Understanding streaming replication
Previous Message Craig Ringer 2012-11-13 02:32:32 Re: Enabling Autovacuum Postgres 9.1 (was Unexpectedly high disk space usage)