From: Oliver Jowett <oliver@opencloud.com>
To: pg@fastcrypt.com
Cc: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: patch for getXXX methods
Date: Mon, 12 Jul 2004 11:37:56 +1200
Message-ID: <40F1CF54.1040606@opencloud.com> (raw)
In-Reply-To: <1089577486.1508.316.camel@localhost.localdomain>
References: <1089407362.1508.292.camel@localhost.localdomain>
<40EF1D5D.2050509@opencloud.com>
<1089416849.1515.298.camel@localhost.localdomain>
<40F15718.7000302@opencloud.com>
<1089577486.1508.316.camel@localhost.localdomain>
Dave Cramer wrote:
> Oliver,
>
> I don't believe you will lose precision if the number is below MAX_LONG
> ? When I tested it on my system, I was able to retrieve a double that
> was equal to MAX_LONG without losing precision.
The attached testcase says otherwise. It produces this output:
> Mismatch: 9223372036854775806 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775805 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775804 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775803 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775802 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775801 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775800 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775799 => 9.223372036854776E18 => 9223372036854775807
[...]
> Mismatch: 9223372036854775296 => 9.223372036854776E18 => 9223372036854775807
> Mismatch: 9223372036854775295 => 9.2233720368547748E18 => 9223372036854774784
> Mismatch: 9223372036854775294 => 9.2233720368547748E18 => 9223372036854774784
and so on.
The problem is that near MAX_LONG you need almost 64 bits of mantissa to
exactly represent the value -- but a double is only a 64-bit value
including space for the exponent. I can't remember the exact split but
from the above it looks like there is around 10 bits of exponent so you
only have ~54 bits for the mantissa -- so you only get a precision of
about +/- 512 when you're dealing with numbers of a magnitude around
MAX_LONG.
-O
Attachments:TestDoublePrecision.java (text/x-java, 295B) download
public class TestDoublePrecision {
public static void main(String[] args) {
for (long l = Long.MAX_VALUE; l != Long.MIN_VALUE; --l) {
double d = (double)l;
long check = (long)d;
if (check != l)
System.out.println("Mismatch: " + l + " => " + d + " => " + check);
}
}
}
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: oliver@opencloud.com, pg@fastcrypt.com
Subject: Re: patch for getXXX methods
In-Reply-To: <40F1CF54.1040606@opencloud.com>
References: <1089407362.1508.292.camel@localhost.localdomain> <40EF1D5D.2050509@opencloud.com> <1089416849.1515.298.camel@localhost.localdomain> <40F15718.7000302@opencloud.com> <1089577486.1508.316.camel@localhost.localdomain> <40F1CF54.1040606@opencloud.com>
* 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