From: | Martin Norbäck Olivers <martin(at)norpan(dot)org> |
---|---|
To: | szy <598546998(at)qq(dot)com> |
Cc: | Erik Brandsberg <erik(at)heimdalldata(dot)com>, pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org> |
Subject: | Re: Inconsistent results for division and multiplication operations |
Date: | 2024-11-26 08:13:22 |
Message-ID: | CALoTC6uBuw0KAC_Ys5YMRfCazXh3gdxeY9OVkpvmnexsE__FAg@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On Mon, Nov 25, 2024 at 5:18 PM szy <598546998(at)qq(dot)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
E-post: martin(at)norpan(dot)org
Kärrhöksvägen 4
656 72 Skattkärr
From | Date | Subject | |
---|---|---|---|
Next Message | Suat Mollasalihoglu | 2024-12-03 11:54:42 | Request for PostgreSQL Resources and Documentation |
Previous Message | Tom Lane | 2024-11-25 16:34:38 | Re: Inconsistent results for division and multiplication operations |