At Albert M.'s job, he was recently tasked with implementing a C++ module to calculate the earth's magnetic field at any point. While I'm sure most of you have that formula memorized, Albert was feeling a bit rusty and consulted the Internet to help him find the code. He was in luck. The National Geophysical Data Center had exactly what he needed, and provided two different versions: one written in Fortran and the other in C.

From all appearances, the C version was generated using f2c, which is a fairly ancient Fortran-to-C converter. Albert figured he'd just start with the C code and modify it as needed. But after staring at it for a few seconds, he decided the wisest course was to brush up on Fortran and pretend he never saw the C code. First, here's the original Fortran code:

   INTEGER N, M, MAXORD
   ...
   MAXORD = ...
   ...
   DO 70 N=1,MAXORD
      DO 60 M=0,N
      ...
      60    CONTINUE
   70 CONTINUE

It's pretty typical Fortran stuff: two nested for-loops, with N ranging from 1 to MAXORD and M from 0 to N in increments of 1. As for the code that f2c generated:

  static int maxord, m, n, D3, D4;
  ...
  maxord = ...
  ...
  for (n=1; n<=maxord; n++)
  {
      for (m=0,D3=1,D4=(n+m+D3)/D3; D4>0; D4--,m+=D3)
      {
      ...
      }
  }

And yes, the lines shown above are the only places in the code where D3 and D4 are referenced. And of course, this is only one of many possible examples of WTf2c-ness. Starting with the Fortran code, Albert was able to get a working C++ implementation in short order. He had no doubt that, had he attempted to use the C code instead, he'd still be no closer to having anything work.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!