Re: Floating point error

From: Edson Richter <edsonrichter(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Floating point error
Date: 2013-02-25 03:02:53
Message-ID: BLU0-SMTP15717703966282A8B775FF3CFF30@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Em 24/02/2013 23:44, Adrian Klaver escreveu:
> On 02/24/2013 06:13 PM, Tom Duffey wrote:
>> Hi Everyone,
>>
>> Riddle me this. I have a database column of type "real" that gets
>> mapped to a Java field of type double via JDBC. We have two
>> databases, test and production, and the test database is periodically
>> blown away and reloaded from a copy of production. We recently
>> noticed that some values do not match when viewed within our
>> application on test vs. production. More specifically:
>>
>> - Selecting values from both test and production DBs using psql shows
>> "10.3885" as the value
>> - The Java app on production shows "10.3884573" while the test app
>> shows "10.3885"
>>
>> I have a hunch that when the value was originally inserted into the
>> production DB it probably contained more than the 6 digits supported
>> by the real data type. It may have even been exactly the "10.3884573"
>> value we see when retrieving via JDBC on production. What I don't
>> understand is why when the value gets mapped back to Java via JDBC
>> those extra digits are coming back. Can anyone explain this or do you
>> think I'm on the wrong track? I stepped through code and it sure
>> seems like the extra information is coming back from the JDBC driver.
>
> Are the production and test apps running on the same platform i.e. OS,
> bitness, etc.

According to the Java Language specification, double and real are not
precise data types because the how it is stored in binary, which in turn
result in such errors.

See here:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html

See also a discussion about how to overcome here:

http://stackoverflow.com/questions/179427/how-to-resolve-a-java-rounding-double-issue

This issue is not exclusive from Java, other languages based on IEEE 754
standard's suffer of same problem.
That's the reason because BigDecimal exists.

Regards,

Edson

>
>>
>> Tom
>>
>> --
>> Tom Duffey
>> tduffey(at)trillitech(dot)com
>> 414-751-0600 x102
>>
>>
>>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-02-25 03:17:33 Re: Floating point error
Previous Message Tom Duffey 2013-02-25 02:58:30 Re: Floating point error

Browse pgsql-hackers by date

  From Date Subject
Next Message Adrian Klaver 2013-02-25 03:17:33 Re: Floating point error
Previous Message Michael Paquier 2013-02-25 02:59:51 Re: Support for REINDEX CONCURRENTLY