• 28% genius (unregistered) in reply to Randy
    Randy:
    I once took a large library (JPL's CSPICE) that had been translated from FORTRAN to C and wrapped in it JNI so we could use it in Java programs.

    The real WTF was I used perl to generate it all.

    Well, it worked, didn't it. Of course it worked, it's perl. Ugly but usefull.

  • 28% genius (unregistered)

    I once had to translate a geophysics program from Qbasic to Turbo Pascal. The QBasic program was a translation from FORTRAN and was littered with labels that were named after the original numerical labels in FORTRAN.

    CAPTCHA: tastey -- no, definitely not.

  • niin (unregistered) in reply to gomer

    The

    m=0,D3=1,D4=xxx

    part is only run once at the start of that inner loop (and as a result, run once per iteration of the outer loop), so m is always equal to zero, and D3 is always equal to 1.

    n is the only thing changing in that assignment.

  • Simmo (unregistered) in reply to Phlip
    Phlip:
    Firethorn:
    I mean, if you take a novel, translate it from English to French, then propose creating a new German translation, which would you pick?
    Nice... Though, a better analogy would be if the novel was translated into Mandarin and you wanted a Cantonese version... but the original translation was done through Babelfish.
    Hang on, I thought Chinese writing was language-neutral, ie. means the same in every Chinese language, much like mathematical notation means the same in every language?
  • csrster (unregistered)

    You guys should see the byte code my java compiler generates - total spaghetti, big ball of mud.

  • david (unregistered) in reply to chrismcb
    chrismcb:
    I don't understand why these are different:

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

    How did the translator decide it knew how to handle the first loop specifically, but had to handle the second one generically?

    The inside code isn't show, but presumably n is referenced inside the loop. So the translator decided that it was not safe to use in the completion test. That is, that it's value might change, giving a different result than the FORTRAN loop.

    As I recall, it used to be common for compilers to not differentiate between left-side references and right-side references, so it wouldn't suprise me that a mere translator might use a simple test.

  • Kris Nuttycombe (unregistered)

    I worked at NGDC for 5 years, and during that time did a complete rewrite of that geomagnetic modeling code from FORTRAN to C.

    Apparently, my rewrite has either been lost by the bureaucracy, or has just never been put online, because that horrhendous F2C stuff is the same thing that they had online when I started working there in 2002.

    Or, I guess, it's possible that the original submitter never found it. NGDC's web presence is a horrhendously disorganized mishmash of ancient dead-end links that have long been forgotten.

    By the way, that original FORTRAN code contains some fairly severe bugs that were never found until the code was rewritten. NGDC now employs a talented geophysicist who's responsible for maintaining the codebase (in C) named Stefan Maus. If the original submitter wants the best, debugged, up-to-date stuff I'd recommend talking to him.

  • Kris Nuttycombe (unregistered)

    Oh, and one more thing... there's also JNI code for the rewritten library available. The web interface for this code is at http://www.ngdc.noaa.gov/seg/geomag/magfield.shtml and all runs atop those JNI interfaces.

  • (cs)

    Some code where I work is alive and well thanks to f2c. It is still in FORTRAN but the gcc FORTRAN compiler was buggy. So to keep it going it was all run through f2c and it works rather well. Yes the code is horrible but nobody cares as it is the FORTRAN that is maintained.

  • miked (unregistered) in reply to dkf
    dkf:
    diaphanein:
    My favorite WTF in this code is the use of static locals unnecessarily. Hope you're not planning on multithreading anytime soon...
    That's the part that makes me cringe too, especially as it also goes a long way towards reducing the C compiler's ability to optimize the mess away.

    Yeah, but the problem is, that in FORTRAN the default is all local variables are equivalent to a static variable in C. F2C can't know if you're taking advantage of the staticness or not, so it has to make them static.

  • miked (unregistered) in reply to Anon
    Anon:
    pitchingchris:
    Anon:
    I think the point of the WTF isn't that the generated code is ugly and complicated, it's that the National Geophysical Data Center thought it was worth posting on the web at all. If they don't have a decently written C version, then don't post a crap one instead.

    Since a lot of scientific stuff is still done in fortran, then they didn't do anything wrong.

    I didn't say anything was wrong with Fortran. Please pay attention. The wtf wasn't posting the Fortran version, it was posting a bad C version. If they can't do a half way decent job of converting it to C, they should have only posted the Fortran version.

    But what if a scientist NEEDS a c function to do this calculation. Say, he has thousands of lines of c code and it would be a real pain to link in a FORTRAN function. It doesn't matter if the c function is pretty, just as long as it's right. I see this all the time. Many scientists program only because the need to, not because they like to, so they don't care about stuff that we care about. They're perfectly happy to copy and paste that ugly c function into their source code and continue on with their work.

  • UnitTestingIsNotACrime (unregistered) in reply to ha
    ha:
    In order to write unit tests you need a device to measure magnetic field. That's why unit tests are useless in math's and physic's scientific programs.

    In order to unit test you need a unit to test, you don't need a spare planet, you are testing that your algorithm gives the expected result for an expected input set.

    In this case you would write your code to address an interface for the "MagneticFieldMeasurisator" which you mock with a set of expected values and then assert a corresponding set of results.

    Surely people involved in maths and physics can grasp the concept of simulation, some of them must be married.

  • pyro (unregistered) in reply to Phlip
    As to the WTF itself... I think the code is a pretty decent way of coding a "iterate from A to B, stepping by C" loop, where A, B and C could all be expressions, any of them could change during the loop, and the loop must count based on what the expressions were at the loop's start. D3 here stores the increment, and D4 stores the number of times to loop. D1 and D2 were probably used earlier for a similar purpose.

    The real real WTF the National Geophysical Data Center passing off code generated by f2c as C source. f2c is a "hail mary" for people who don't have a real FORTRAN compiler for their platform, not a universal translator.

    As such, it only cares if the final compiled binary runs correctly as a unit, not if it's readable, efficient, or if an arbitrary chunk cut/pasted out of it works at all.

  • bull (unregistered) in reply to djork

    So coooool! I lover pointers. Gimme the link, I will use this code later.

  • bull (unregistered) in reply to djork
    djork:
    The real WTF is that Alex didn't post some of the wrost code from that app, like these lines:
    *(p+n+m*13) = ct**(p+n-1+m*13); dp[m][n] = ct*dp[m][n-1]-st**(p+n-1+m*13); goto S50;

    So coooool! I love pointers. I hate arrays. Gimme the link, I will use this code later.

    Btw, please delete my previous post( and please don't create a wtf entries about me)

  • zzo38 (unregistered) in reply to Simmo
    Simmo:
    Hang on, I thought Chinese writing was language-neutral, ie. means the same in every Chinese language, much like mathematical notation means the same in every language?
    I think so, I think ideographs are same with Mandarin, Cantonese, but they speak differently. They also have traditional Chinese and national standard Chinese formats of writing (but I prefer to play Chinese Chess with traditional Chinese, because it is closer to the Japanese kanji).
  • ELIZA (unregistered) in reply to UnitTestingIsNotACrime
    UnitTestingIsNotACrime:
    ha:
    In order to write unit tests you need a device to measure magnetic field. That's why unit tests are useless in math's and physic's scientific programs.

    In order to unit test you need a unit to test, you don't need a spare planet, you are testing that your algorithm gives the expected result for an expected input set.

    In this case you would write your code to address an interface for the "MagneticFieldMeasurisator" which you mock with a set of expected values and then assert a corresponding set of results.

    Surely people involved in maths and physics can grasp the concept of simulation, some of them must be married.

    [sarcasm] Ohhhhh, you mean a rehearsal. [/sarcasm] Seriously, mathematicians generally focus on proving that the math is valid, though it is a common practice to run through an example to help one understand how an algorithm works. Scientists in my understanding generally treat serious projects like wedding: Apollos 8 through 10 were rehearsals of Apollos 11-20; they rehearsed the repair of the HST literally hundreds of times in sims before doing it IRL; in fact, it is apparently the norm to work out the same results by several different means in order to rule out systematic error and I am sure it carries over into programming (systematic errors in this case are generally caused by misimplementation of the algorithm) where one would likely check several different results against previous algoritha and implementations of the algorithm and against results obtained by use of the theory directly.

Leave a comment on “WTf2c”

Log In or post as a guest

Replying to comment #:

« Return to Article