Re: integer instead of 'double precision'?

Поиск
Список
Период
Сортировка
От Martijn van Oosterhout
Тема Re: integer instead of 'double precision'?
Дата
Msg-id 20121113071240.GA32072@svana.org
обсуждение исходный текст
Ответ на Re: integer instead of 'double precision'?  (Willy-Bas Loos <willybas@gmail.com>)
Список 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

Вложения

В списке pgsql-general по дате отправления:

Предыдущее
От: Craig Ringer
Дата:
Сообщение: Re: Enabling Autovacuum Postgres 9.1 (was Unexpectedly high disk space usage)
Следующее
От: "Albe Laurenz"
Дата:
Сообщение: Re: Understanding streaming replication