Re: Inconsistent results for division and multiplication operations

Erik Brandsberg <erik@heimdalldata.com>

From: Erik Brandsberg <erik@heimdalldata.com>
To: szy <598546998@qq.com>
Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>
Date: 2024-11-25T15:53:38Z
Lists: pgsql-sql
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
>
>
> ------------------------------
> szy
> 598546998@qq.com
>
> <https://wx.mail.qq.com/home/index?t=readmail_businesscard_midpage&nocheck=true&name=szy&icon=http%3A%2F%2Fthirdqq.qlogo.cn%2Fg%3Fb%3Doidb%26k%3DiaAmEopniaNALwVq2rar6n3Q%26kti%3DZLTe1AAAAAE%26s%3D640%26t%3D1622504424&mail=598546998%40qq.com&code=Gezi7Gl9e-YYAktdETJ0hu2-JIM5l3FTObsX105iXnV2Az4OVWt5ENk8nrBlqhIKcNOvXiaTQhfswEOxGaCYhQ>
>
>