adambard / learnxinyminutes-docs

Code documentation written as code! How novel and totally my idea!
https://learnxinyminutes.com/
Other
11.53k stars 3.36k forks source link

Fortran: double precision pi not set corrrectly #5170

Closed Beliavsky closed 2 weeks ago

Beliavsky commented 2 weeks ago

The Fortran code has a line

real, parameter :: PI = 3.1415926535897931

that does not do what the user intends, for two reasons. First, real variables are single precision by default in Fortran. Second, a literal constant such as 3.1415926535897931 is interpreted as single precision unless you append "d0". So the results of the code

implicit none
real, parameter :: PI = 3.1415926535897931
double precision, parameter :: pi_d = 3.1415926535897931d0
print*,PI
print*,pi_d
end

are

   3.14159274    
   3.1415926535897931

So I suggest replacing the definition of pi currently used in your code with what I show above in the 3rd line. (You can rename pi_d to pi.) Or you can just write

real, parameter :: PI = 3.14159265

and avoid showing digits that will not be stored in single precision pi.