Обсуждение: BUG #5150: math bug

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

BUG #5150: math bug

От
"Gray"
Дата:
The following bug has been logged online:

Bug reference:      5150
Logged by:          Gray
Email address:      gray@ms-irk.ru
PostgreSQL version: 8.2.6
Operating system:   i686-pc-linux-gnu
Description:        math bug
Details:

select 1/3*3,(1.0/3.0)*3.0,floor((1.0/3.0)*3.0);

returns
0, 1, 0

Re: BUG #5150: math bug

От
Robert Haas
Дата:
On Fri, Oct 30, 2009 at 1:39 AM, Gray <gray@ms-irk.ru> wrote:
>
> The following bug has been logged online:
>
> Bug reference: =A0 =A0 =A05150
> Logged by: =A0 =A0 =A0 =A0 =A0Gray
> Email address: =A0 =A0 =A0gray@ms-irk.ru
> PostgreSQL version: 8.2.6
> Operating system: =A0 i686-pc-linux-gnu
> Description: =A0 =A0 =A0 =A0math bug
> Details:
>
> select 1/3*3,(1.0/3.0)*3.0,floor((1.0/3.0)*3.0);
>
> returns
> 0, 1, 0

Well, the first answer is correct, because 1/3 is a request for
integer division, so you get 0, and 0 * 3 is still zero.

I don't believe the second answer is really what you got, because
surely if you requested floating-point division the answer would be a
floating point number, not just 1.  On pg 8.3.8, I get
0.999999999999999999990, which explains why the third answer comes out
to zero.

In general, floating point arithmetic is inaccurate and sucky.  That
has nothing to do with PostgreSQL; it's just life.

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

...Robert

Re: BUG #5150: math bug

От
Tom Lane
Дата:
Robert Haas <robertmhaas@gmail.com> writes:
> In general, floating point arithmetic is inaccurate and sucky.  That
> has nothing to do with PostgreSQL; it's just life.

Actually, I think these examples are being done in "numeric" not float8.
Your comment stands though --- 1.0/3.0 does not give the exact rational
number 1/3, but some finite decimal approximation to it, which when
multiplied by 3 will not produce exactly 1.0.

There is special-purpose software out there that can compute exactly
with rational numbers, but you aren't likely to find it embedded in any
general-purpose tools like databases --- the use-case just isn't wide
enough.  One reason why not is that it'll still fall down on irrational
numbers.

            regards, tom lane

Re: BUG #5150: math bug

От
John R Pierce
Дата:
Tom Lane wrote:
> There is special-purpose software out there that can compute exactly
> with rational numbers, but you aren't likely to find it embedded in any
> general-purpose tools like databases --- the use-case just isn't wide
> enough.  One reason why not is that it'll still fall down on irrational
> numbers.
>


<nit>

1/3 is a rational number.  however,  it is a repeating fraction when
expressed in decimal.

</nit>

Re: BUG #5150: math bug

От
David Fetter
Дата:
On Fri, Oct 30, 2009 at 08:51:57PM -0700, John R Pierce wrote:
> Tom Lane wrote:
>> There is special-purpose software out there that can compute
>> exactly with rational numbers, but you aren't likely to find it
>> embedded in any general-purpose tools like databases --- the
>> use-case just isn't wide enough.  One reason why not is that it'll
>> still fall down on irrational numbers.
>>
>
>
> <nit>
>
> 1/3 is a rational number.  however,  it is a repeating fraction when
> expressed in decimal.
>
> </nit>

<nit level="2">
The set of algebraic numbers, of which rational numbers are a proper
subset, is countable and hence has Lebesgue measure zero on the real
line.
</nit> ;)

Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

Re: BUG #5150: math bug

От
Robert Haas
Дата:
On Fri, Oct 30, 2009 at 11:51 PM, John R Pierce <pierce@hogranch.com> wrote:
> Tom Lane wrote:
>>
>> There is special-purpose software out there that can compute exactly
>> with rational numbers, but you aren't likely to find it embedded in any
>> general-purpose tools like databases --- the use-case just isn't wide
>> enough. =A0One reason why not is that it'll still fall down on irrational
>> numbers.
>>
>
>
> <nit>
>
> 1/3 is a rational number. =A0however, =A0it is a repeating fraction when
> expressed in decimal.
>
> </nit>

That's true.  Nobody said otherwise.

...Robert

Re: BUG #5150: math bug

От
Mark Kirkwood
Дата:
David Fetter wrote:
> On Fri, Oct 30, 2009 at 08:51:57PM -0700, John R Pierce wrote:
>
>> Tom Lane wrote:
>>
>>> There is special-purpose software out there that can compute
>>> exactly with rational numbers, but you aren't likely to find it
>>> embedded in any general-purpose tools like databases --- the
>>> use-case just isn't wide enough.  One reason why not is that it'll
>>> still fall down on irrational numbers.
>>>
>>>
>> <nit>
>>
>> 1/3 is a rational number.  however,  it is a repeating fraction when
>> expressed in decimal.
>>
>> </nit>
>>
>
> <nit level="2">
> The set of algebraic numbers, of which rational numbers are a proper
> subset, is countable and hence has Lebesgue measure zero on the real
> line.
> </nit> ;)
>
>

LOL - fortunately (going by the bug) he is not trying to compute a
measure (i.e integrate) from a set of 'em.

Mark