Re: Should casting to integer produce same result as trunc()

Поиск
Список
Период
Сортировка
От Alban Hertroys
Тема Re: Should casting to integer produce same result as trunc()
Дата
Msg-id CAF-3MvNxkJ5REqp+Nqxtsd2LjQ=+gVGeb97F1BCeNhmpw1cBMQ@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Should casting to integer produce same result as trunc()  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: Should casting to integer produce same result as trunc()
Re: Should casting to integer produce same result as trunc()
Список pgsql-general
On 11 October 2011 15:41, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Alban Hertroys <haramrae@gmail.com> writes:
>> On 11 Oct 2011, at 2:55, Harvey, Allan AC wrote:
>>> My simple understanding of trunc() and casting to an integer says that
>>> there is a bug here.
>
>> Which the type-cast should round to 4380103 and 4380104 respectively.
>> It doesn't:
>
> That's because a cast from float to int rounds, it doesn't truncate.
>
> regression=# select (4.7::float8)::int;
>  int4
> ------
>    5
> (1 row)

I figured it would be something like that. Is that how it's defined in
the SQL standard?

All other programming languages I've come to know truncate floats on
such casts (C, php, python, java, to name a few).
This is probably quite surprising to people used to these languages.

I agree that rounding is the right thing to do. We are probably just
too used to programming languages that truncate instead of round
because it was more convenient to implement it that way at the time.

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

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

Предыдущее
От: Alban Hertroys
Дата:
Сообщение: Re: Global Variables?
Следующее
От: Sandro Santilli
Дата:
Сообщение: Re: [postgis-users] Query slow down, never completes