gettingstarted
[Top] [All Lists]

Re: Trouble from C to REALBasic

To: Getting Started <gettingstarted at lists dot realsoftware dot com>
Subject: Re: Trouble from C to REALBasic
From: Erick Tejkowski <lists at purplee dot net>
Date: Thu, 31 Mar 2005 06:33:58 -0600
Delivered-to: gettingstarted at lists dot realsoftware dot com
References: <9dff94d8b922cf4f92a18576ea611aba at utkux dot utcc dot utk dot edu>
To me, this line in your RB code looks suspect:

if x<1.0e-100 then

Is this supposed to be scientific notation? If so, I doubt that this is valid in RB. You'd need to get the value some other way.

I would also double-check all of your math functions like log and fabs/abs in RB compared to C. Otherwise, the code looks like it should work ... it looks like basic math functions. To debug the C code, just add a printf statement everywhere you want to learn a value.

Good luck,
Erick


On Mar 28, 2005, at 10:04 AM, Jeff Borckardt wrote:

I am trying to convert some code from C to REALBasic. I have the compiled C application (and the code) but I don't know how to step through it or compile the code myself. I have written a REALBasic application (and I can obviously step through it...very nicely)... I did my best to figure out how to convert the C-code to RB.

(FYI...The program uses Maximum Likelihood algorithms to estimate human motor thresholds.)

The problem is that the two applications are giving me different values... the C program gives the correct results (that I am trying to emulate with RB)... but I don't know exactly where in the RB program the problem is occurring.

I have pasted 3 functions from the C source and my RB interpretation. I chose these 3 because I have limited space in my emails to the RB list and I think these are the most likely to be the offending function(s).

Does anything jump out at anyone as being a problem here?

Thanks...Jeff

SOME OF THE C CODE:

double sureln(double x) {
  if (x<1.0e-100)
                return -12000;
  else
    return(log(x));
  return log(x);
}

double CuGauss(double x, double mue, double sigma) {

const double Wurzel2 = 1.414213562;
double xtrans,t,z,ans,hilf,hilf1,hilf2, hilf3,hilf4,erfcc;

   if (sigma<1.0e-10 ) {
     if (x<mue) return 0.0;
     else return 1.0;

     printf("Error in CuGauss\n");
     return -1; 
   }

   xtrans = (x-mue)/(Wurzel2*sigma);
   z  =  fabs(xtrans);

   if (z>50) {
     if (xtrans>0) return 1.0;
     else return 0.0;
     printf("Error in CuGauss\n");
     return -1;
   }

   t  =  1.0/(1.0+0.5*z);
   hilf = (-0.82215223+t*0.17087277);
   hilf1 = (1.48851587+t*hilf);
   hilf2 = (-1.13520398+t*hilf1);
   hilf3 = (0.27886807+t*hilf2);
   hilf4 = (-0.18628806+t*hilf3);
   ans  =  t*exp(-z*z-1.26551223+t*(1.00002368+
      t*(0.37409196+t*(0.09678418+t*hilf4))));

   if (xtrans >= 0.0)
                erfcc  =  ans;
   else
                erfcc  =  2.0-ans;
   return (0.5*(2.0-erfcc));
}

double funcfinal(double x) {
double L=0.0;
int i;
double xsigma;
  L = 0.0;
  xsigma = relSigmaFinal*x/100;
  for (i = 1; i<= nrStimulus;i++) {
    if (Erfolge[i]==1)
      L = L+sureln(1-CuGauss(x,Staerken[i],xsigma));
    else
      L = L+sureln((CuGauss(x,Staerken[i],xsigma)));
  }

  return -L;
}


THE CORRESPONDING REALBasic CODE:

Window1.sureln:
Protected Function sureln(x as double) As double
  if x<1.0e-100 then
    return -12000
  else
    return log(x)
  end
End Function

Window1.CuGauss:
Protected Function CuGauss(x as double, mue as double, sigma as double) As double
  dim Wurzel2 as double
  dim xtrans,t,z,ans,hilf,hilf1,hilf2,hilf3,hilf4,erfcc as double

  Wurzel2=1.414213562

  if sigma<1.0e-10 then
    if x<mue then
      return 0
    else
      return 1
    end
    msgBox "ERROR IN CUGAUSS"
    return -1
  end

  xtrans=(x-mue)/(Wurzel2*sigma)
  z=abs(xtrans)

  if z>50 then
    if xtrans>0 then
      return 1
    else
      return 0
    end
    msgBox "ERROR in CUGAUSS"
    return -1
  end

  t  =  1.0/(1.0+0.5*z)
  hilf = (-0.82215223+t*0.17087277)
  hilf1 = (1.48851587+t*hilf)
  hilf2 = (-1.13520398+t*hilf1)
  hilf3 = (0.27886807+t*hilf2)
  hilf4 = (-0.18628806+t*hilf3)
ans = t*exp(-z*z -1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+t*hilf4))))

  if xtrans>=0 then
    erfcc=ans
  else
    erfcc=2-ans
  end

  return (.5*(2-erfcc))

End Function

Window1.funcfinal:
Protected Function funcfinal(x as double) As double
  dim L as double
  dim i as integer
  dim xsigma as double

  L=0
  xsigma=relsigmaFinal*x/100
  for i=1 to nrstimulus
    if erfolge(i)=1 then
      L=L+sureln(1-cugauss(x,staerken(i),xsigma))
    else
      L=L+sureln((cugauss(x,staerken(i),xsigma)))
    end
  next
  return -L

End Function

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>