Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nKbxV-0002bf-L0 for pgsql-general@arkaria.postgresql.org; Thu, 17 Feb 2022 08:16:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nKbxU-0006Lm-HW for pgsql-general@arkaria.postgresql.org; Thu, 17 Feb 2022 08:16:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nKbxU-0006Ld-75 for pgsql-general@lists.postgresql.org; Thu, 17 Feb 2022 08:16:12 +0000 Received: from smtprelay04.ispgateway.de ([80.67.31.42]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nKbxQ-0007cw-57 for pgsql-general@lists.postgresql.org; Thu, 17 Feb 2022 08:16:11 +0000 Received: from [109.90.124.214] (helo=mail.office.datagis.com) by smtprelay04.ispgateway.de with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nKbxL-0006PP-Qh; Thu, 17 Feb 2022 09:16:03 +0100 Received: from localhost (intranator.datagis.local [127.0.0.1]) by localhost (Postfix) with ESMTP id 8EB7D332; Thu, 17 Feb 2022 09:16:03 +0100 (CET) X-Virus-Scanned: by Intra2net Mail Security (AVE=8.3.64.92,VDF=8.19.7.102) X-Spam-Status: X-Spam-Level: 0 Received: from [192.168.12.10] (dslb-002-205-128-037.002.205.pools.vodafone-ip.de [2.205.128.37]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: smtp-auth-user) by mail.office.datagis.com (Postfix) with ESMTPSA id 0835D123; Thu, 17 Feb 2022 09:16:01 +0100 (CET) Message-ID: <4b255a51-d9cd-8957-0d56-3b022ec0f3ad@datagis.com> Date: Thu, 17 Feb 2022 09:16:02 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 Subject: Re: Strange results when casting string to double Content-Language: en-US To: Gavan Schneider , "pgsql-generallists.postgresql.org" Cc: Adrian Klaver , "David G. Johnston" References: <0d711ac6-6975-ae07-8963-aa91c6500776@datagis.com> <8d9290c7-684d-c445-830b-b738ce558d19@datagis.com> <521796F1-4F27-49BB-BB27-3F23B678382F@pendari.org> From: Carsten Klein Organization: DataGis In-Reply-To: <521796F1-4F27-49BB-BB27-3F23B678382F@pendari.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Df-Sender: NTkwMDE1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Feb 17, 2022 at 00:07 Gavan Schneider wrote: > Harking back to my long distant formative years I was taught to never ever rely on equality tests when it came to floating point values. Nothing has changed in this regard. > > If binary exact is part of the requirement then the answer is INTEGER or NUMERIC never FLOAT, REAL, DOUBLE or anything similar. > > So, assuming the trigger function is the only source of this spurious grief one could apply the lesson taught in the 60s from the dawn of computing: > EQUALITY = absolute_value(op1 - op2) < epsilon — where op1 & op2 are DOUBLE, and epsilon is smaller than you care for > > Given the intrinsic (standards compliant) uncertainty when converting from absolute (e.g., string representation) to floating point there will never be value resolving why there are differences. > > I suggest using the comparison that is appropriate to the representation of those values or fix the design by using the proper representation. Just some(!) background: I know that there are other ways to compare _any_ floating point values. However, doing that for a whole ROW in a fast manner is not trivial (e. g. it tends to get quite slow). With the hstore extension and (so called) binary equality I've found a very fast way which is also fully generic. hstore uses text representation, so comparisons depend on how values are converted to and from text. But all that is not the point. Double precision conversion algorithms are well defined and deterministic, so it should yield the same results when called with the same arguments (every time and on every machine). That is 1.56::double precision == 1.56000000000000005329070518201E0 1.56::double precision != 1.55999999999999983124610025698E0 With reduced precision (as in PG), that gives you: 1.56::double precision == 1.56 1.56::double precision != 1.55999999999999 However, one of my ProstgreSQL servers returns the latter (wrong) value. You can test this with C library function 'strtod' or with any of the online converters around: https://www.binaryconvert.com/result_double.html Click 'New Conversion' and enter 1.56 into the 'Decimal' field. Then 'Convert to binary' or hit enter. So, the primary problem of that PostgreSQL server is, that it converts text to double in a wrong way. Apart from any triggers, "binary equality" and whatever else I'm doing in this project, this has dramatic effects on the database, as it's messing up the values that I'm storing: Imagine I do: INSERT INTO foo (my_col) VALUES ('Hello World'); But the database has happily stored a different string: SELECT my_col FROM foo; my_col ------------- Hello Worlc (1 row) Finding that string again may be done with "fuzzy search" or regular expressions, but the much better approach is the database not to let mess up the string while storing it. Double precision values are limited in precision (in binary) and there are numbers, that cannot be stored exactly (as it's true for many decimal numbers, like 1/3 ~ 0.33333 as well). Nevertheless, with a given maximum of precision, the same values should have the same (well defined) binary value so that conversions between text and double should not change the value at any time on any machine. Carsten