float8 comparison failure

Started by Radek Kanovskyover 25 years ago2 messagesbugs
Jump to latest

============================================================================
POSTGRESQL BUG REPORT TEMPLATE
============================================================================

Your name : Radek Kanovsky
Your email address : rk@dat.cz

System Configuration
---------------------
Architecture (example: Intel Pentium) : PC - AMD K6 3D

Operating System (example: Linux 2.0.26 ELF) : Linux 2.2.16 (Debian)

PostgreSQL version (example: PostgreSQL-6.5.3): PostgreSQL-6.5.3
(6.5.1, 6.5.2)

Compiler used (example: gcc 2.8.0) : gcc 2.95.2
libc6 2.1.3

Please enter a FULL description of your problem:
------------------------------------------------

I have one table defined as follows

CREATE TABLE misto (
idm VARCHAR(8) NOT NULL PRIMARY KEY,
nazev VARCHAR(64) NOT NULL,
okres VARCHAR(2) NOT NULL,
sourad_s FLOAT,
sourad_d FLOAT,
modifikace DATETIME NOT NULL DEFAULT now(),
modifikoval VARCHAR(32) NOT NULL DEFAULT getpgusername()
) ;
CREATE INDEX misto_okres ON misto (okres);
CREATE INDEX misto_nazev ON misto (nazev);

These three SELECTs don't return same result:

SELECT idm,sourad_s,sourad_d FROM misto WHERE idm = 'MHO00179';
idm | sourad_s| sourad_d
--------+-------------+-------------
MHO00179|48.9247114069|17.6064515081
(1 row)

SELECT idm,sourad_s,sourad_d FROM misto WHERE sourad_s = 48.9247114069;
idm | sourad_s| sourad_d
--------+-------------+-------------
MHO00179|48.9247114069|17.6064515081
(1 row)

SELECT idm,sourad_s,sourad_d FROM misto WHERE sourad_d = 17.6064515081;
idm|sourad_s|sourad_d
---+--------+--------
(0 rows)

Please describe a way to repeat the problem. Please try to provide a
concise reproducible example, if at all possible:
----------------------------------------------------------------------

The problem appears randomly. I havent found any relation
with anything else. I often use Python interface for INSERT,
UPDATE, SELECT.

If you know how this problem might be fixed, list the solution below:
---------------------------------------------------------------------

This helps:

BEGIN;
UPDATE misto SET sourad_d = 17.6064515081 WHERE idm = 'MHO00179';
END;

SELECT idm,sourad_s,sourad_d FROM misto WHERE sourad_d = 17.6064515081;
idm | sourad_s| sourad_d
--------+-------------+-------------
MHO00179|48.9247114069|17.6064515081

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Radek Kanovsky (#1)
Re: float8 comparison failure

Radek Kanovsky <rk@dat.cz> writes:

[ float8 values that appear equal are not always equal ]

This is not a bug, it's an inherent consequence of the inaccuracy of
floating-point math. Use some other data type (numeric, maybe) if
you expect to be able to do exact equality comparisons.

regards, tom lane