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

From szy
Subject 回复: Inconsistent results for division and multiplication operations
Date
Msg-id tencent_92088BAA6FD9CC940204E558F80B8AE77C0A@qq.com
Whole thread Raw
In response to Re: Inconsistent results for division and multiplication operations  (Erik Brandsberg <erik@heimdalldata.com>)
Responses Re: Inconsistent results for division and multiplication operations
List 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);
         ?column?
--------------------------
 5.01  
(1 row)

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

This is a common issue with using floating point math.  You will see the same issue with many systems.  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 digits as the input, it would be identical.  https://learn.microsoft.com/en-us/office/troubleshoot/access/floating-calculations-info


On Mon, Nov 25, 2024 at 10:46 AM szy <598546998@qq.com> 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;
         ?column?
--------------------------
 5.0149999999999999999806  
(1 row)

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

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



 

pgsql-sql by date:

Previous
From: Erik Brandsberg
Date:
Subject: Re: Inconsistent results for division and multiplication operations
Next
From: Philip Semanchuk
Date:
Subject: Re: Inconsistent results for division and multiplication operations