tags:

views:

105

answers:

2

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)

+1  A: 

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.

Mick Sharpe
Good point, thank you.
ccook
+1  A: 

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

ax
wow, thanks. May I ask what you used to look at the binary?
ccook
i used IDA <http://www.hex-rays.com/idapro/ >
ax
Thank you for the link and update regarding g77.
ccook