# Re: Pi.

• From: "Matthew Halfant" <halfant@xxxxxxx>
• Date: Tue, 05 Jul 2005 16:40:50 GMT

I agree with the approach -- compute pi to the requested precision rather
than depend on a stored constant. Ironically, acos(-1.0d0) only returns an
accurate value if it tests for an argument of -1.0d0 and returns a stored
value of pi. If it tries to compute it, the answer won't be better than
single-precision accuracy. For example, the code

program test
print *,acos(-1.d0 + epsilon(1.d0))
end program test

produces the value 3.14159263251637.

The problem arises because acos(x) has a vertical tangent at x = -1. For
this reason I use

pi = 4*atan(1.d0)

which at least CAN calculate an accurate answer without resorting to lookup.

"Michael Metcalf" <michaelmetcalf@xxxxxxxxxxxxxx> wrote in message
news:dae16u\$3cm\$03\$1@xxxxxxxxxxxxxxxxxxxx
>
> > You have to define it yourself, and it will be defined in whatever
> > precision you chose....
> >
> > e.g.
> >
> >
> > INTEGER, PARAMETER :: DP = SELECTED_REAL_KIND(P=11)
> > REAL (KIND=DP), PARAMETER :: pi =
> > 3.141592653589793238462643383279502884197_DP
> >
> This risks mistyping. You can get it to machine precision by:
>
> double precision pi
> pi = acos(-1.0d0)
>
> This has to be, however, a variable rather than a named constant (in
Fortran
> 95 but not Fortran 2003).
>
> Regards,
>
> Mike Metcalf
>
>

.

• Follow-Ups:
• References:

## Relevant Pages

• Re: SP on a 64-bit CPU and DP on a 32-bit CPU
... > better than doing the same process in a 32-bit CPU. ... > Fortran program below on an AMD XP CPU and an AMD64 CPU. ... > double precision:: a30,a31,a32,a33,a34 ... > end program test ...
(comp.lang.fortran)
• Re: calculation accuracy
... DOUBLE PRECISION:: PI_D ... END PROGRAM test ... in fortran is quite a bit different than what I get in excel. ...
(comp.lang.fortran)
• Re: increasing width
... Well, I don't know fortran 90, but a simple search on an on-line ... end program test ... declare variables of that precision. ... good luck ...
(comp.lang.fortran)
• SP on a 64-bit CPU and DP on a 32-bit CPU
... better than doing the same process in a 32-bit CPU. ... Fortran program below on an AMD XP CPU and an AMD64 CPU. ... double precision:: a30,a31,a32,a33,a34 ... end program test ...
(comp.lang.fortran)
• Object Oriented Optimization
... I have the following problem with re-using a fortran 77 optimization ... double precision, dimension, intent:: co ... several objectives and constraints. ...
(comp.lang.fortran)