[fpc-pascal] Converting code from C++ to FP....

Bo Berglund bo.berglund at gmail.com
Sat Mar 19 06:07:15 CET 2011


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




More information about the fpc-pascal mailing list