Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!unmvax!ariel.unm.edu!news From: john@ghostwheel.unm.edu (John Prentice) Newsgroups: comp.lang.fortran Subject: f2c experiences? Message-ID: <1990Nov25.204354.12324@ariel.unm.edu> Date: 25 Nov 90 20:43:54 GMT Sender: news@ariel.unm.edu (USENET News System) Organization: University of New Mexico Math Dept., Albuquerque, NM Lines: 162 There has been much discussion about using f2c to convert Fortran to C. Out of curiosity, I ran my previously posted complex root solver for quadratic equations through f2c. The Fortran code was: program root c c solve a*z**2 + b*z + c = 0 for complex a,b,c,z c implicit none real zero,one,two,three,four,five parameter (zero=0.0,one=1.0,two=2.0,three=3.0,four=4.0, * five=5.0) complex a,b,c,root1,root2,disc,sqrt1 c c hard-wire in a,b,c to make it simple c a=(one,zero) b=(-three,two) c=(five,-one) c c calculate the discriminant c disc=b**2-cmplx(four)*a*c c c calculate the upper half plane square root of the c discriminant c sqrt1=sqrt(disc) c c now calculate the roots c root1=(-b+sqrt1)/(cmplx(two)*a) root2=(-b-sqrt1)/(cmplx(two)*a) c c print out result c write (*,'('' the roots are: '',1p2e15.5/16x,1p2e15.5)') root1, * root2 c end and the resulting C code (via f2c) was: /* -- translated by f2c (version of 2 November 1990 13:43:50). You must link the resulting object file with the libraries: -lF77 -lI77 -lm -lc (in that order) */ #include "f2c.h" /* Table of constant values */ static integer c__2 = 2; /* Main program */ MAIN__() { /* System generated locals */ complex q__1, q__2, q__3, q__4; /* Builtin functions */ void pow_ci(), c_sqrt(), c_div(); integer s_wsfe(), do_fio(), e_wsfe(); /* Local variables */ static complex disc, a, b, c, root1, root2, sqrt1; /* Fortran I/O blocks */ static cilist io___8 = { 0, 6, 0, "(' the roots are: ',1p2e15.5/16x,1p2e\ 15.5)", 0 }; /* solve a*z**2 + b*z + c = 0 for complex a,b,c,z */ /* hard-wire in a,b,c to make it simple */ a.r = (float)1., a.i = (float)0.; b.r = (float)-3., b.i = (float)2.; c.r = (float)5., c.i = (float)-1.; /* calculate the discriminant */ pow_ci(&q__2, &b, &c__2); q__4.r = a.r * (float)4. - a.i * (float)0., q__4.i = a.i * (float)4. + a.r * (float)0.; q__3.r = q__4.r * c.r - q__4.i * c.i, q__3.i = q__4.r * c.i + q__4.i * c.r; q__1.r = q__2.r - q__3.r, q__1.i = q__2.i - q__3.i; disc.r = q__1.r, disc.i = q__1.i; /* calculate the upper half plane square root of the */ /* discriminant */ c_sqrt(&q__1, &disc); sqrt1.r = q__1.r, sqrt1.i = q__1.i; /* now calculate the roots */ q__3.r = -(doublereal)b.r, q__3.i = -(doublereal)b.i; q__2.r = q__3.r + sqrt1.r, q__2.i = q__3.i + sqrt1.i; q__4.r = a.r * (float)2. - a.i * (float)0., q__4.i = a.i * (float)2. + a.r * (float)0.; c_div(&q__1, &q__2, &q__4); root1.r = q__1.r, root1.i = q__1.i; q__3.r = -(doublereal)b.r, q__3.i = -(doublereal)b.i; q__2.r = q__3.r - sqrt1.r, q__2.i = q__3.i - sqrt1.i; q__4.r = a.r * (float)2. - a.i * (float)0., q__4.i = a.i * (float)2. + a.r * (float)0.; c_div(&q__1, &q__2, &q__4); root2.r = q__1.r, root2.i = q__1.i; /* print out result */ s_wsfe(&io___8); do_fio(&c__2, (char *)&root1, (ftnlen)sizeof(real)); do_fio(&c__2, (char *)&root2, (ftnlen)sizeof(real)); e_wsfe(); } /* MAIN__ */ /* Main program alias */ int root_ () { MAIN__ (); } #ifdef uNdEfInEd comments from the converter: (stderr from f2c) MAIN root: #endif Notwithstanding that the C code is not what I would regard as a model of clarity, it also will not execute on the Sun 3 I tried it on. It will compile and link, but it dies on a segmentation fault. I would be curious if other people would try it and see if it works on their machines. If this is typical of the reliability of f2c (assuming I didn't just make a dumb mistake somewhere), then I would say it is a ways from being a production tool for porting Fortran to C. That weakens the argument for using the huge Fortran math libraries with C numerical routines. I have had some success in the past using f2c to convert Fortran to C, though on one test it expanded a 50 line Fortran program to nearly 1000 lines (the code was a simple Fortran parser). I can accept that however given that the conversion of i/o routines is not that easy and C lacks most of the string handling ability of Fortran. What are other peoples experience with f2c? By the way, this note is not meant as a criticism of the people working on f2c. I think this is an excellant project and the need for a good Fortran to C converter is great. It is meant more to find out if we are anywhere near that goal and to suggest that it may be a bit premature to suggest abondoning Fortran for numerical work if you need the existing math libraries. John Prentice Amparo Corporation Albuquerque, NM john@unmfys.unm.edu