From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andreas Joseph Krogh <andreas@visena.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: Thangavel, Parameswaran <Parameswaran.Thangavel@rsa.com>
Cc: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: PostgresSQL 10 | Driver 42.2.5 | Float Conversion Issue
Date: Tue, 20 Oct 2020 13:17:54 -0400
Message-ID: <652597.1603214274@sss.pgh.pa.us> (raw)
In-Reply-To: <VisenaEmail.90.b09bf69bfcec8e52.17546ec4f83@tc7-visena>
References: <CH2PR19MB3798B6096BD4EA60C2068367831F0@CH2PR19MB3798.namprd19.prod.outlook.com>
<VisenaEmail.9.b226ab802321bcdc.17544a6d3e3@tc7-visena>
<CH2PR19MB3798DAEA77750DD4F4CE67A0831F0@CH2PR19MB3798.namprd19.prod.outlook.com>
<VisenaEmail.e.3213a432433c7179.17544c1d629@tc7-visena>
<CAKFQuwbbYTc63HeEBPXcDzPU28Do4z+y9ZEwQKVFwCnbPP1FaQ@mail.gmail.com>
<VisenaEmail.90.b09bf69bfcec8e52.17546ec4f83@tc7-visena>
Andreas Joseph Krogh <andreas@visena.com> writes:
> How can this be sane?
If you're expecting exact results from float calculations, you need
a refresher course in what floating-point arithmetic is all about.
Standard IEEE float4 representation can handle somewhere between
six and seven decimal digits of accuracy. It's common to round
off the display to six digits to make sure that you don't see
any bogus digits. float4_numeric() does that:
snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
which explains the results David gets from testing direct
float4-to-numeric coercions. However, that's not the same
code path used for textual display of float4. float4out()
can be coerced into showing possibly-imprecise digits:
regression=# select '1234567'::float4;
float4
-------------
1.23457e+06
(1 row)
regression=# show extra_float_digits;
extra_float_digits
--------------------
0
(1 row)
regression=# set extra_float_digits = 1;
SET
regression=# select '1234567'::float4;
float4
---------
1234567
(1 row)
(Also note that v12 and later behave differently still, since they
use a new algorithm for converting floats to decimal.)
I'm not sure though why a JDBC driver change, without a server change,
would affect anything here. Maybe the driver is/was changing
extra_float_digits behind your back. Or maybe it's now asking for the
float value to be transmitted in binary, and then the decimal conversion
is being done on the Java side using who-knows-what rules.
Bottom line is that if you are using float anywhere along the line
in a calculation you expect exact results from, You're Doing It
Wrong. It's not really that exciting to trace down exactly where
the roundoff is happening, because the end answer is still going to
be that you need to not do that. Use numeric, or integer if you
know you're dealing with not-too-large integers.
regards, tom lane
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-jdbc@postgresql.org
Cc: tgl@sss.pgh.pa.us, andreas@visena.com, david.g.johnston@gmail.com, Parameswaran.Thangavel@rsa.com
Subject: Re: PostgresSQL 10 | Driver 42.2.5 | Float Conversion Issue
In-Reply-To: <652597.1603214274@sss.pgh.pa.us>
References: <CH2PR19MB3798B6096BD4EA60C2068367831F0@CH2PR19MB3798.namprd19.prod.outlook.com> <VisenaEmail.9.b226ab802321bcdc.17544a6d3e3@tc7-visena> <CH2PR19MB3798DAEA77750DD4F4CE67A0831F0@CH2PR19MB3798.namprd19.prod.outlook.com> <VisenaEmail.e.3213a432433c7179.17544c1d629@tc7-visena> <CAKFQuwbbYTc63HeEBPXcDzPU28Do4z+y9ZEwQKVFwCnbPP1FaQ@mail.gmail.com> <VisenaEmail.90.b09bf69bfcec8e52.17546ec4f83@tc7-visena> <652597.1603214274@sss.pgh.pa.us>
* 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