- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am using Fortran90. I would like to declare 'pi' as a double precision parameter.
How can I express 'pi' value in double precision,and how many decimal points should I consider in this case?
Can anybody help me in this regard?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm sure there are other ways of defining pi, but I always use atan, which is an intrinsic function that has been available in all Fortrans.
The use of atan in a parameter statement is a recent version of fortran.
This should give some idea of the precision available.
real(8), parameter :: PI_8 = 4 * atan (1.0_8)
real(16), parameter :: PI_16 = 4 * atan (1.0_16)
write (*,*) pi_8
write (*,*) pi_16
end
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, John!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello
pi_8=acos(-1.0_8) works as well and you are sure that cos(pi_8)=-1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I made a test program to see how good "4 * acos(1.0_8)" really is and was surprised it gave the same answer as the most accurate value I could find.
[fortran]
program testPi
real(8), parameter :: piCalculated = 4 * atan(1.0_8)
real(8), parameter :: piAccurate = 3.1415926535897932_8
print *, piCalculated, piAccurate, piCalculated - piAccurate
print *, cos(piCalculated) + 1.0_8, cos(piAccurate) + 1.0_8
end program
[/fortran]
Results are:
3.14159265358979 3.14159265358979 0.000000000000000E+000
0.000000000000000E+000 0.000000000000000E+000
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Andrew,
The answer should be that accurate, as your piAccurate is truncated to real*8 storage. If it wasn't we'd be asking more questions.
As I don't use real*16 very often, I wonder and am amazed at the accuracy of these calculations, as I can never provide input data of sufficient accuracy.
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, Andrew & John once again. However, I have declared all variables as double precision, so I want to declare PI as a double precision parameter. Can I declare DOUBLE PRECISION, PARAMETER::pi=4.0D0*DATAN(1.0D) instead of REAL
(8),
PARAMETER
:: pi = 4 *
ATAN
(1.0_8)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, for making a mistake. I wanted to write DOUBLE PRECISION, PARAMETER::pi=4.0D0*DATAN(1.0D0), where D means double precision.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I prefer "4 * ATAN (1.0d0)" as
ATAN as I prefer to use the generic name rather than DATAN. (DATAN indicates the code is probably Fortran 77)
4.0d0 is not required as ATAN implies a real*8 calculation and 4 converts to 4.0d0 with no loss of accuracy. 4 is easier to read.
Although it is non portable, for me, 1.0_8 does document the precision more clearly than 1.0d0. (1.0e0 is very different but has a similar interpretation outside Fortran)
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, John!
![](/skins/images/2E08A100FB92911314A240D1EAFB2828/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page