From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: How to do faster DML
Date: Sat, 17 Feb 2024 21:52:33 +0100
Message-ID: <20240217205233.nvzdigbxgfq6dyfh@hjp.at> (raw)
In-Reply-To: <20240216003401.dzjc5a5vn2au7yln@hjp.at>
References: <20240211222230.tt3mpjdvgrimeodu@hjp.at>
<CAB+=1TVjov+y-8=d3ey-B7-Wj-23KPAKQQAejkjXcS50_KZ=pg@mail.gmail.com>
<CAKAnmmLcTS7zpeZ3m7mkJxGsu+f+Tv9bp5KV3T52Q+c8DMTm+A@mail.gmail.com>
<CAB+=1TXv=RNOttpmzJOwbczSsaS0VnigFMLkPVY051fiVAxPdg@mail.gmail.com>
<20240213150234.ydiitfoowojhj2n3@hjp.at>
<CAB+=1TVCHMPjOS-uoFj36zBFPTzybY+VhrRC8WTa0pgg-0_xhA@mail.gmail.com>
<CAKFQuwZ4qqf-B9b8uQ5Xrxge9Rn0eeAdDPsn-WCwRBGBqNp01A@mail.gmail.com>
<20240215233130.mutkz2rcjehhtixg@hjp.at>
<CAKFQuwYGomKiqUcTwg==vDKY1-3K++jLZhYWcU0zkTaF91CkcQ@mail.gmail.com>
<20240216003401.dzjc5a5vn2au7yln@hjp.at>
On 2024-02-16 01:34:01 +0100, Peter J. Holzer wrote:
> On 2024-02-15 16:51:56 -0700, David G. Johnston wrote:
> > What you see with an exact type is what you get, which allows for implementing
> > equality, unlike inexact which requires epsilon checking.
>
> You can check binary fp values for equality. If they are equal, they
> will compare equal. If they aren't, they won't.
>
> What you can't expect is that the laws of commutativity, associativity,
> etc. hold. If you compute a value in two different ways which should be
> equivalent mathematically (e.g. a*b/c vs. a/c*b), the result may be
> different and an equality test may fail. But that is also the case for
> numeric (and of course integer).
To illustrate that point:
hjp=> create table t_n (a numeric, b numeric, c numeric);
CREATE TABLE
hjp=> insert into t_n values(47, 52, 10);
INSERT 0 1
-- the numbers are not specially chosen. I just invoked
-- select (random()*100)::int;
-- three times, and they were the ones that came up.
hjp=> select *, a / b * c, a * c / b, a / b * c = a * c / b from t_n;
╔════╤════╤════╤════════════════════════╤════════════════════╤══════════╗
║ a │ b │ c │ ?column? │ ?column? │ ?column? ║
╟────┼────┼────┼────────────────────────┼────────────────────┼──────────╢
║ 47 │ 52 │ 10 │ 9.03846153846153846150 │ 9.0384615384615385 │ f ║
╚════╧════╧════╧════════════════════════╧════════════════════╧══════════╝
(1 row)
So with type numeric two expressions which should be equal
mathematically, aren't in fact equal.
Now let's try the same thing with binary floating point:
hjp=> create table t_f (a float8, b float8, c float8);
CREATE TABLE
hjp=> insert into t_f values(47, 52, 10);
INSERT 0 1
hjp=> select *, a / b * c, a * c / b, a / b * c = a * c / b from t_f;
╔════╤════╤════╤═══════════════════╤═══════════════════╤══════════╗
║ a │ b │ c │ ?column? │ ?column? │ ?column? ║
╟────┼────┼────┼───────────────────┼───────────────────┼──────────╢
║ 47 │ 52 │ 10 │ 9.038461538461538 │ 9.038461538461538 │ t ║
╚════╧════╧════╧═══════════════════╧═══════════════════╧══════════╝
(1 row)
Now they are indeed equal. This is *not* guaranteed and I got a bit
lucky here, but the fact that I got lucky on the first try shows that
"float bad, numeric good" is not backed up by reality.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Attachments:
signature.asc (application/pgp-signature, 832B) download
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: hjp-pgsql@hjp.at
Subject: Re: How to do faster DML
In-Reply-To: <20240217205233.nvzdigbxgfq6dyfh@hjp.at>
References: <20240211222230.tt3mpjdvgrimeodu@hjp.at> <CAB+=1TVjov+y-8=d3ey-B7-Wj-23KPAKQQAejkjXcS50_KZ=pg@mail.gmail.com> <CAKAnmmLcTS7zpeZ3m7mkJxGsu+f+Tv9bp5KV3T52Q+c8DMTm+A@mail.gmail.com> <CAB+=1TXv=RNOttpmzJOwbczSsaS0VnigFMLkPVY051fiVAxPdg@mail.gmail.com> <20240213150234.ydiitfoowojhj2n3@hjp.at> <CAB+=1TVCHMPjOS-uoFj36zBFPTzybY+VhrRC8WTa0pgg-0_xhA@mail.gmail.com> <CAKFQuwZ4qqf-B9b8uQ5Xrxge9Rn0eeAdDPsn-WCwRBGBqNp01A@mail.gmail.com> <20240215233130.mutkz2rcjehhtixg@hjp.at> <CAKFQuwYGomKiqUcTwg==vDKY1-3K++jLZhYWcU0zkTaF91CkcQ@mail.gmail.com> <20240216003401.dzjc5a5vn2au7yln@hjp.at> <20240217205233.nvzdigbxgfq6dyfh@hjp.at>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox