문제

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)

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top