pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
float4
3+ messages / 3 participants
[nested] [flat]

* float4
@ 1999-06-28 19:43 JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
  1999-06-28 20:30 ` Re: [ADMIN] float4 Don Garrett <garrett@bgb-consulting.com>
  1999-06-28 23:32 ` Re: [SQL] float4 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 3+ messages in thread

From: JT Kirkpatrick @ 1999-06-28 19:43 UTC (permalink / raw)
  To: 'pgsql-admin@hub.org' <pgsql-admin@hub.org>; 'pgsql-sql@hub.org' <pgsql-sql@hub.org>

we have a database (linux, 6.4.2, new odbc, connections from msaccess / 
win98) -- access doesn't fully appreciate the monetary type in postgres, so 
we made all monetary fields FLOAT4's.  weird stuff -- or maybe not to you 
-- we insert 23516.69 into a float4 field directly in psql, and then select 
it, and it shows 23516.7.  this is not good for a field used for MONEY!!! 
 what are the limitations of float4?  should i have used float8?

thanks for any help you can offer. . .

jt




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [ADMIN] float4
  1999-06-28 19:43 float4 JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
@ 1999-06-28 20:30 ` Don Garrett <garrett@bgb-consulting.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Don Garrett @ 1999-06-28 20:30 UTC (permalink / raw)
  To: ; +Cc: 'pgsql-admin@hub.org' <pgsql-admin@hub.org>; 'pgsql-sql@hub.org' <pgsql-sql@hub.org>

  You shouldn't use a float at all for exact amounts. Floats handle
arbitrary precision, but do it rounding off values somewhat. The way the
round off works is based on the (base 2) internal representation and so
it never quite seems to make sense in decimal. It's possible on some
machines/float formats to have "if (1.0 + 1.0 = 2.0)" evaluate to false.

  When I first started programming, I did a very simple custom
accounting package using floats.... and had to go back and rewrite all
of it.

  You will do better to use integers (no rounding at all) and consider
the contents to be the number of pennies involved, not dollars.

JT Kirkpatrick wrote:
> 
> we have a database (linux, 6.4.2, new odbc, connections from msaccess /
> win98) -- access doesn't fully appreciate the monetary type in postgres, so
> we made all monetary fields FLOAT4's.  weird stuff -- or maybe not to you
> -- we insert 23516.69 into a float4 field directly in psql, and then select
> it, and it shows 23516.7.  this is not good for a field used for MONEY!!!
>  what are the limitations of float4?  should i have used float8?
> 
> thanks for any help you can offer. . .
> 
> jt

--
Don Garrett                                          dgarrett@acm.org
BGB Consulting                  http://www.bgb-consulting.com/garrett



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [SQL] float4
  1999-06-28 19:43 float4 JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
@ 1999-06-28 23:32 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 1999-06-28 23:32 UTC (permalink / raw)
  To: JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>; +Cc: 'pgsql-admin@hub.org' <pgsql-admin@hub.org>; 'pgsql-sql@hub.org' <pgsql-sql@hub.org>

JT Kirkpatrick <jt-kirkpatrick@mpsllc.com> writes:
> we made all monetary fields FLOAT4's.  weird stuff -- or maybe not to you 
> -- we insert 23516.69 into a float4 field directly in psql, and then select 
> it, and it shows 23516.7.  this is not good for a field used for MONEY!!! 

Yup, no surprise.  float4 is good to about 6 decimal digits on most
machines, and yours is evidently right in there with the pack.

float8 is good to about 16 digits on most hardware, but I wouldn't
really recommend it either if you need guaranteed-exact calculations.

The money type in Postgres is a fairly crude hack that I can't recommend
(it's basically an int4 representing cents, and will therefore poop out
at 2^31 cents or about $20million).

What you really want is the general "numeric" type added in Postgres 6.5
--- decimal representation with as many digit positions as you specify.
It's a good deal slower for calculations than native floats,
unfortunately, especially if you use a very wide numeric field.
But calculations per-se are seldom the bottleneck for a database
application...

BTW, the current plan is to phase out the money type in favor of numeric
in some future release.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~1999-06-28 23:32 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-06-28 19:43 float4 JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
1999-06-28 20:30 ` Don Garrett <garrett@bgb-consulting.com>
1999-06-28 23:32 ` Tom Lane <tgl@sss.pgh.pa.us>

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