Re: [HACKERS] 123.45 - 123 = 0.45

From: David Hartwig <daveh(at)insightdist(dot)com>
To: Bruce Momjian <maillist(at)candle(dot)pha(dot)pa(dot)us>
Cc: sferac(at)bo(dot)nettuno(dot)it, pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] 123.45 - 123 = 0.45
Date: 1998-12-15 15:18:21
Message-ID: 36767DBD.129AAA1F@insightdist.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Try this:

#include <stdio.h>

main()
{
double f1 = 123.45;
double f2 = 123.00;
double r;

r = f1 - f2;
printf("%0.15f %0.15f %0.15f\n", f1, f2, r);
}

Internal representation of 123.45 is not exact. In the conversion to
binary, an irrational number is created which is truncated to 64 bits.

Bruce Momjian wrote:

> > Hi all,
> >
> > I tried to divide 123.45 by 123.00 but PostgreSQL gives me a wrong
> > result:
> >
> > hygea=> select 123.45 - 123.00;
> > ?column?
> > -----------------
> > 0.450000000000003
> > (1 row)
> >
>
> Wow, I get the same thing here.
>
> Even this doesn't work:
>
> test=> select float8(123.45) - float8(123.00);
> ?column?
> -----------------
> 0.450000000000003
> (1 row)
>
> Now constants are automatically promoted to float8, so I expected the
> same results, but what is going on here?
>
> --
> Bruce Momjian | http://www.op.net/~candle
> maillist(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
> + If your life is a hard drive, | 830 Blythe Avenue
> + Christ can be your backup. | Drexel Hill, Pennsylvania 19026

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Vince Vielhaber 1998-12-15 15:34:42 Re: [HACKERS] 123.45 - 123 = 0.45
Previous Message David Hartwig 1998-12-15 14:55:24 Re: [SQL] MINUS and slow 'not in'