public inbox for [email protected]  
help / color / mirror / Atom feed
From: =?gb18030?B?c3p5?= <[email protected]>
To: =?gb18030?B?RXJpayBCcmFuZHNiZXJn?= <[email protected]>
Cc: =?gb18030?B?cGdzcWwtc3Fs?= <[email protected]>
Subject: =?gb18030?B?u9i4tKO6IEluY29uc2lzdGVudCByZXN1bHRzIGZv?= =?gb18030?B?ciBkaXZpc2lvbiBhbmQgbXVsdGlwbGljYXRpb24g?= =?gb18030?B?b3BlcmF0aW9ucw==?=
Date: Tue, 26 Nov 2024 00:12:15 +0800
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAFcck8EQ3h15-P=Ndt269gE50FDMc=BmWM0iVfUuLbev5HJUbQ@mail.gmail.com>
References: <[email protected]>
	<CAFcck8EQ3h15-P=Ndt269gE50FDMc=BmWM0iVfUuLbev5HJUbQ@mail.gmail.com>

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
[email protected]



&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 <[email protected]&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
[email protected]



&nbsp;

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: =?gb18030?B?u9i4tKO6IEluY29uc2lzdGVudCByZXN1bHRzIGZv?= =?gb18030?B?ciBkaXZpc2lvbiBhbmQgbXVsdGlwbGljYXRpb24g?= =?gb18030?B?b3BlcmF0aW9ucw==?=
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox