Re: Inconsistent results for division and multiplication operations - Mailing list pgsql-sql

From Martin Norbäck Olivers
Subject Re: Inconsistent results for division and multiplication operations
Date
Msg-id CALoTC6uBuw0KAC_Ys5YMRfCazXh3gdxeY9OVkpvmnexsE__FAg@mail.gmail.com
Whole thread Raw
In response to 回复: Inconsistent results for division and multiplication operations  ("szy" <598546998@qq.com>)
List pgsql-sql


On Mon, Nov 25, 2024 at 5:18 PM szy <598546998@qq.com> wrote:
If the number of significant digits in the input is not fixed, it becomes challenging to achieve consistent results by rounding.
for example
postgres=# select round(1.003/1.002*5.01,2);
         ?column?
--------------------------
 5.01  
(1 row)

postgres=# select round(1.003*5.01/1.002,2);
      ?column?
--------------------
 5.02
(1 row)


Correct. That's why you should always use numeric with the desired precision if you want precision numbers.

for instance
select 1.003/1.002*5.01 :: numeric(10,4)
will give the same result as
select 1.003*5.01/1.002 :: numeric(10,4)

They are much slower to calculate than floating point, however, so if you don't care about precision you can keep using just floating point.

Regards,

Martin 

--
Martin Norbäck Olivers
IT-konsult, Masara AB
Telefon: +46 703 22 70 12
Kärrhöksvägen 4
656 72 Skattkärr

pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Inconsistent results for division and multiplication operations
Next
From: Suat Mollasalihoglu
Date:
Subject: Request for PostgreSQL Resources and Documentation