Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 82B47B5DC31 for ; Tue, 24 May 2011 13:24:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 20183-09 for ; Tue, 24 May 2011 16:24:36 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 6E2F1B5DBE0 for ; Tue, 24 May 2011 13:24:36 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p4OGOZcj007676; Tue, 24 May 2011 12:24:35 -0400 (EDT) To: "Kevin Grittner" cc: pgsql-bugs@postgresql.org, "jose soares" Subject: Re: BUG #6036: why a REAL should be queried as a string? In-reply-to: <4DDB8CD3020000250003DBF2@gw.wicourts.gov> References: <201105240714.p4O7EXQW059510@wwwmaster.postgresql.org> <4DDB8CD3020000250003DBF2@gw.wicourts.gov> Comments: In-reply-to "Kevin Grittner" message dated "Tue, 24 May 2011 10:47:47 -0500" Date: Tue, 24 May 2011 12:24:35 -0400 Message-ID: <7675.1306254275@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/175 X-Sequence-Number: 30314 "Kevin Grittner" writes: > You're running into multiple issues here. First off, you should > understand that "real" is an approximate data type, which is not > capable of exactly representing many decimal fractions. "numeric" > can exactly represent decimal fractions. So, while it's OK when you > compare real to numeric on values for which real happens to be > exact, it falls apart when its approximation doesn't exactly match > the numeric value: Actually, the comparison constant is getting cast to float8, as can be seen with "explain": regression=# explain select * from frazione where importo='0.833'; QUERY PLAN ---------------------------------------------------------- Seq Scan on frazione (cost=0.00..36.75 rows=11 width=8) Filter: (importo = 0.833::real) (2 rows) regression=# explain select * from frazione where importo=0.833; QUERY PLAN ---------------------------------------------------------- Seq Scan on frazione (cost=0.00..36.75 rows=11 width=8) Filter: (importo = 0.833::double precision) (2 rows) But the basic point is correct: 0.833::float8 is a value that has no exact match in float4 arithmetic. regression=# select 0.833::float8 - 0.833::float4; ?column? ----------------------- -4.29153446113162e-09 (1 row) In general, doing exact comparisons on float values is going to bite you sooner or later, unless you know exactly what you're doing. regards, tom lane