Re: Inconsistent results for division and multiplication operations
Martin Norbäck Olivers <martin@norpan.org>
From: Martin Norbäck Olivers <martin@norpan.org>
To: szy <598546998@qq.com>
Cc: Erik Brandsberg <erik@heimdalldata.com>,
pgsql-sql <pgsql-sql@lists.postgresql.org>
Date: 2024-11-26T08:13:22Z
Lists: 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 E-post: martin@norpan.org Kärrhöksvägen 4 656 72 Skattkärr