Re: Floating point error

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Tom Duffey *EXTERN*" <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-25 14:00:03
Message-ID: A737B7A37273E048B164557ADEF4A58B057B8604@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Tom Duffey wrote:
> Here is a smaller test case that does not involve Java. I guess this probably is just due to floating
> point error when the initial value is inserted that is too large for the field but it's still a
> surprise.
>
> Create a test table, insert a couple values and view the results:
>
> 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)

SET extra_float_digits=3;
SELECT * FROM test;

id | value
----+------------
1 | 10.3884573
2 | 10.3885002
(2 rows)

PostgreSQL by default omits the last three digits to avoid
differences on different architectures (I think).

When you convert to double precision, you'll see these digits.

> At this point you would think you have two equal values. Now change the type:
>
> ALTER TABLE test ALTER COLUMN value TYPE DOUBLE PRECISION;
> SELECT * FROM test;
>
> id | value
> ----+------------------
> 1 | 10.3884572982788
> 2 | 10.388500213623
> (2 rows)

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Duffey 2013-02-25 14:08:57 Re: Floating point error
Previous Message Tom Duffey 2013-02-25 13:01:30 Re: Floating point error

Browse pgsql-hackers by date

  From Date Subject
Next Message Stephen Frost 2013-02-25 14:05:24 Re: Strange Windows problem, lock_timeout test request
Previous Message Tom Duffey 2013-02-25 13:01:30 Re: Floating point error