Fortran vs C

Henry Spencer henry at zoo.toronto.edu
Sun Dec 2 09:37:00 AEST 1990


In article <219 at validgh.com> dgh at validgh.com (David G. Hough on validgh) writes:
>There have been recent statements that functions like sqrt() are
>equivalent in Fortran and C.   This is incorrect.
>
>In C, sqrt() is simply a function invocation.  It has no known
>semantics unless #include <math.h> has been seen, in which case
>ANSI-C knows that it is a double-precision function of a double-precision
>argument...

Unfortunately, this too is incorrect.  In the absence of #include <math.h>,
"sqrt" remains reserved for system use as an external name, and all bets
are off if a program uses it for that purpose itself.  (It can, however,
be used as an internal-only name, e.g. a static function.)  In the presence
of #include <math.h>, it is *not* known to be a double-precision function
of a double-precision argument; that is merely one of several possibilities.
What is known is that you can call it as if it were one.  It may well be
a macro whose internals invoke compiler-specific magic, rather than a
function invocation of any kind.

>Thanks to the default conversions, you can use sqrt on float
>arguments with correct results, but they may take twice as long to compute
>as necessary if you really meant sqrtf().

There is no programming language that can remove the programmer's ultimate
responsibility for efficient code.

>sqrt() won't work on long double arguments unless long double == double.

However, sqrtl() is reserved for future use for this purpose, and people
supplying a useful `long double' type would presumably supply one.

>In Fortran, sqrt() is a generic intrinsic function, really an operator like +.  

I don't recall this in the existing Fortran language (although I have not
used Fortran 77 very much and might have missed it).  If we're talking about
future languages, like Fortran 20xx (called Fortran 9x by some optimists),
the folks over in comp.lang.c++ can fill you in on a descendant of C, already
in widespread use and in the works for standardization, which permits such
things.

>As mentioned by others, ANSI-C defines exception handling for the sqrt()...

Now, *this* is a real issue, and one I'd forgotten about.
-- 
"The average pointer, statistically,    |Henry Spencer at U of Toronto Zoology
points somewhere in X." -Hugh Redelmeier| henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list