Page 1 of 1

C++ to PB

Posted: Sun Nov 19, 2006 10:26 pm
by stef
Some software I have auto-generates a C++ code snippet which I want to convert to PB . . . but with no experience of C I'm struggling to know what's going on. Can anyone offer me a solution?

It's basically for a formula I need to apply to a set of variables which I supply, and are identified as X1 thru X9. The C++ code is as follows;

#include <math.h>

double gModel(double d[]);

double gModel(double d[])
{
const double G1C0 = -4.308685;
const double G1C1 = -6.65509;
const double G2C0 = -5.364014;
const double G2C1 = -1.628174;
const double G3C0 = 7.809296;
const double G3C1 = 4.200164;
const double G4C0 = 4.932343;
const double G4C1 = 7.813782;

double dblTemp = 0.0;

dblTemp = sin(atan((d[1]*d[0])));
dblTemp += d[3];
dblTemp += (d[2]+((G3C1+G3C0)+d[2]));
dblTemp += d[3];

return dblTemp;
}

I've tried substituting the d[x] array(?) elements with my X variables and the G3Cx constants as declared, but the answer is not quite as expected.

The software also allows the code to be produced with or without "labels". Above is without labels. The 'with labels' version is as below;

#include <math.h>

double gModelHY(double d[]);

double gModelHY(double d[])
{
const double G1C0 = -4.308685;
const double G1C1 = -6.65509;
const double G2C0 = -5.364014;
const double G2C1 = -1.628174;
const double G3C0 = 7.809296;
const double G3C1 = 4.200164;
const double G4C0 = 4.932343;
const double G4C1 = 7.813782;

const int X1 = 0;
const int X2 = 1;
const int X3 = 2;
const int X4 = 3;
const int X5 = 4;
const int X6 = 5;
const int X7 = 6;
const int X8 = 7;
const int X9 = 8;

double dblTemp = 0.0;

dblTemp = sin(atan((d[X2]*d[X1])));
dblTemp += d[X4];
dblTemp += (d[X3]+((G3C1+G3C0)+d[X3]));
dblTemp += d[X4];

return dblTemp;
}


Anyone point me in the right direction?

Stef

PS
Software also outputs in other languages, but I'm not familiar with any of them.

Posted: Sun Nov 19, 2006 11:19 pm
by Clutch
Does this do what you're seeking?

Code: Select all

#G1C0 = -4.308685
#G1C1 = -6.65509
#G2C0 = -5.364014
#G2C1 = -1.628174
#G3C0 =  7.809296
#G3C1 =  4.200164
#G4C0 =  4.932343
#G4C1 =  7.813782
  
Enumeration
  #X1
  #X2
  #X3
  #X4
  #X5
  #X6
  #X7
  #X8
  #X9
EndEnumeration

Procedure.d gModel(d.d(1))
  Protected dblTemp.d
  
  dblTemp = Sin(ATan(d(1) * d(0)))
  dblTemp + (2 * d(3))
  dblTemp + (d(2) + ((#G3C1 + #G3C0) + d(2)))
  
  ProcedureReturn dblTemp
EndProcedure

Procedure.d gModelHY(d.d(1))
  Protected dblTemp.d
  
  dblTemp = Sin(ATan(d(#X2) * d(#X1)))
  dblTemp + (2 * d(#X4))
  dblTemp + (d(#X3) + ((#G3C1 + #G3C0) + d(#X3)))
  
  ProcedureReturn dblTemp
EndProcedure

;Test it
Dim doubleArray.d(10)

doubleArray(0) = 1.9
doubleArray(1) = 2.8
doubleArray(2) = 3.7
doubleArray(3) = 4.6
doubleArray(4) = 5.5

Debug gModel(doubleArray())
Debug gModelHY(doubleArray())

End
-EDIT-
Could also be reduced to this:

Code: Select all

#G1C0 = -4.308685
#G1C1 = -6.65509
#G2C0 = -5.364014
#G2C1 = -1.628174
#G3C0 =  7.809296
#G3C1 =  4.200164
#G4C0 =  4.932343
#G4C1 =  7.813782

Procedure.d gModel(d.d(1))
  ProcedureReturn Sin(ATan(d(1) * d(0))) + (2 * d(3)) + (2 * d(2)) + (#G3C1 + #G3C0)
EndProcedure

Posted: Mon Nov 20, 2006 12:04 am
by stef
Clutch,

Wow! Fast reply . . Thanks, but I need to examine your solution(s) a little closer to check out what's going on.

If I merely wanted to convert the code to say an Excel formula (to operate on variables in columns A-I) would that be easy enough? It would probably be easier for me to get my head around.

Many thanks

Stef

Posted: Mon Nov 20, 2006 1:10 am
by Clutch
Stef,
You mention using 9 variables, but it seems the functions in the C code you posted only operate on the first four. For the Excel version, assuming your variables (the first four, anyway) are in the first row, I suppose the formula roughly translates to:

Code: Select all

=SIN(ATAN(B1 * A1)) + (2 * C1) + (2 * D1) + 12.00946
The 12.00946 value is the sum of the constants G3C0 and G3C1. The algorithms in both C functions you posted take the sine of the arctangent of the product of the second and first variables. It then adds to that: the value of the fourth, the value of the third, the constant G3C1, the constant G3C0, the value of the third variable again, and finally the value of the fourth again. That all gets reduced into the Excel forumla above. I'm not sure where your fifth through ninth variables would come into play.

HTH
Clutch

Posted: Mon Nov 20, 2006 1:22 am
by stef
Hi Clutch,

Clutch wrote:Stef,
You mention using 9 variables, but it seems the functions in the C code you posted only operate on the first four. For the Excel version, assuming your variables (the first four, anyway) are in the first row, I suppose the formula roughly translates to:

Code: Select all

=SIN(ATAN(B1 * A1)) + (2 * C1) + (2 * D1) + 12.00946

9 variables yes, but the code may not be using all of them depending upon circumsatnces.
Anyway, the above is great, many thanks. Bedtime here now, so I'll check it out in more detail tomorrow.

Thanks agan,

Stef