# How to create PI number in Fortran

Status
Not open for further replies.

#### Teg-Men

##### Junior Member level 2 Hi, how can I create PI number in fortran?
I used REAL, PARAMETER :: PI = 3.14159265358979323846264338327950288419716939937510
statement and also this number that is obtained by Gregory–Leibniz series:
PI = 16*ATAN(1./5.) - 4*ATAN(1./239.)
I applied them into WRITE(*,*) TAN(PI*THETA/180) statement where Theta is input in degree
For 45 degree, both give me 1.00000
But for 90 degree they again both give -2.287733E+07
So, where is the problem, with PI? or Fortran?

#### FvM

##### Super Moderator
Staff member Re: PI number in fortran

The "problem" (I don't see an actual problem) is, that FORTRAN WRITE instruction has no explicite representation for INFINITY.

P.S.: I assume, that you are aware of resolution limits of floating point arithmetics? REAL is a single precision format, so it doesn't have more than 6-7 significant decimal digits.

Last edited:
• Teg-Men

### Teg-Men

points: 2

#### Teg-Men

##### Junior Member level 2 Re: PI number in fortran

I am new in Fortran. I have seen an exercise in a book (Fortran 95 2003 for Scientists and Engineers) and the problem states, we want to calculate tangent of an angle, but since the tangent is simply sin(x) / cos(x), if we have cos(x) < 10^(-20) give an error message, otherwise calculate it by using tan(x) function. Even if I enter 90 degree, since cos(x) < 10^(-20) it could give me error message. Here is the code:

PROGRAM EX3632
IMPLICIT NONE
! VARIABLES
REAL :: THETA
REAL, PARAMETER :: PI = 3.14159265358979323846264338327950288419716939937510
! EXECUTION
WRITE(*,*) "WRITE THE ANGLE IN DEGREE FORMAT TO CALCULATE"
WRITE(*,*) "TANGENT?"
IF(ABS( COS(PI*THETA/180) ) > 1E-20) THEN
WRITE(*,*) TAN(PI*THETA/180.)
WRITE(*,*) TAN((16*ATAN(1./5.) - 4*ATAN(1./239.))*THETA/180)
ELSE
WRITE(*,*) "ERROR, COS(x) IS TOO SMALL TO CALCULATE"
END IF
END PROGRAM EX3632

#### FvM

##### Super Moderator
Staff member Re: PI number in fortran

Even if I enter 90 degree, since cos(x) < 10^(-20) it could give me error message.
You didn't exactly tell, what you try to achieve. However, if you calculate cos(pi/2), the result won't be necessarily < 10**-20, because the argument is sufficient different from 90 degree. That's why I asked, if you aware of the limited resolution of floating point numbers. The problem isn't particularly related to FORTRAN.

It's good, to handle possible numeric under- or overflow in your code. But you shouldn't rely on it's occurrance with particular input numbers.

• Teg-Men

points: 2