pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem with C functions ?
2+ messages / 2 participants
[nested] [flat]

* Problem with C functions ?
@ 1999-11-12 09:47 Manuel Weindorf <weindorf@ipf.uni-karlsruhe.de>
  1999-11-12 10:16 ` Re: [INTERFACES] Problem with C functions ? Karel Zak - Zakkr <zakkr@zf.jcu.cz>
  0 siblings, 1 reply; 2+ messages in thread

From: Manuel Weindorf @ 1999-11-12 09:47 UTC (permalink / raw)
  To: pgsql-interfaces

Hi there,

I compiled a C-funtion to compute the azimuth of a LSEG but the function
never
returns the correct value, but elog( ... ) report the correct value.
So there seems to be a problem returning doubles or floats ??

compile the folling function with
gcc -fPIC -O2 -shared  -o lseg_azimuth.o -c lseg_azimuth.c
-I/opt/local/postgres/include -L/opt/local/postgres/lib
ld -G -Bdynamic -o libgeom.so   lseg_azimuth.o 

#include <math.h>

#include "postgres.h"
#include "utils/geo_decls.h"

double lseg_azimuth( LSEG* lseg );
float add_one( float arg );

double lseg_azimuth( LSEG* lseg )
{
  double x1, y1;
  double x2, y2;
  double ori;

  x1 = lseg->p[0].x;
  y1 = lseg->p[0].y;

  x2 = lseg->p[1].x;
  y2 = lseg->p[1].y;

  ori = atan2( (x2-x1), (y2-y1));
  ori = (ori / M_PI) * 200.0;

  if ( ori < 0.0 )
    ori += 400.0;

  elog( NOTICE, "ori = %8.3lf", ori );

  return( ori );  
}


then use:

create function lseg_azimuth(lseg) returns float8
  as ' ... path to the shared lib you just created ...' language 'c';


create table xxx (geometry lseg);

insert into xxx (geometry) values
('[(4148.675,5414.255),(4151.405,5414.255)]');

select lseg_azimuth(geometry) from xxx;

will return:

NOTICE:  ori =  100.000
lseg_azimuth
------------
    4148.675
(1 row)

But that's obviously not what it should be ...

Maybe you have any suggestions ?

many thanks
Manuel
-- 
Manuel Weindorf (weindorf@ipf.bau-verm.uni-karlsruhe.de)
Institut fuer Photogrammetrie und Fernerkundung, Universitaet Karlsruhe
Postfach 6980 D-76128 Karlsruhe Tel. +49721 6086010 Fax +49721 694568
http://www-ipf.bau-verm.uni-karlsruhe.de



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

* Re: [INTERFACES] Problem with C functions ?
  1999-11-12 09:47 Problem with C functions ? Manuel Weindorf <weindorf@ipf.uni-karlsruhe.de>
@ 1999-11-12 10:16 ` Karel Zak - Zakkr <zakkr@zf.jcu.cz>
  0 siblings, 0 replies; 2+ messages in thread

From: Karel Zak - Zakkr @ 1999-11-12 10:16 UTC (permalink / raw)
  To: Manuel Weindorf <weindorf@ipf.uni-karlsruhe.de>; +Cc: pgsql-interfaces





On Fri, 12 Nov 1999, Manuel Weindorf wrote:

> Hi there,
> 
> I compiled a C-funtion to compute the azimuth of a LSEG but the function
> never
> returns the correct value, but elog( ... ) report the correct value.
> So there seems to be a problem returning doubles or floats ??
> 
> compile the folling function with
> gcc -fPIC -O2 -shared  -o lseg_azimuth.o -c lseg_azimuth.c
> -I/opt/local/postgres/include -L/opt/local/postgres/lib
> ld -G -Bdynamic -o libgeom.so   lseg_azimuth.o 
> 
> #include <math.h>
> 
> #include "postgres.h"
> #include "utils/geo_decls.h"
> 
> double lseg_azimuth( LSEG* lseg );
> float add_one( float arg );
> 
> double lseg_azimuth( LSEG* lseg )
> {
>   double x1, y1;
>   double x2, y2;
>   double ori;
> 
>   x1 = lseg->p[0].x;
>   y1 = lseg->p[0].y;
> 
>   x2 = lseg->p[1].x;
>   y2 = lseg->p[1].y;
> 
>   ori = atan2( (x2-x1), (y2-y1));
>   ori = (ori / M_PI) * 200.0;
> 
>   if ( ori < 0.0 )
>     ori += 400.0;
> 
>   elog( NOTICE, "ori = %8.3lf", ori );
> 
>   return( ori );  
> }

(See in PgSQL source code src/backend/utils/adt/ and geo routines).

Try this:

double*
lseg_azimuth( LSEG* lseg )
{
	double *result = (double) palloc(sizeof(double));	

	......

	*result = any_value;	

	......
	
	return result;
}
 
							Karel
						

------------------------------------------------------------------------------
Karel Zak <zakkr@zf.jcu.cz>                      http://home.zf.jcu.cz/~zakkr/

Docs:         http://docs.linux.cz                          (big docs archive)	
Kim Project:  http://home.zf.jcu.cz/~zakkr/kim/              (process manager)
FTP:          ftp://ftp2.zf.jcu.cz/users/zakkr/              (C/ncurses/PgSQL)
------------------------------------------------------------------------------




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


end of thread, other threads:[~1999-11-12 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-11-12 09:47 Problem with C functions ? Manuel Weindorf <weindorf@ipf.uni-karlsruhe.de>
1999-11-12 10:16 ` Karel Zak - Zakkr <zakkr@zf.jcu.cz>

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