pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Round integer division
7+ messages / 4 participants
[nested] [flat]

* Round integer division
@ 2010-06-25 22:53 Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2010-06-25 23:00 ` Re: Round integer division Scott Marlowe <scott.marlowe@gmail.com>
  2010-06-25 23:25 ` Re: Round integer division Adrian Klaver <adrian.klaver@gmail.com>
  2010-06-26 13:09 ` Re: Round integer division Jasen Betts <jasen@xnet.co.nz>
  0 siblings, 3 replies; 7+ messages in thread

From: Lee Hachadoorian @ 2010-06-25 22:53 UTC (permalink / raw)
  To: pgsql-sql

Is it documented anywhere that floating-point numbers round
"scientifically", that is 0.5 rounds to the nearest even number? Compare:

SELECT round(2.5::real), round(2.5::numeric), round(3.5::real),
round(3.5::numeric);

generates

2 | 3 | 4 | 4

I stumbled across this when I was trying to use round(a::real/b::real)
to generate a rounded result to dividing integers, and noticed sometimes
0.5 was truncated and sometimes it was rounded up. Couldn't find
anything about this in the archives or the data type documentation. Is
there something obvious that I'm I missing?

Thanks,

-- 

Lee Hachadoorian
PhD Student, Geography
Program in Earth & Environmental Sciences
CUNY Graduate Center




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Round integer division
  2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
@ 2010-06-25 23:00 ` Scott Marlowe <scott.marlowe@gmail.com>
  2010-06-25 23:07   ` Re: Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2 siblings, 1 reply; 7+ messages in thread

From: Scott Marlowe @ 2010-06-25 23:00 UTC (permalink / raw)
  To: Lee Hachadoorian <lee.hachadoorian@gmail.com>; +Cc: pgsql-sql

On Fri, Jun 25, 2010 at 6:53 PM, Lee Hachadoorian
<lee.hachadoorian@gmail.com> wrote:
> Is it documented anywhere that floating-point numbers round
> "scientifically", that is 0.5 rounds to the nearest even number? Compare:
>
> SELECT round(2.5::real), round(2.5::numeric), round(3.5::real),
> round(3.5::numeric);
>
> generates
>
> 2 | 3 | 4 | 4
>
> I stumbled across this when I was trying to use round(a::real/b::real)
> to generate a rounded result to dividing integers, and noticed sometimes
> 0.5 was truncated and sometimes it was rounded up. Couldn't find
> anything about this in the archives or the data type documentation. Is
> there something obvious that I'm I missing?

That all floating point representations are approximate?



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Round integer division
  2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2010-06-25 23:00 ` Re: Round integer division Scott Marlowe <scott.marlowe@gmail.com>
@ 2010-06-25 23:07   ` Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2010-06-26 13:18     ` Re: Round integer division Jasen Betts <jasen@xnet.co.nz>
  0 siblings, 1 reply; 7+ messages in thread

From: Lee Hachadoorian @ 2010-06-25 23:07 UTC (permalink / raw)
  To: Scott Marlowe <scott.marlowe@gmail.com>; +Cc: pgsql-sql



On 06/25/2010 07:00 PM, Scott Marlowe wrote:
> That all floating point representations are approximate?
>
But if it's error due to approximation, shouldn't the result be random?
I tried this for a handful of larger numbers, and it appears to
consistently round to the even number. Wouldn't that have to be
intentionally programmed that way?

--Lee

-- 
Lee Hachadoorian
PhD Student, Geography
Program in Earth & Environmental Sciences
CUNY Graduate Center




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Round integer division
  2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2010-06-25 23:00 ` Re: Round integer division Scott Marlowe <scott.marlowe@gmail.com>
  2010-06-25 23:07   ` Re: Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
@ 2010-06-26 13:18     ` Jasen Betts <jasen@xnet.co.nz>
  0 siblings, 0 replies; 7+ messages in thread

From: Jasen Betts @ 2010-06-26 13:18 UTC (permalink / raw)
  To: pgsql-sql

On 2010-06-25, Lee Hachadoorian <lee.hachadoorian@gmail.com> wrote:
>
>
> On 06/25/2010 07:00 PM, Scott Marlowe wrote:
>> That all floating point representations are approximate?
>>
> But if it's error due to approximation, shouldn't the result be random?
> I tried this for a handful of larger numbers, and it appears to
> consistently round to the even number. Wouldn't that have to be
> intentionally programmed that way?

Yes it is, an enginneer at intel made that decision in the 70s when
the 8087 coprocessor was developed.

If you're not running on x86-like hardware the round operation may
do something different.



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Round integer division
  2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
@ 2010-06-25 23:25 ` Adrian Klaver <adrian.klaver@gmail.com>
  2010-06-25 23:32   ` Re: Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2 siblings, 1 reply; 7+ messages in thread

From: Adrian Klaver @ 2010-06-25 23:25 UTC (permalink / raw)
  To: pgsql-sql; +Cc: Lee Hachadoorian <lee.hachadoorian@gmail.com>

On Friday 25 June 2010 3:53:01 pm Lee Hachadoorian wrote:
> Is it documented anywhere that floating-point numbers round
> "scientifically", that is 0.5 rounds to the nearest even number? Compare:
>
> SELECT round(2.5::real), round(2.5::numeric), round(3.5::real),
> round(3.5::numeric);
>
> generates
>
> 2 | 3 | 4 | 4
>
> I stumbled across this when I was trying to use round(a::real/b::real)
> to generate a rounded result to dividing integers, and noticed sometimes
> 0.5 was truncated and sometimes it was rounded up. Couldn't find
> anything about this in the archives or the data type documentation. Is
> there something obvious that I'm I missing?
>
> Thanks,
>
> --
>
> Lee Hachadoorian
> PhD Student, Geography
> Program in Earth & Environmental Sciences
> CUNY Graduate Center

See here for explanation:
http://archives.postgresql.org/pgsql-general/2010-03/msg00969.php

-- 
Adrian Klaver
adrian.klaver@gmail.com



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Round integer division
  2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
  2010-06-25 23:25 ` Re: Round integer division Adrian Klaver <adrian.klaver@gmail.com>
@ 2010-06-25 23:32   ` Lee Hachadoorian <lee.hachadoorian@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Lee Hachadoorian @ 2010-06-25 23:32 UTC (permalink / raw)
  To: pgsql-sql

Thanks. That was a fairly recent post, too, but I couldn't come up with
the right keyword search to find it.

On 06/25/2010 07:25 PM, Adrian Klaver wrote:
> On Friday 25 June 2010 3:53:01 pm Lee Hachadoorian wrote:
>   
>> Is it documented anywhere that floating-point numbers round
>> "scientifically", that is 0.5 rounds to the nearest even number? 
>>     
> See here for explanation:
> http://archives.postgresql.org/pgsql-general/2010-03/msg00969.php
>
>   

-- 
Lee Hachadoorian
PhD Student, Geography
Program in Earth & Environmental Sciences
CUNY Graduate Center




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Round integer division
  2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
@ 2010-06-26 13:09 ` Jasen Betts <jasen@xnet.co.nz>
  2 siblings, 0 replies; 7+ messages in thread

From: Jasen Betts @ 2010-06-26 13:09 UTC (permalink / raw)
  To: pgsql-sql

On 2010-06-25, Lee Hachadoorian <lee.hachadoorian@gmail.com> wrote:
> Is it documented anywhere that floating-point numbers round
> "scientifically", that is 0.5 rounds to the nearest even number? 

That's swiss rounding.  And no, as I understand it documented that 
most arithmetic) is platform specific.

Postgres is written in C and the relevant portions of the 
C standards douments (and discussions thereof) give a good 
picture of the functioning of postgres arithmetic. 





^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2010-06-26 13:18 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-06-25 22:53 Round integer division Lee Hachadoorian <lee.hachadoorian@gmail.com>
2010-06-25 23:00 ` Scott Marlowe <scott.marlowe@gmail.com>
2010-06-25 23:07   ` Lee Hachadoorian <lee.hachadoorian@gmail.com>
2010-06-26 13:18     ` Jasen Betts <jasen@xnet.co.nz>
2010-06-25 23:25 ` Adrian Klaver <adrian.klaver@gmail.com>
2010-06-25 23:32   ` Lee Hachadoorian <lee.hachadoorian@gmail.com>
2010-06-26 13:09 ` Jasen Betts <jasen@xnet.co.nz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox