Обсуждение: Divide a float4 by 1 - what is going on???????
Hi people, can anyone explain what is going on here : ? CIMSOFT=# CREATE TEMP TABLE test (n1 FLOAT4); CREATE TABLE CIMSOFT=# INSERT INTO test (n1) VALUES (2.456677); INSERT 6571521 1 CIMSOFT=# SELECT * FROM test; n1 --------- 2.45668 (1 row) CIMSOFT=# SELECT n1/1 FROM test; ?column? ------------------ 2.45667695999146 (1 row) Why I get so many digits by a division with one? Anybody should have learned that everything / 1 = everything ;-) Thnx, Daniel PS : PostgreSQL 8.0.0 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC) 3.4.2 (mingw
Float provides 6-7 digits of precision. I see nothing surprising down below. > -----Original Message----- > From: pgsql-general-owner@postgresql.org [mailto:pgsql-general- > owner@postgresql.org] On Behalf Of Daniel Schuchardt > Sent: Friday, September 16, 2005 5:13 PM > To: pgsql-general@postgresql.org > Subject: [GENERAL] Divide a float4 by 1 - what is going on??????? > > Hi people, > > can anyone explain what is going on here : ? > > CIMSOFT=# CREATE TEMP TABLE test (n1 FLOAT4); > CREATE TABLE > CIMSOFT=# INSERT INTO test (n1) VALUES (2.456677); > INSERT 6571521 1 > CIMSOFT=# SELECT * FROM test; > n1 > --------- > 2.45668 > (1 row) > > CIMSOFT=# SELECT n1/1 FROM test; > ?column? > ------------------ > 2.45667695999146 > (1 row) > > Why I get so many digits by a division with one? Anybody should have > learned that everything / 1 = everything ;-) > > Thnx, > Daniel > > PS : > > PostgreSQL 8.0.0 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC) 3.4.2 > (mingw > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org
On Sat, Sep 17, 2005 at 02:12:45AM +0200, Daniel Schuchardt wrote: > CIMSOFT=# SELECT n1/1 FROM test; > ?column? > ------------------ > 2.45667695999146 > (1 row) > > Why I get so many digits by a division with one? Anybody should have > learned that everything / 1 = everything ;-) Looks like the division is being done in double precision (float8) and you're seeing the effects of an inexact representation. test=> SELECT 2.456677::real / 1; ?column? ------------------ 2.45667695999146 (1 row) test=> SELECT 2.456677::real / 1::real; ?column? ---------- 2.45668 (1 row) -- Michael Fuhr