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

From: "Harvey, Allan AC" <HarveyA(at)OneSteel(dot)com>
To: "Alban Hertroys" <haramrae(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Should casting to integer produce same result as trunc()
Date: 2011-10-11 22:08:19
Message-ID: E97A5BB7699CAD48BE2711E712471165073F8F49@ntlmsg03.onesteel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: Alban Hertroys [mailto:haramrae(at)gmail(dot)com]
> Sent: Wednesday, 12 October 2011 1:35 AM
> To: Tom Lane
> Cc: Harvey, Allan AC; pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] Should casting to integer produce same
> result as trunc()
>
> On 11 October 2011 15:41, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > Alban Hertroys <haramrae(at)gmail(dot)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.

Thanks guys, yes C/C++ is my poison, forming my expectations.

Allan

The material contained in this email may be confidential, privileged or copyrighted. If you are not the intended recipient, use, disclosure or copying of this information is prohibited. If you have received this document in error, please advise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses contained in this email or any attachments.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sean Laurent 2011-10-11 22:38:26 Re: Postgres 9.01, Amazon EC2/EBS, XFS, JDBC and lost connections
Previous Message Merlin Moncure 2011-10-11 22:02:23 Re: libpq 8.3 and 8.4 interfaces