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: Jeff Borckardt <jeffb at utkux dot utcc dot utk dot edu>
Date: Mon, 28 Mar 2005 12:55:32 -0500
Delivered-to: gettingstarted at lists dot realsoftware dot com
References: <9dff94d8b922cf4f92a18576ea611aba at utkux dot utcc dot utk dot edu> <383A8E96-9FAF-11D9-B597-000393C6C4C4 at hit dot net>
Yeah... I know it's sort of a vague situation...

In RB, I'm outputting doubles as strings [str(value)] to an editfield. The C-program writes to a console window.

The program is supposed to help a clinician essentially figure out how much electrical energy (0 to 100% of an electro-magnetic stimulator's output capability) supplied to a patient's head is necessary to get him to move his thumb (called the motor threshold). It is different for everyone and it can take a long time to assess it without the aid of maximum likelihood algorithms like the one in the program.

The problem is not nearly as subtle as I described. My RB app is outputting bizarre values... Both programs first run "20" through the functions and assumes it fails to evoke thumb movement ("0"). Then it runs "80" through the functions and assumes it evoked thumb movement ("1"). It then figures out the next intensity to be tested. The C program says "45". The RB version says "30". You then tell the program if that intensity caused the thumb to move (1/0). If you tell the C program "0", it suggests you try "62". If you tell the RB program that 30 resulted in no movement "0", it stops iterating and concludes that the motor threshold is "3013".

So it's pretty messed-up... I wasn't sure if log() was different in RB versus C (e.g., log in RB is natural, maybe it is base10 in C or whatever) or if exp() was different between languages?

Any thoughts, comments, questions, suggestions etc are welcome.

Thanks,
Jeff

On Mar 28, 2005, at 12:31 PM, CV wrote:

Jeff,

I think it would be helpful if you would describe how you are outputting your results in each case and provide in your post a comparison of the different values. Are you viewing the results in the debugger, or displaying output using Format() or String()?

Jack


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>