pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Precision when substracting two values with SQL
6+ messages / 5 participants
[nested] [flat]

* Precision when substracting two values with SQL
@ 2002-08-12 19:57  Juancho <squid@maxiconsumo.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Juancho @ 2002-08-12 19:57 UTC (permalink / raw)
  To: pgsql-admin

Hello:
	I have a problem substracting two values whith SQL.  Please
consider this cases...

ciudadela=# select 0.2-0.2;
 ?column? 
----------
        0
(1 row)

	That's OK for me,


ciudadela=# select 0.2-0.21;
       ?column?       
----------------------
 -0.00999999999999998
(1 row)

	I was hoping -0.01,


ciudadela=# select 0.21-0.22;
 ?column? 
----------
    -0.01
(1 row)

	Again, that's OK for me,


	My question is why is this behavior taking place (the second case),
 and how can I avoid it.
	Any suggestion would be really appreciated.
	Thanks,


Juan Manuel
Buenos Aires
Argentina





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Precision when substracting two values with SQL
@ 2002-08-12 20:24  Tim Ellis <Tim.Ellis@gamet.com>
  parent: Juancho <squid@maxiconsumo.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Tim Ellis @ 2002-08-12 20:24 UTC (permalink / raw)
  To: pgsql-admin

> ciudadela=# select 0.2-0.2;
>         0
> ciudadela=# select 0.2-0.21;
>  -0.00999999999999998
> ciudadela=# select 0.21-0.22;
>     -0.01

I also get this on my Postgres 7.2.1, both Linux and Solaris.

-- 
Tim Ellis
Senior Database Architect
Gamet, Inc.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Precision when substracting two values with SQL
@ 2002-08-12 20:27  Larry Rosenman <ler@lerctr.org>
  parent: Tim Ellis <Tim.Ellis@gamet.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Larry Rosenman @ 2002-08-12 20:27 UTC (permalink / raw)
  To: Tim Ellis <Tim.Ellis@gamet.com>; +Cc: pgsql-admin

On Mon, 2002-08-12 at 15:24, Tim Ellis wrote:
> > ciudadela=# select 0.2-0.2;
> >         0
> > ciudadela=# select 0.2-0.21;
> >  -0.00999999999999998
> > ciudadela=# select 0.21-0.22;
> >     -0.01
> 
> I also get this on my Postgres 7.2.1, both Linux and Solaris.
You are comparing floats.  

This is a STANDARD problem with Base 10 Floating point. 

LER

-- 
Larry Rosenman                     http://www.lerctr.org/~ler
Phone: +1 972-414-9812                 E-Mail: ler@lerctr.org
US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Precision when substracting two values with SQL
@ 2002-08-12 20:27  Andrew Sullivan <andrew@libertyrms.info>
  parent: Juancho <squid@maxiconsumo.com>
  1 sibling, 2 replies; 6+ messages in thread

From: Andrew Sullivan @ 2002-08-12 20:27 UTC (permalink / raw)
  To: pgsql-admin

On Mon, Aug 12, 2002 at 04:57:32PM -0300, Juancho wrote:
> 
> ciudadela=# select 0.2-0.21;
>        ?column?       
> ----------------------
>  -0.00999999999999998
> (1 row)
> 
> 	I was hoping -0.01,

Try 

select (0.2-0.21)::numeric(12,2);

A

-- 
----
Andrew Sullivan                               87 Mowat Avenue 
Liberty RMS                           Toronto, Ontario Canada
<andrew@libertyrms.info>                              M6K 3E3
                                         +1 416 646 3304 x110




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Precision when substracting two values with SQL
@ 2002-08-12 20:33  Juancho <squid@maxiconsumo.com>
  parent: Andrew Sullivan <andrew@libertyrms.info>
  1 sibling, 0 replies; 6+ messages in thread

From: Juancho @ 2002-08-12 20:33 UTC (permalink / raw)
  To: pgsql-admin

>
>
>Try 
>
>select (0.2-0.21)::numeric(12,2);
>
>A
>
>  
>

Thank you, I will do this...




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Precision when substracting two values with SQL
@ 2002-08-12 20:51  Jean-Luc Lachance <jllachan@nsd.ca>
  parent: Andrew Sullivan <andrew@libertyrms.info>
  1 sibling, 0 replies; 6+ messages in thread

From: Jean-Luc Lachance @ 2002-08-12 20:51 UTC (permalink / raw)
  To: Andrew Sullivan <andrew@libertyrms.info>; +Cc: pgsql-admin

I think you meant:

select 0.2:: numeric(12,2) - 0.21::numeric(12,2);

If you are not conviced try:

select (0.2-0.21)::numeric(40,20);


Andrew Sullivan wrote:
> 
> On Mon, Aug 12, 2002 at 04:57:32PM -0300, Juancho wrote:
> >
> > ciudadela=# select 0.2-0.21;
> >        ?column?
> > ----------------------
> >  -0.00999999999999998
> > (1 row)
> >
> >       I was hoping -0.01,
> 
> Try
> 
> select (0.2-0.21)::numeric(12,2);
> 
> A
> 
> --
> ----
> Andrew Sullivan                               87 Mowat Avenue
> Liberty RMS                           Toronto, Ontario Canada
> <andrew@libertyrms.info>                              M6K 3E3
>                                          +1 416 646 3304 x110
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2002-08-12 20:51 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-08-12 19:57 Precision when substracting two values with SQL Juancho <squid@maxiconsumo.com>
2002-08-12 20:24 ` Tim Ellis <Tim.Ellis@gamet.com>
2002-08-12 20:27   ` Larry Rosenman <ler@lerctr.org>
2002-08-12 20:27 ` Andrew Sullivan <andrew@libertyrms.info>
2002-08-12 20:33   ` Juancho <squid@maxiconsumo.com>
2002-08-12 20:51   ` Jean-Luc Lachance <jllachan@nsd.ca>

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