pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: pgsql-bugs@postgresql.org
Cc: jose soares <jose.soares@sferacarta.com>
Subject: Re: BUG #6036: why a REAL should be queried as a string?
Date: Tue, 24 May 2011 12:24:35 -0400
Message-ID: <7675.1306254275@sss.pgh.pa.us> (raw)
In-Reply-To: <4DDB8CD3020000250003DBF2@gw.wicourts.gov>
References: <201105240714.p4O7EXQW059510@wwwmaster.postgresql.org>
	<4DDB8CD3020000250003DBF2@gw.wicourts.gov>

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> 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



view thread (3+ messages)

Message-ID: <7675.1306254275@sss.pgh.pa.us>
Permalink:  ../7675.1306254275@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/7675.1306254275@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, Kevin.Grittner@wicourts.gov, jose.soares@sferacarta.com
  Subject: Re: BUG #6036: why a REAL should be queried as a string?
  In-Reply-To: <7675.1306254275@sss.pgh.pa.us>
  References: <201105240714.p4O7EXQW059510@wwwmaster.postgresql.org> <4DDB8CD3020000250003DBF2@gw.wicourts.gov> <7675.1306254275@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox