tags:

views:

924

answers:

4

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?

A: 

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.

Andrew McGregor
+6  A: 

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).

John Feminella
+20  A: 

This style ensures that the maximum precision available on ANY architecture is used when assigning a value to PI.

John Gietzen
Ah but of course, thanks!
ccook
But be warned if you follow this approach -- not all compilers and underlying maths libraries are equal in the results they return for trig functions of floating-point numbers, especially at critical points of those functions. There has recently been a long discussion of this on comp.lang.fortran where most of the Fortran experts hang out. Their conclusion -- specify the constant by pi = 3.14159...(enough digits for required precision then some for safety).
High Performance Mark
+6  A: 

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)
Jason