Fortran vs C

David G. Hough on validgh dgh at validgh.com
Fri Nov 30 01:06:06 AEST 1990


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.  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().
sqrt() won't work on long double arguments unless long double == double.

In Fortran, sqrt() is a generic intrinsic function, really an operator like +.  
Its semantics are known to the compiler.  The precision of the sqrt operation
can match that of the operand.

As mentioned by others, ANSI-C defines exception handling for the sqrt() function
and not for operators.  On high-performance implementations, error checking,
involving a conditional test and branch, can take as long as the sqrt
operation itself, even if performed inline.  System V implementations are
worse, requiring an external call to matherr(), although this will be 
removed in a future version of SVID.
-- 

David Hough

dgh at validgh.com		uunet!validgh!dgh	na.hough at na-net.stanford.edu



More information about the Comp.lang.c mailing list