Re: Floating point error

From: Kevin Grittner <kgrittn(at)ymail(dot)com>
To: Tom Duffey <tduffey(at)trillitech(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Floating point error
Date: 2013-02-26 07:48:09
Message-ID: 1361864889.69877.YahooMailNeo@web162905.mail.bf1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Tom Duffey <tduffey(at)trillitech(dot)com> wrote:

> CREATE TABLE test (
>     id INTEGER PRIMARY KEY,
>     value REAL NOT NULL
> );
>
> INSERT INTO test (id, value) VALUES (1, 10.3884573), (2, 10.3885);
> SELECT * FROM test;
>
> id |  value
> ----+---------
>   1 | 10.3885
>   2 | 10.3885
> (2 rows)
>
> At this point you would think you have two equal values.

At this point, try this:

select * from test where value = '10.3885';

 id |  value
----+---------
  2 | 10.3885
(1 row)

Now try this:

select * from test where value = 10.3885;
 id | value
----+-------
(0 rows)

Always remember that floating point types are *approximate* data
types; equality often does not behave as you might expect.  You're
probably aware of the below issues, but just in case:

select '99999999'::real = ('99999999'::real + '1'::real);
 ?column?
----------
 t
(1 row)

select '.1'::real::float;
      float8       
-------------------
 0.100000001490116
(1 row)

--
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ali Pouya 2013-02-26 10:14:53 Partitionning by trigger
Previous Message Adrian Klaver 2013-02-25 21:30:27 Re: Use, Set Catalog and JDBC questions

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2013-02-26 08:06:08 Re: [PATCH] Add PQconninfoParseParams and PQconninfodefaultsMerge to libpq
Previous Message Amit Kapila 2013-02-26 07:06:24 Re: [PATCH] Add PQconninfoParseParams and PQconninfodefaultsMerge to libpq