Обсуждение: Round integer division

Поиск
Список
Период
Сортировка

Round integer division

От
Lee Hachadoorian
Дата:
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



Re: Round integer division

От
Scott Marlowe
Дата:
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?


Re: Round integer division

От
Lee Hachadoorian
Дата:

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



Re: Round integer division

От
Adrian Klaver
Дата:
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


Re: Round integer division

От
Lee Hachadoorian
Дата:
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



Re: Round integer division

От
Jasen Betts
Дата:
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. 




Re: Round integer division

От
Jasen Betts
Дата:
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.


Re: round(Numeric)

От
silly sad
Дата:
On 06/26/10 17:09,  wrote:
> 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.

IT IS REALLY SAD !
the round(Numeric) does not behave as Math science prescribes :(

Shame on PostgresQL !