X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1645CD1B28B for ; Sun, 11 Jul 2004 20:38:39 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 12431-08 for ; Sun, 11 Jul 2004 23:38:37 +0000 (GMT) Received: from yass.opencloud.co.nz (yass.opencloud.co.nz [203.79.85.162]) by svr1.postgresql.org (Postfix) with ESMTP id EFE69D1B266 for ; Sun, 11 Jul 2004 20:38:32 -0300 (ADT) Received: from 203-79-104-60.cable.paradise.net.nz ([203.79.104.60] helo=opencloud.com) by yass.opencloud.co.nz with asmtp (Exim 3.35 #1 (Debian)) id 1Bjnts-0002El-00; Mon, 12 Jul 2004 11:38:28 +1200 Message-ID: <40F1CF54.1040606@opencloud.com> Date: Mon, 12 Jul 2004 11:37:56 +1200 From: Oliver Jowett User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pg@fastcrypt.com Cc: "pgsql-jdbc@postgresql.org" Subject: Re: patch for getXXX methods 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> In-Reply-To: <1089577486.1508.316.camel@localhost.localdomain> Content-Type: multipart/mixed; boundary="------------080106060007050109040308" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200407/99 X-Sequence-Number: 10483 This is a multi-part message in MIME format. --------------080106060007050109040308 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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 --------------080106060007050109040308 Content-Type: text/x-java; name="TestDoublePrecision.java" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="TestDoublePrecision.java" 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); } } } --------------080106060007050109040308--