Thread

  1. BUG #5150: math bug

    Gray <gray@ms-irk.ru> — 2009-10-30T05:39:10Z

    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
    
    
  2. Re: BUG #5150: math bug

    Robert Haas <robertmhaas@gmail.com> — 2009-10-31T03:10:02Z

    On Fri, Oct 30, 2009 at 1:39 AM, Gray <gray@ms-irk.ru> wrote:
    >
    > 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
    
    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
    
    
  3. Re: BUG #5150: math bug

    Tom Lane <tgl@sss.pgh.pa.us> — 2009-10-31T03:29:29Z

    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
    
    
  4. Re: BUG #5150: math bug

    John R Pierce <pierce@hogranch.com> — 2009-10-31T03:51:57Z

    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>
    
    
    
    
  5. Re: BUG #5150: math bug

    David Fetter <david@fetter.org> — 2009-10-31T05:45:21Z

    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
    
    
  6. Re: BUG #5150: math bug

    Robert Haas <robertmhaas@gmail.com> — 2009-10-31T13:10:56Z

    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.  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>
    
    That's true.  Nobody said otherwise.
    
    ...Robert
    
    
  7. Re: BUG #5150: math bug

    Mark Kirkwood <markir@paradise.net.nz> — 2009-11-01T07:12:27Z

    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