Rohit Sthapit
Rohit Sthapit

Reputation: 85

Calculating Sin(x) using Fortran

      integer   n
  real term , sum , deg
  write(*,*) 'Enter Degree'
  read(*,*) deg
  deg = deg *  3.14 /180
  n =  3
  term = deg
  sum = 0
2     if ( abs(term)  .gt. 0.000001)  then !<<<<<<<<<<< THIS CONDITION
      goto 1
   else
      goto 3
    endif
 1        sum = sum + term
     write( *,*) 'Your', n - 2, ' Term is ' , term
     term = term *(( deg ** 2)/ (n *( n - 1)))  * (-1)
     n = n + 2
     goto 2
3      write(*,*) ' YOur final sum ' , sum
   pause
   end

I found this program for the calculating Sin(x) It is clear the The value of sin(x) is entered by User by I didn't get the whole point of condition ( abs(term) .gt. 0.000001) Does this mean that the computer can't be more precise than this. correct me if I am wrong

Upvotes: 0

Views: 7160

Answers (2)

SethMMorton
SethMMorton

Reputation: 48745

The condition if ( abs(term) .gt. 0.000001) is a way of testing that the term is non-zero. With integers, you would just use if (term .ne. 0), but for real numbers it might not be represented as identically zero internally. if ( abs(term) .gt. 0.000001) filters numbers that are non-zero within the precision of the real number.

Upvotes: 0

This program uses default real variables. They usually allow to precision of approx. 6 digits. You can use the so called double precision which can allow more. Below you see example for 15 digits.

integer,parameter :: dp = selected_real_kind(p=15,r=200)
real(dp) ::  term , sum , deg

deg = deg *  3.14_dp /180

and so on...

See:

http://gcc.gnu.org/onlinedocs/gfortran/SELECTED_005fREAL_005fKIND.html

http://gcc.gnu.org/onlinedocs/gfortran/ISO_005fFORTRAN_005fENV.html (especially real64)

In old programs you can also see

double precision x

which is obsolete, or

real*8 x

which is nonstandard.

Upvotes: 2

Related Questions