Author Topic: Building a ScriptBasic extension module  (Read 17467 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #30 on: May 30, 2011, 10:52:42 AM »
I have run into what is called Inline Functions and looking for advice how I can implement it in the SB GSL extension module. This is an example of the prototype for a inline function.

Code: [Select]
extern inline double GSL_MAX_DBL (double a, double b)

Quote
This function returns the maximum of the double precision numbers a and b using an inline function. The use of a function allows for type checking of the arguments as an extra safety feature. On platforms where inline functions are not available the macro GSL_MAX will be automatically substituted.


AIR

  • Guest
Re: Building a ScriptBasic extension module
« Reply #31 on: May 30, 2011, 08:22:11 PM »
I have run into what is called Inline Functions and looking for advice how I can implement it in the SB GSL extension module. This is an example of the prototype for a inline function.

Code: [Select]
extern inline double GSL_MAX_DBL (double a, double b)

Quote
This function returns the maximum of the double precision numbers a and b using an inline function. The use of a function allows for type checking of the arguments as an extra safety feature. On platforms where inline functions are not available the macro GSL_MAX will be automatically substituted.

No need, as far as I can see for GSL_MAX_DBL

It's defined as:

Code: [Select]
INLINE_FUN double
GSL_MAX_DBL (double a, double b)
{
  return GSL_MAX (a, b);
}

GSL_MAX is a Macro:

Code: [Select]
#define GSL_MAX(a,b) ((a) > (b) ? (a) : (b))
Which translated to basic-like syntax would be something like:
Code: [Select]
if a > b then return a else return b
So my thoughts are that you can call the GSL_MAX macro directly, OR call either the inlined function GSL_MAX_DBL() or the standard function gsl_max().

As long as you include the header file that contains these, all three should be available.

Since you would be creating a wrapper function yourself for this scenario, it would probably be best from a performance standpoint to use the Macro.  The functions above call it themselves, with the rational being that they provide type-safety that you in turn would provide with YOUR wrapper function.

A.


Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #32 on: May 30, 2011, 11:24:17 PM »
Here is what I have at this point. I think GSL_MIN_INT() is broken.

Code: [Select]
/* Maximum and Minimum functions */


besFUNCTION(_MIN)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN_DBL(a, b));
besEND


besFUNCTION(_MAX_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX_DBL(a, b));
besEND


besFUNCTION(_MIN_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND  

  besRETURN_LONG(GSL_MIN_INT(a, b));
besEND


besFUNCTION(_MAX_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND  

  besRETURN_LONG(GSL_MAX_INT(a, b));
besEND


besFUNCTION(_MIN_LDBL)
  long double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN_LDBL(a, b));
besEND


besFUNCTION(_MAX_LDBL)
  long double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX_LDBL(a, b));
besEND

Code: [Select]
DECLARE SUB _MIN ALIAS "_MIN" LIB "gsl"
DECLARE SUB _MAX ALIAS "_MAX" LIB "gsl"
DECLARE SUB MIN_DBL ALIAS "_MIN_DBL" LIB "gsl"
DECLARE SUB MAX_DBL ALIAS "_MAX_DBL" LIB "gsl"
DECLARE SUB MIN_INT ALIAS "_MIN_INT" LIB "gsl"
DECLARE SUB MAX_INT ALIAS "_MAX_INT" LIB "gsl"
DECLARE SUB MIN_LDBL ALIAS "_MIN_LDBL" LIB "gsl"
DECLARE SUB MAX_LDBL ALIAS "_MAX_LDBL" LIB "gsl"

a_d = 12.5
b_d = 25.0

a_i = 10
b_i = 20


PRINT "MIN: ",FORMAT("%g",_MIN(a_d,b_d)),"\n"
PRINT "MAX: ",FORMAT("%g",_MAX(a_d,b_d)),"\n"
PRINT "MIN_DBL: ",FORMAT("%g",MIN_DBL(a_d,b_d)),"\n"
PRINT "MAX_DBL: ",FORMAT("%g",MAX_DBL(a_d,b_d)),"\n"
PRINT "MIN_INT: ",FORMAT("%g",MIN_INT(a_i,b_i)),"\n"
PRINT "MAX_INT: ",FORMAT("%g",MAX_INT(a_i,b_i)),"\n"
PRINT "MIN_LDBL: ",FORMAT("%g",MIN_LDBL(a_d,b_d)),"\n"
PRINT "MAX_LDBL: ",FORMAT("%g",MAX_LDBL(a_d,b_d)),"\n"

MIN: 12.5
MAX: 25
MIN_DBL: 12.5
MAX_DBL: 25
MIN_INT: 0
MAX_INT: 20
MIN_LDBL: -nan
MAX_LDBL: -nan

Based on your comment of MIN / MAX is used by default, I used a_i and b_i for the GSL_MIN and GSL_MAX functions and it work fine. I still think GSL_MIN_INT is broken. I'm not sure what is going on with GSL_MIN_LDBL and GSL_MAX_LDBL.
« Last Edit: May 30, 2011, 11:44:20 PM by ABB »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #33 on: May 31, 2011, 04:47:30 PM »
I ran across the following which may shed some light on the inline functions for GSL.

Quote from: Brian Gough
The inline keyword is not part of ANSI C, only C++.

Code: [Select]
/* sys/minmax.c
 *
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman, Brian Gough
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>

#define GSL_MAX(a,b) ((a) > (b) ? (a) : (b))
#define GSL_MIN(a,b) ((a) < (b) ? (a) : (b))

int GSL_MAX_INT (int a, int b);
int GSL_MIN_INT (int a, int b);
double GSL_MAX_DBL (double a, double b);
double GSL_MIN_DBL (double a, double b);
long double GSL_MAX_LDBL (long double a, long double b);
long double GSL_MIN_LDBL (long double a, long double b);

int
GSL_MAX_INT (int a, int b)
{
  return GSL_MAX (a, b);
}

int
GSL_MIN_INT (int a, int b)
{
  return GSL_MIN (a, b);
}

double
GSL_MAX_DBL (double a, double b)
{
  return GSL_MAX (a, b);
}

double
GSL_MIN_DBL (double a, double b)
{
  return GSL_MIN (a, b);
}

long double
GSL_MAX_LDBL (long double a, long double b)
{
  return GSL_MAX (a, b);
}

long double
GSL_MIN_LDBL (long double a, long double b)
{
  return GSL_MIN (a, b);
}

« Last Edit: May 31, 2011, 05:02:08 PM by ABB »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #34 on: May 31, 2011, 05:52:03 PM »
Based on the above post and Armando's suggestions, I changed the code as follows. I doesn't seem to be working as expected by defaulting to the standard GSL_MIN and GSL_MAX macros.

Code: [Select]
/* Maximum and Minimum functions */


besFUNCTION(_MIN)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MIN_DBL(a, b))
  {
    return GSL_MIN (a, b);
  }
besEND


besFUNCTION(_MAX_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MAX_DBL(a, b))
  {
    return GSL_MAX (a, b);
  }
besEND


besFUNCTION(_MIN_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND 

  besRETURN_LONG(GSL_MIN_INT(a, b))
  {
    return GSL_MIN (a, b);
  }
besEND


besFUNCTION(_MAX_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND 

  besRETURN_LONG(GSL_MAX_INT(a, b))
  {
    return GSL_MAX (a, b);
  }
besEND


besFUNCTION(_MIN_LDBL)
  long double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MIN_LDBL(a, b))
  {
    return GSL_MIN (a, b);
  }
besEND


besFUNCTION(_MAX_LDBL)
  long double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MAX_LDBL(a, b))
  {
    return GSL_MAX (a, b);
  }
besEND

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #35 on: May 31, 2011, 06:08:35 PM »
It seems the above doesn't work so I just made each function use GSL_MIN and GSL_MAX functions for all the functions. It still hasn't change from my original version. (GSL_MIN_INT returns zero and the LDBL functions return -nan)

Code: [Select]
/* Maximum and Minimum functions */


besFUNCTION(_MIN)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_LDBL)
  long double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX_LDBL)
  long double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND

Code: [Select]
DECLARE SUB _MIN ALIAS "_MIN" LIB "gsl"
DECLARE SUB _MAX ALIAS "_MAX" LIB "gsl"
DECLARE SUB MIN_DBL ALIAS "_MIN_DBL" LIB "gsl"
DECLARE SUB MAX_DBL ALIAS "_MAX_DBL" LIB "gsl"
DECLARE SUB MIN_INT ALIAS "_MIN_INT" LIB "gsl"
DECLARE SUB MAX_INT ALIAS "_MAX_INT" LIB "gsl"
DECLARE SUB MIN_LDBL ALIAS "_MIN_LDBL" LIB "gsl"
DECLARE SUB MAX_LDBL ALIAS "_MAX_LDBL" LIB "gsl"

a_d = 12.5
b_d = 25.0

a_i = 10
b_i = 20


PRINT "MIN: ",FORMAT("%g",_MIN(b_i,a_i)),"\n"
PRINT "MAX: ",FORMAT("%g",_MAX(b_i,a_i)),"\n"
PRINT "MIN_DBL: ",FORMAT("%g",MIN_DBL(a_d,b_d)),"\n"
PRINT "MAX_DBL: ",FORMAT("%g",MAX_DBL(a_d,b_d)),"\n"
PRINT "MIN_INT: ",FORMAT("%g",MIN_INT(a_i,b_i)),"\n"
PRINT "MAX_INT: ",FORMAT("%g",MAX_INT(a_i,b_i)),"\n"
PRINT "MIN_LDBL: ",FORMAT("%g",MIN_LDBL(a_d,b_d)),"\n"
PRINT "MAX_LDBL: ",FORMAT("%g",MAX_LDBL(a_d,b_d)),"\n"

MIN: 10
MAX: 20
MIN_DBL: 12.5
MAX_DBL: 25
MIN_INT: 0
MAX_INT: 20
MIN_LDBL: -nan
MAX_LDBL: -nan
« Last Edit: May 31, 2011, 06:11:13 PM by ABB »

AIR

  • Guest
Re: Building a ScriptBasic extension module
« Reply #36 on: May 31, 2011, 07:59:09 PM »
"-nan" means "Not A Number", basically saying the values being passed aren't recognised.

Does SB have a LONG DOUBLE type?  I think that's where the problem lies

The following works here (32bit Xubuntu):

Code: [Select]
/* Maximum and Minimum functions */


besFUNCTION(_MIN)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MIN_DBL(a, b));
besEND


besFUNCTION(_MAX_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MAX_DBL(a, b));
besEND


besFUNCTION(_MIN_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND 

  besRETURN_LONG(GSL_MIN_INT(a, b));
besEND


besFUNCTION(_MAX_INT)
  int a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND 

  besRETURN_LONG(GSL_MAX_INT(a, b));
besEND


besFUNCTION(_MIN_LDBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MIN_LDBL((long double)a, (long double)b));
besEND


besFUNCTION(_MAX_LDBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND 

  besRETURN_DOUBLE(GSL_MAX_LDBL((long double)a, (long double)b));
besEND

MIN: 10
MAX: 20
MIN_DBL: 12.5
MAX_DBL: 25
MIN_INT: 10
MAX_INT: 20
MIN_LDBL: 12.5
MAX_LDBL: 25


Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #37 on: May 31, 2011, 09:32:06 PM »
I think your right about SB not supporting a long double. I still have the problem with GSL_MIN_INT returning zero. I copied and pasted your code verbatim and recompiled the gsl.so extension module. The only difference I can see is your running under 32 bit and I'm running under 64 bit.

MIN: 10
MAX: 20
MIN_DBL: 12.5
MAX_DBL: 25
MIN_INT: 0
MAX_INT: 20
MIN_LDBL: 12.5
MAX_LDBL: 25

I will try this on my desktop Ubuntu 11.04 32 bit version and see if it works there.

« Last Edit: May 31, 2011, 09:38:44 PM by ABB »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #38 on: June 01, 2011, 08:06:04 AM »
I copied the interface.c, basext.h and minmax.sb from my Ubuntu 64 bit laptop to my Ubuntu 32 bit desktop. I recompiled the gsl.so extension module and ran the minmax.sb test program. I got the same results as you. Seems to be  a problem with GSL_MIN_INT() under 64 bit.
« Last Edit: June 02, 2011, 10:14:41 AM by ABB »

AIR

  • Guest
Re: Building a ScriptBasic extension module
« Reply #39 on: June 01, 2011, 01:47:48 PM »
Seems to be a problem with GSL_MIN_INT() under 64 bit.  :'(

The function/macro is fine.  What you're seeing is an odd by-product of 64bit computation while working on 32bit numbers.

Change it to the following and it works (at least with 64bit Xubuntu 10.10):
Code: [Select]
besFUNCTION(_MIN_INT)
  long a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND 
  besRETURN_LONG(GSL_MIN_INT(a,b));
besEND


besFUNCTION(_MAX_INT)
  long a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND 

  besRETURN_LONG(GSL_MAX_INT(a, b));
besEND

MIN: 10
MAX: 20
MIN_DBL: 12.5
MAX_DBL: 25
MIN_INT: 10
MAX_INT: 20
MIN_LDBL: 12.5
MAX_LDBL: 25

Remember, an INT is a 32bit number in 32/64 bit OS's no matter what.  A LONG, on the other hand, is a 32bit number like an INT under a 32bit OS, but it's a 64bit number under a 64bit OS.

I think what's happening is that the 64bit version of SB interprets "ii" to be longs, but I'm guessing here.

A.

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #40 on: June 01, 2011, 03:11:38 PM »
jrs@laptop:~/sb/test/gsl$ scriba minmax.sb
MIN: 10
MAX: 20
MIN_DBL: 12.5
MAX_DBL: 25
MIN_INT: 10
MAX_INT: 20
MIN_LDBL: 12.5
MAX_LDBL: 25
jrs@laptop:~/sb/test/gsl$

Thank You !

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #41 on: June 01, 2011, 10:45:38 PM »
This is a beta version of the GSL Mathematical Functions portion of the library. I have attached the 32 and 64 bit version for Linux if you would like to try it out. I plan to create a gsl.bas module include file when the library has been fully implemented in ScriptBasic.

Code: [Select]
/*
   GNU Scientific Library
   Based on GSL 1.15
   Interface By: John Spikowski
   Refinements By: Armando I. Rivera (AIR)
   Version 0.01
*/

#include <stdio.h>
#include "../../basext.h"
#include <gsl/gsl_math.h>
      
besVERSION_NEGOTIATE
    return (int)INTERFACE_VERSION;
besEND

besSUB_START

besEND

besSUB_FINISH

besEND

/* Elementary Functions */

besFUNCTION(_log1p)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_log1p(x));
besEND


besFUNCTION(_expm1)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_expm1(x));
besEND


besFUNCTION(_hypot)
  double x,y;

  besARGUMENTS("rr")
    &x,&y
  besARGEND  

  besRETURN_DOUBLE(gsl_hypot(x, y));
besEND


besFUNCTION(_hypot3)
  double x,y,z;
  
  besARGUMENTS("rrr")
    &x,&y,&z
  besARGEND  

  besRETURN_DOUBLE(gsl_hypot3(x, y, z));
besEND


besFUNCTION(_acosh)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_acosh(x));
besEND


besFUNCTION(_asinh)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_asinh(x));
besEND


besFUNCTION(_atanh)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_atanh(x));
besEND


besFUNCTION(_ldexp)
  double x;
  int y;

  besARGUMENTS("ri")
    &x,&y
  besARGEND  

  besRETURN_DOUBLE(gsl_ldexp(x, y));
besEND


besFUNCTION(_frexp)
  double f;
  LEFTVALUE e;

  besARGUMENTS("r")
    &f
  besARGEND
  besLEFTVALUE(besARGUMENT(2),e);
  
  besRETURN_DOUBLE(gsl_frexp(f, *e));
besEND


/* Small integer powers */


besFUNCTION(_pow_int)
  double x;
  int n;

  besARGUMENTS("ri")
    &x,&n
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_int(x, n));
besEND


besFUNCTION(_pow_uint)
  double x;
  unsigned int n;

  besARGUMENTS("ri")
    &x,&n
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_uint(x, n));
besEND


besFUNCTION(_pow_2)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_2(x));
besEND


besFUNCTION(_pow_3)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_3(x));
besEND


besFUNCTION(_pow_4)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_4(x));
besEND


besFUNCTION(_pow_5)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_5(x));
besEND


besFUNCTION(_pow_6)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_6(x));
besEND


besFUNCTION(_pow_7)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_7(x));
besEND


besFUNCTION(_pow_8)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_8(x));
besEND


besFUNCTION(_pow_9)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_pow_9(x));
besEND


/* Testing the Sign of Numbers */


besFUNCTION(_SIGN)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_LONG(GSL_SIGN(x));
besEND


/* Testing for Odd and Even Numbers */


besFUNCTION(_IS_ODD)
  int x;

  besARGUMENTS("i")
    &x
  besARGEND  

  besRETURN_LONG(GSL_IS_ODD(x));
besEND


besFUNCTION(_IS_EVEN)
  int x;

  besARGUMENTS("i")
    &x
  besARGEND  

  besRETURN_LONG(GSL_IS_EVEN(x));
besEND


/* Maximum and Minimum functions */


besFUNCTION(_MIN)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN(a, b));
besEND


besFUNCTION(_MAX)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX(a, b));
besEND


besFUNCTION(_MIN_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN_DBL(a, b));
besEND


besFUNCTION(_MAX_DBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX_DBL(a, b));
besEND


besFUNCTION(_MIN_INT)
  long a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND  
  besRETURN_LONG(GSL_MIN_INT(a,b));
besEND


besFUNCTION(_MAX_INT)
  long a,b;

  besARGUMENTS("ii")
    &a,&b
  besARGEND  

  besRETURN_LONG(GSL_MAX_INT(a, b));
besEND


besFUNCTION(_MIN_LDBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MIN_LDBL((long double)a, (long double)b));
besEND


besFUNCTION(_MAX_LDBL)
  double a,b;

  besARGUMENTS("rr")
    &a,&b
  besARGEND  

  besRETURN_DOUBLE(GSL_MAX_LDBL((long double)a, (long double)b));
besEND


/* Approximate Comparison of Floating Point Numbers */


besFUNCTION(_fcmp)
  double x,y,epsilon;
  
  besARGUMENTS("rrr")
    &x,&y,&epsilon
  besARGEND  

  besRETURN_LONG(gsl_fcmp(x, y, epsilon));
besEND



Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #42 on: June 02, 2011, 11:50:55 PM »
I have started working on the Representation of complex numbers section of the GSL library. The functions return a packed two element array which I'm trying to return as a binary string. The goal was to use the ScriptBasic UNPACK function to split the two double values returned. It looks like gcc is complaining about the returning type being incompatible.

Code: [Select]
/*
   GNU Scientific Library
   Based on GSL 1.15
   Interface By: John Spikowski
   Refinements By: Armando I. Rivera (AIR)
   Version 0.02
*/

#include <stdio.h>
#include "../../basext.h"
#include <gsl/gsl_math.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>

.
.
.

/* Representation of complex numbers */


besFUNCTION(_complex_rect)
  double x,y;

  besARGUMENTS("rr")
    &x,&y
  besARGEND  

  besRETURN_STRING(gsl_complex_rect(x, y));
besEND

Code: [Select]
jrs@laptop:~/sb/scriptbasic/extensions/gsl$ make -B
gcc -w -m64 -fPIC -DSTATIC_LINK=1 -c -lgslcblas -lgsl -o /home/jrs/sb/scriptbasic/bin/mod/obj/gsl/s_interface.o interface.c
interface.c: In function ‘_complex_rect’:
interface.c:400:3: error: invalid operands to binary == (have ‘void *’ and ‘gsl_complex’)
interface.c:400:3: error: incompatible type for argument 1 of ‘strlen’
interface.c:400:3: note: expected ‘const char *’ but argument is of type ‘gsl_complex’
interface.c:400:3: error: incompatible type for argument 2 of ‘memcpy’
interface.c:400:3: note: expected ‘const void *’ but argument is of type ‘gsl_complex’
make: *** [/home/jrs/sb/scriptbasic/bin/mod/obj/gsl/s_interface.o] Error 1
jrs@laptop:~/sb/scriptbasic/extensions/gsl$

« Last Edit: June 02, 2011, 11:54:58 PM by JRS »

Offline AIR

  • BASIC Developer
  • Posts: 685
Re: Building a ScriptBasic extension module
« Reply #43 on: June 03, 2011, 06:49:25 PM »
It's a struct, which is why the compiler throws errors when you try to return a string.

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #44 on: June 03, 2011, 06:54:09 PM »
I was thinking rather then a direct return, convert it in C before passing it back to scriba. Your thoughts on the best way to handle this?