Re: somebody could explain this? - Mailing list pgsql-hackers

From Martijn van Oosterhout
Subject Re: somebody could explain this?
Date
Msg-id 20051104164542.GC13966@svana.org
Whole thread Raw
In response to somebody could explain this?  ("Cristian Prieto" <cristian@clickdiario.com>)
Responses Re: [OT] somebody could explain this?
Re: somebody could explain this?
List pgsql-hackers
On Fri, Nov 04, 2005 at 10:16:50AM -0600, Cristian Prieto wrote:
> Hello, I'm using PostgreSQL 8.0.4 in Fedora Core 3, right now I'm learning a
> little about the postgresql internals and the way some kind of SPs could be
> written in c language; I found something really weird and I cannot explain
> to me this behavior:

What's happening here is that the multiplication, being floating point,
has some accumulated error such that when you multiply it by 100 and
convert it to an int, it hits the cutoff.

Note: converting a float to an int tends to invoke trunc() or something
similar. trunc(14.999999999999) = 14. You need to decide whether maybe
you want round() instead. Or decide the actual cutoff yourself. I
sometimes fix this by adding 0.00001 to numbers before converting to
integer, to avoid these issues.

Floating points numbers are accurate but not precise.

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: postgresql-8.1RC1 on Solaris 10, amd64x2
Next
From: Andrew Dunstan
Date:
Subject: Re: somebody could explain this?