Converting code from C++ to FP....

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting code from C++ to FP....

Bo Berglund
I have a bunch of C++ files containing math processing functions,
which I need to convert to pascal.
But I have never programmed C++ (only ANSI C some 15 years ago) and I
am stuck because of the syntax differences. It is all about the
handling of data in arrays and I would be grateful for some hints on
how to convert it.

Below is an example function from this file set.

I have deduced that the variables named g<something> are globally
defined, but I don't know how to handle the following:

1) The second line in the loop contains the command std::max, how can
that be translated? I have not found any class definition for "std"
with a method "max"....

2) The parameter into the function is a pointer *A of type double. To
me that indicates a value of some kind, but in the code it suddenly
seems to appear as an array with indexing. What would be the proper
pascal translation of this?


void ForwardModel::Decompose(double *A)
{
  int i, j, k, jBegin, band;
  __int64 jPos, kPos;
  double sum;

  band = gNumNodeYZ;
  kPos = 0;
  for (k=1; k<=gNumNodes; ++k)
  {
      kPos += band;
      jBegin = std::max(k-band, 1);
      jPos = (jBegin-1) * band;
      for (j=jBegin; j<k; ++j)
      {
          jPos += band;
          sum = 0.0;
          for (i=jBegin; i<j; ++i) sum += A[kPos+i-1] * A[jPos+i-1];
          A[kPos+j-1] = (A[kPos+j-1] - sum) / A[jPos+j-1];
      }
      sum = 0.0;
      for (j=jBegin; j<k; ++j) sum += A[kPos+j-1] * A[kPos+j-1];
      A[kPos+k-1] = sqrt(A[kPos+k-1] - sum);
  }
}

Crudely translated:

procedure Decompose(var A: double); <<== Should it be an array here?
var
  i, j, k, jBegin, band: integer;
  jPos, kPos: Int64;
  sum: double;
begin
  band = gNumNodeYZ;
  kPos = 0;
  for k:=1 to gNumNodes do
  begin
    kPos := kPos + band;
    jBegin := std::max(k-band, 1); <<== what to do here?
    jPos := (jBegin-1) * band;
    for j := jBeginto k-1 do
    begin
      jPos := jPos + band;
      sum := 0.0;
      for i := jBegin to j-1 do
        sum := sum + A[kPos+i-1] * A[jPos+i-1]; <<== array????
      A[kPos+j-1] := (A[kPos+j-1] - sum) / A[jPos+j-1];  
    end
    sum := 0.0;
    for j := jBegin to k-1 do
      sum := sum + A[kPos+j-1] * A[kPos+j-1]; <<== array????
    A[kPos+k-1] := sqrt(A[kPos+k-1] - sum);
  end
end;


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Aleksa Todorovic-2
On Sat, Mar 19, 2011 at 06:07, Bo Berglund <[hidden email]> wrote:
>
> 1) The second line in the loop contains the command std::max, how can
> that be translated? I have not found any class definition for "std"
> with a method "max"....
>

There is function Max in unit Math, so you could say:

jBegin := Max(k-band, 1);

> 2) The parameter into the function is a pointer *A of type double. To
> me that indicates a value of some kind, but in the code it suddenly
> seems to appear as an array with indexing. What would be the proper
> pascal translation of this?

Indeed. C doesn't actually have difference between pointer to some
data and arrays. You could use open array:

procedure Decompose(var A: array of double);
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Jeppe Johansen-2
Den 19-03-2011 08:30, Aleksa Todorovic skrev:
>
>> 2) The parameter into the function is a pointer *A of type double. To
>> me that indicates a value of some kind, but in the code it suddenly
>> seems to appear as an array with indexing. What would be the proper
>> pascal translation of this?
> Indeed. C doesn't actually have difference between pointer to some
> data and arrays. You could use open array:
>
> procedure Decompose(var A: array of double);
Or you could use

procedure Decompose(A: pdouble);

That way it would be precisely the same as the C solution. You can
array-index A the same way as in C(in some fpc modes)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Ingemar Ragnemalm
In reply to this post by Bo Berglund
Jeppe Johansen <[hidden email]> wrote:

> Den 19-03-2011 08:30, Aleksa Todorovic skrev:
>  
>>> 2) The parameter into the function is a pointer *A of type double. To
>>> me that indicates a value of some kind, but in the code it suddenly
>>> seems to appear as an array with indexing. What would be the proper
>>> pascal translation of this?
>>>      
>> Indeed. C doesn't actually have difference between pointer to some
>> data and arrays. You could use open array:
>>
>> procedure Decompose(var A: array of double);
>>    
> Or you could use
>
> procedure Decompose(A: pdouble);
>
> That way it would be precisely the same as the C solution. You can
> array-index A the same way as in C(in some fpc modes)
>  

I would only do that when mixing C and Pascal code, to interface with C
code.
In a Pascal-only program, never. Converting bad C code (that means
almost any C code)
to good FPC code is a great thing to do, converting to bad FPC code is
not as much fun.


/Ingemar

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Bo Berglund
In reply to this post by Aleksa Todorovic-2
On Sat, 19 Mar 2011 08:30:45 +0100, Aleksa Todorovic
<[hidden email]> wrote:

>On Sat, Mar 19, 2011 at 06:07, Bo Berglund <[hidden email]> wrote:
>>
>> 1) The second line in the loop contains the command std::max, how can
>> that be translated? I have not found any class definition for "std"
>> with a method "max"....
>>
>
>There is function Max in unit Math, so you could say:
>
>jBegin := Max(k-band, 1);
>
>> 2) The parameter into the function is a pointer *A of type double. To
>> me that indicates a value of some kind, but in the code it suddenly
>> seems to appear as an array with indexing. What would be the proper
>> pascal translation of this?
>
>Indeed. C doesn't actually have difference between pointer to some
>data and arrays. You could use open array:
>
>procedure Decompose(var A: array of double);

Thanks for the information!
Now I have found another very strange construct:

void ForwardModel::SetMixedBoundaryCondition(const int iElec,
   const double* SX0,
   const double* SY0,
   const double* SZ0,
   double* SX,
   double* SY,
   double* SZ)

Now it seems like the variable type declaration itself is a
pointer....

Instead of double *Name it is double* Name. What is the difference?
It is still being used as an array:
          SX[n1] = SX0[n1] * (1.0 - w1);

Does C++ allow any placement of the * operator with the same meaning?

And what does const double* mean? Pointer to some object but not
allowed to change that object????

Having programmed Delphi for 16 years this is hard to wrap ones head
around. :-(


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Jürgen Hestermann
In reply to this post by Ingemar Ragnemalm


Ingemar Ragnemalm schrieb:
> Converting bad C code (that means almost any C code)
> to good FPC code is a great thing to do, converting to bad FPC code is
> not as much fun.
Yes, when already in progress of converting to Pascal then get rid of C
design flaws and convert to clear Pascal code.



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Aleksa Todorovic-2
In reply to this post by Bo Berglund
On Sat, Mar 19, 2011 at 13:03, Bo Berglund <[hidden email]> wrote:

>
> Now I have found another very strange construct:
>
> void ForwardModel::SetMixedBoundaryCondition(const int iElec,
>   const double* SX0,
>   const double* SY0,
>   const double* SZ0,
>   double* SX,
>   double* SY,
>   double* SZ)
>
> Now it seems like the variable type declaration itself is a
> pointer....
>
> Instead of double *Name it is double* Name. What is the difference?
> It is still being used as an array:
>          SX[n1] = SX0[n1] * (1.0 - w1);
>
> Does C++ allow any placement of the * operator with the same meaning?
>
> And what does const double* mean? Pointer to some object but not
> allowed to change that object????
>
> Having programmed Delphi for 16 years this is hard to wrap ones head
> around. :-(

Welcome to the world of Chell ;-) I (unfortunately) have to use it
daily. Ok, not let's be helpful.

In the situation above, const has similar meaning as the one in Pascal
- you have some value which you are not allowed to change. So, your
header should be something like:

procedure ForwardModel.SetMixedBoundaryCondition(const iElec :
Integer; const SX0, SY0, SZ0 : array of double; var SX, SY, SZ : array
of double);

Hint: if you use trunk version of FPC, I suggest you use constref to
ensure SX0, SY0 and SZ0 are passed as pointer.

procedure ForwardModel.SetMixedBoundaryCondition(const iElec :
Integer; constref SX0, SY0, SZ0 : array of double; var SX, SY, SZ :
array of double);

As Jeppe suggested, you can also use pointers:

procedure ForwardModel.SetMixedBoundaryCondition(const iElec :
Integer; const SX0, SY0, SZ0 : pdouble; SX, SY, SZ : pdouble);
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Bo Berglund
In reply to this post by Jürgen Hestermann
On Sat, 19 Mar 2011 13:10:25 +0100, Jürgen Hestermann
<[hidden email]> wrote:

>
>
>Ingemar Ragnemalm schrieb:
>> Converting bad C code (that means almost any C code)
>> to good FPC code is a great thing to do, converting to bad FPC code is
>> not as much fun.
>Yes, when already in progress of converting to Pascal then get rid of C
>design flaws and convert to clear Pascal code.
>

That is indeed my aim! :-)


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Jorge Aldo G. de F. Junior
no one explained so let me try :

> void ForwardModel::SetMixedBoundaryCondition(const int iElec,

void somefunction means its a pascal procedure

>   const double* SX0,

const has the same effect as fpc const before a parameter,

double* SX0
is the same as
double *SX0
or
double * SX0

so this becomes

const SX0 : pdouble (ugly pointer as array Cishism)

>   const double* SY0,

const SY0 : pdouble

>   const double* SZ0,

idem

>   double* SX,
>   double* SY,
>   double* SZ)

SX : pdouble;

etc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Bo Berglund
On Sat, 19 Mar 2011 11:30:47 -0300, "Jorge Aldo G. de F. Junior"
<[hidden email]> wrote:

>no one explained so let me try :
>
>> void ForwardModel::SetMixedBoundaryCondition(const int iElec,
>
>void somefunction means its a pascal procedure
>
>>   const double* SX0,
>
>const has the same effect as fpc const before a parameter,
>
>double* SX0
>is the same as
>double *SX0
>or
>double * SX0
>
>so this becomes
>
>const SX0 : pdouble (ugly pointer as array Cishism)
>
>>   const double* SY0,
>
>const SY0 : pdouble
>
>>   const double* SZ0,
>
>idem
>
>>   double* SX,
>>   double* SY,
>>   double* SZ)
>
>SX : pdouble;
>
>etc

Thanks! :-)


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Converting code from C++ to FP....

Micha Nelissen
In reply to this post by Jorge Aldo G. de F. Junior
Jorge Aldo G. de F. Junior wrote:
> const has the same effect as fpc const before a parameter,

This is not true.

> double* SX0
> is the same as
> double *SX0
> or
> double * SX0
>
> so this becomes
>
> const SX0 : pdouble (ugly pointer as array Cishism)

No! 'const double * X' in C makes the pointed-to data const, while
'const X: PDouble' in pascal makes the parameter itself const. You
cannot assign another address of a double to it, while you can in C.

In C terms, pascal can only do 'double * const X'. Pascal is not really
useful this way, 'const' is generally only useful for structures and
strings.

Micha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal