Thread: BUG #3791: Bug in aritmethic calculations

BUG #3791: Bug in aritmethic calculations

From
"Michailas"
Date:
The following bug has been logged online:

Bug reference:      3791
Logged by:          Michailas
Email address:      michailas.p@gmail.com
PostgreSQL version: 8.2.4-2
Operating system:   Debian
Description:        Bug in aritmethic calculations
Details:

postgres=# SELECT abs(300 - 200)/200*100 as bad, abs(300-200)*100/200 as
good;

 bad | good
-----+------
   0 |   50
(1 row)

Re: BUG #3791: Bug in aritmethic calculations

From
"Gevik Babakhani"
Date:
I think this is not a bug.
abs(300 - 200)/200 is calculated as integer.
try:

gevik=# SELECT abs(300 - 200)/200::float*100 as bad, abs(300-200)*100/200 as
good;
 bad | good
-----+------
  50 |   50
(1 row)

------------------------------------------------
Gevik Babakhani

PostgreSQL NL       http://www.postgresql.nl
TrueSoftware BV     http://www.truesoftware.nl
------------------------------------------------


> -----Original Message-----
> From: pgsql-bugs-owner@postgresql.org
> [mailto:pgsql-bugs-owner@postgresql.org] On Behalf Of Michailas
> Sent: Friday, November 30, 2007 10:18 AM
> To: pgsql-bugs@postgresql.org
> Subject: [BUGS] BUG #3791: Bug in aritmethic calculations
>
>
> The following bug has been logged online:
>
> Bug reference:      3791
> Logged by:          Michailas
> Email address:      michailas.p@gmail.com
> PostgreSQL version: 8.2.4-2
> Operating system:   Debian
> Description:        Bug in aritmethic calculations
> Details:
>
> postgres=# SELECT abs(300 - 200)/200*100 as bad,
> abs(300-200)*100/200 as good;
>
>  bad | good
> -----+------
>    0 |   50
> (1 row)
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

Re: BUG #3791: Bug in aritmethic calculations

From
Gregory Stark
Date:
"Michailas" <michailas.p@gmail.com> writes:

> postgres=# SELECT abs(300 - 200)/200*100 as bad, abs(300-200)*100/200 as
> good;
>
>  bad | good
> -----+------
>    0 |   50
> (1 row)

That's how integer arithmetic works. Try just 1/2 and you'll see you get 0.
But if you do floating point arithmetic with 1.0/2 you'll get 0.5.

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com
  Ask me about EnterpriseDB's RemoteDBA services!