Question

When using the following to compute PI in fortran77, will the compiler evaluate this value or will it be evaluated at run time?

PI=4.D0*DATAN(1.D0)

Was it helpful?

Solution

EDIT: depends on the compiler: see my EDIT below. EDIT END

i second Mick Sharpe's suggestion that it will be evaluated at runtime. just out of curiosity, i compiled PI=4.D0*DATAN(1.D0) with Silverfrost's ftn77 compiler and looked at the generated binary. the relevant part looks like so:

fld1                 ; push 1.D0 onto the FPU register stack
call    ATAN_X
fmul    dbl_404000   ; multiply by 4.D0

so indeed, no compiler cleverness here.

this of course might be different with another compiler (eg. g77). EDIT: apparently, with g77 (the fortran77 front-end for gcc) it is possible (and enabled by default) to use gcc's built-in atan function to auto-fold PI=4.D0*DATAN(1.D0) into a constant. EDIT END

OTHER TIPS

Calls to math functions are normally evaluated at run time. After all, there's nothing to stop you writing your own math functions. This would not be possible if they were evaluated at compile time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top