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

From Csaba Nagy
Subject Re: somebody could explain this?
Date
Msg-id 1131121880.4892.35.camel@coppola.muc.ecircle.de
Whole thread Raw
In response to somebody could explain this?  ("Cristian Prieto" <cristian@clickdiario.com>)
List pgsql-hackers
Cristian,

I bet it's related to some rounding issue and the fact that floating
formats are approximative even for small integers.
Probably 12 ands up being slightly less in floating format (something
like 11.999...), and the cast to integer is truncating it.
Not 100% sure though... read up on your API, I'm not a C programmer :-)

HTH,
Csaba.


On Fri, 2005-11-04 at 17:16, 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:
> 
> #include "postgres.h"
> #include "fmgr.h"
> 
> PG_FUNCTION_INFO_V1(test);
> 
> Datum
> repeat_item(PG_FUNCTION_ARGS)
> {
>     int num_times;
>     
>     num_times = PG_GETARG_FLOAT8(0) * 100;
>     PG_RETURN_INT32(num_times);
> }
> 
> 
> Inside psql this happens:
> 
> # Create or replace function test(float) returns integer as 'test.so'
> language 'c' stable;
> 
> select test(0.1);
> Returns 10
> 
> Select test(0.11);
> Returns 11
> 
> Select test(0.12)
> Returns 11
> 
> Select test(0.13)
> Returns 13
> 
> Select test(0.14)
> Returns 14
> 
> Select test(0.15)
> Returns 14
> 
> 
> What Is happening here?
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match



pgsql-hackers by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: Reducing the overhead of NUMERIC data
Next
From: "Jim C. Nasby"
Date:
Subject: Crash during elog.c...