回复: Inconsistent results for division and multiplication operations

From: szy <598546998(at)qq(dot)com>
To: Erik Brandsberg <erik(at)heimdalldata(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: 回复: Inconsistent results for division and multiplication operations
Date: 2024-11-25 16:12:15
Message-ID: tencent_92088BAA6FD9CC940204E558F80B8AE77C0A@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?column?
--------------------------
&nbsp;5.01&nbsp;&nbsp;
(1 row)

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

szy
598546998(at)qq(dot)com

&nbsp;

This is a common issue with using floating point math.&nbsp; You will see the same issue with many systems.&nbsp; Basically, the order of operations can trigger very minor differences in results, but if you round the first result to the same number of significant&nbsp;digits as the input, it would be identical.&nbsp;&nbsp;https://learn.microsoft.com/en-us/office/troubleshoot/access/floating-calculations-info

On Mon, Nov 25, 2024 at 10:46 AM szy <598546998(at)qq(dot)com&gt; wrote:

Hi PostgreSQL community,

I have observed inconsistent results when performing division and multiplication operations in PostgreSQL.

postgres=# select 1.003/1.002*5.01;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?column?
--------------------------
&nbsp;5.0149999999999999999806&nbsp;&nbsp;
(1 row)

postgres=# select 1.003*5.01/1.002;
&nbsp; &nbsp; &nbsp; ?column?
--------------------
&nbsp;5.0150000000000000
(1 row)

However, the expected result should be consistent for both queries. The actual results differ

szy
598546998(at)qq(dot)com

&nbsp;

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Philip Semanchuk 2024-11-25 16:23:36 Re: Inconsistent results for division and multiplication operations
Previous Message Erik Brandsberg 2024-11-25 15:53:38 Re: Inconsistent results for division and multiplication operations