dbTalk Databases Forums  

Selecting reals into doubles

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Selecting reals into doubles in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Will Newton
 
Posts: n/a

Default Selecting reals into doubles - 03-03-2004 , 05:19 AM







I have attached some SQL which produces what to me, at least, is
rather unexpected results. Selecting real columns into double
precision columns loses some precision. Is this expected or documented
anywhere?

Thanks,


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


Reply With Quote
  #2  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Selecting reals into doubles - 03-03-2004 , 08:17 AM






On Wed, Mar 03, 2004 at 11:19:15 +0000,
Will Newton <will (AT) gbdirect (DOT) co.uk> wrote:
Quote:
I have attached some SQL which produces what to me, at least, is
rather unexpected results. Selecting real columns into double
precision columns loses some precision. Is this expected or documented
anywhere?
You left out the output. But probably what you are seeing are the effects
of increased precision not decreased precision. Neither of the two
numbers you entered is exactly representable as floating point numbers.
When being printed as single precision numbers you got the same thing
back as you entered because within the number of digits used to display
single precision numbers those are going to be the closest to what is
stored. This isn't going to be the case for double precision numbers
in general.

If you really want exact decimal fractions, you want to use the numeric type
to store them.

Quote:
Thanks,

DROP TABLE precision_test;
DROP TABLE precision_test2;

CREATE TABLE precision_test
(
foo real
);

INSERT INTO precision_test
SELECT 20.20
UNION SELECT 1969.22;

CREATE TABLE precision_test2
(
foo double precision
);

INSERT INTO precision_test2 (foo) SELECT foo from precision_test;


SELECT * FROM precision_test;
SELECT * FROM precision_test2;


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: Selecting reals into doubles - 03-03-2004 , 09:04 AM



Will Newton <will (AT) gbdirect (DOT) co.uk> writes:
Quote:
I have attached some SQL which produces what to me, at least, is
rather unexpected results. Selecting real columns into double
precision columns loses some precision. Is this expected or documented
anywhere?
You shouldn't be surprised; this is a fundamental behavior of floating
point arithmetic anywhere.

There isn't any "loss of precision" per se --- the value represented in
the float8 column is the same as what was in the float4 column. The
difference is that the float8 output routine is programmed to print
about 15 digits of precision whereas the float4 routine prints no more
than 6. So you get to see the fact that the stored value wasn't really
20.2 but only something close to it.

If you find this surprising maybe you should be using type "numeric".

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.