What is the motivation for defining PI as
PI=4.D0*DATAN(1.D0)
within a Fortran 77 code? I understand how it works, but, what is the reasoning?
What is the motivation for defining PI as
PI=4.D0*DATAN(1.D0)
within a Fortran 77 code? I understand how it works, but, what is the reasoning?
That sounds an awful lot like a work-around for a compiler bug. Or it could be that this particular program depends on that identity being exact, and so the programmer made it guaranteed.
It's because this is an exact way to compute pi
to arbitrary precision. You can simply continue executing the function to get greater and greater precision and stop at any point to have an approximation.
By contrast, specifying pi
as a constant provides you with exactly as much precision as was originally given, which may not be appropriate for highly scientific or mathematical applications (as Fortran is frequently used with).
This style ensures that the maximum precision available on ANY architecture is used when assigning a value to PI.
Because Fortran does not have a built-in constant for PI
. But rather than typing in the number manually and potentially making a mistake or not getting the maximum possible precision on the given implementation, letting the library calculate the result for you guarantees that neither of those downsides happen.
These are equivalent and you'll sometimes see them too:
PI=DACOS(-1.D0)
PI=2.D0*DASIN(1.D0)