INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

I can't find the mistake

I can't find the mistake

(OP)
Hey guys, i were writing a code to calculate the digits of pi using the leibniz formula; the code is shown below:

CODE --> Fortran

program picalc
    implicit none
    real :: pi,g
    integer :: i,l
    pi=0
    print *, "Tell me the precision you want"
    read(*,*) l
    do i=0,l
        g = (1/(4*i+1))-(1/(4*i+3))
        pi=pi+g
    end do

   print *, 'The calculated value of pi is', 4*pi
end program picalc 
The thing is, the code can be compiled, but the 'g' expression inside the loop gives 1 in the first iteration, when it should be 0,6666666..., and zero in every other iteration (what clearly is wrong), not giving the value of pi. I want to know what i'm doing wrong and how can i fix it. Thanks guys!
Obs.:forgive my english, i dunno if i wrote something wrong, i really didn't check it in a translator.

RE: I can't find the mistake

As you already know, when programming computers, there is a difference on how integers are handle and how reals are handled...that is why you declare some variable one way and others a different way.

When dealing with integers, also known as integral numbers, the division is called "integral division". With integers, this operation yields only the whole number of the result and throws away fractions. Examples:

1/4 = 0
2/4 = 0
5/4 = 1
7/4 = 1
9/4 = 2

When dealing with reals, the result is what most (non-programming) people expect:

1.0/4.0 = 0.25
2.0/4.0 = 0.5

So, in your program above, just add decimal points to the numbers in the equation for g.

RE: I can't find the mistake

(OP)
Thank you so much salgerman, i really appreciate the help and the patience :D

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close