Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

How to create PI number in Fortran

Status
Not open for further replies.

Teg-Men

Junior Member level 2
Joined
Sep 22, 2010
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Ankara
Activity points
1,468
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
Joined
Jan 22, 2008
Messages
47,478
Helped
14,047
Reputation
28,349
Reaction score
12,701
Trophy points
1,393
Location
Bochum, Germany
Activity points
276,115
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

Junior Member level 2
Joined
Sep 22, 2010
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Ankara
Activity points
1,468
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?"
READ(*,*) THETA
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
Joined
Jan 22, 2008
Messages
47,478
Helped
14,047
Reputation
28,349
Reaction score
12,701
Trophy points
1,393
Location
Bochum, Germany
Activity points
276,115
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.
 
Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top