Comparing two double values method
Dears,
I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.
I wrote this patch to fix this.
--
Regard,
Bowen Shi
Attachments:
v1-0001-Fix-double-value-compare-problem.patchapplication/octet-stream; name=v1-0001-Fix-double-value-compare-problem.patchDownload
From 775b2d34f0a2e95643b7c109074c807004b3d915 Mon Sep 17 00:00:00 2001
From: bovenshi <bovenshi@tencent.com>
Date: Tue, 10 Oct 2023 17:53:05 +0800
Subject: [PATCH] Fix double value compare problem.
Comparing the value of two double types cannot be done directly using the '==' or '!=' operators.
---
src/backend/utils/misc/guc.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 5308896..5fa17e9 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -1433,7 +1433,8 @@ check_GUC_init(struct config_generic *gconf)
{
struct config_real *conf = (struct config_real *) gconf;
- if (*conf->variable != 0.0 && *conf->variable != conf->boot_val)
+ if (fabs(*conf->variable - 0.0) > 1e-6 &&
+ fabs(*conf->variable - conf->boot_val) > 1e-6)
{
elog(LOG, "GUC (PGC_REAL) %s, boot_val=%g, C-var=%g",
conf->gen.name, conf->boot_val, *conf->variable);
--
2.9.3
On Tue, 10 Oct 2023 at 12:33, Bowen Shi <zxwsbg12138@gmail.com> wrote:
Dears,
I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.
I don't think I understand the problem. The code checks that the
dynamic initialization values are equal to the current value of the
GUC, or 0. Why would a "margin for error" of 1e-6 be of any use?
Why was the margin of 1e-6 chosen instead of one based on the exponent
of the GUC's current value (if any)?
In my view, this would break the code, not fix it, as it would
decrease the cases where we detect broken GUC registrations.
Kind regards,
Matthias van de Meent
Neon (https://neon.tech)
On 10/10/2023 13:31, Bowen Shi wrote:
Dears,
I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.I wrote this patch to fix this.
No, the compile-time initial values should match exactly.
--
Heikki Linnakangas
Neon (https://neon.tech)
Heikki Linnakangas <hlinnaka@iki.fi> writes:
On 10/10/2023 13:31, Bowen Shi wrote:
I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.
No, the compile-time initial values should match exactly.
Right. The point of this test is to catch cases where you wrote,
say,
double my_guc = 1.1;
but the boot_val for it in guc_tables.c is 1.2. There is no
reason to allow any divergence in the spellings of the two C
literals, so as long as they're compiled by the same compiler
there's no reason to expect that the compiled values wouldn't
be bit-equal.
The point of the exclusions for zero is to allow you to just
write
double my_guc;
without expressing an opinion about the initial value.
(Yes, this does mean that "double my_guc = 0.0;" could
be misleading. It's just a heuristic though.)
regards, tom lane