computing pi and a line of code

This is the place for queries that don't fit in any of the other categories.

computing pi and a line of code

Postby lorentz » Mon May 27, 2013 4:59 pm

Hello,

i'm very new to programming and the python language, so i'm glad to have found a place I can post my questions. I have been following the "learn python the hard way" tutorial online and have found it very helpful in terms of understanding how to structure code and to do some pretty cool things.

Something that I have decided to do in order to expand what I know is to take ideas and see if I can hash them out in code. Making a tic tac toe game for example. Something that I am currently doing is trying to improve upon a piece of code that computes pi. It works by using the taylor series of arctan where pi = 4(1/1 - 1/3 + 1/5 - 1/7 ...) This converges very slowly and a range of (0, 500000) gives me about 4 correct decimal places.

Code: Select all
total = 0
sign = 1
for n in xrange(0, 500000):
   t = sign / (n*2 + 1.0)
   total = total + t
   sign = sign * -1
   print total, t, sign
print total * 4


This is the code i'm using. I had some help from my stepdad in putting it together, but I understand the math behind it and can (almost) make sense of each line as I read it through. The part that is bugging me is this;
Code: Select all
print total, t, sign


If someone could explain the significance of that line, that would really help me out! My goal is to write a similar piece of code based on the arctan of x, where x = tan(pi/8). It should converge much faster.

Lorentz.
Last edited by stranac on Mon May 27, 2013 5:01 pm, edited 1 time in total.
Reason: Added code tags
lorentz
 
Posts: 9
Joined: Mon May 27, 2013 4:46 pm

Re: computing pi and a line of code

Postby stranac » Mon May 27, 2013 5:05 pm

lorentz wrote:The part that is bugging me is this;
Code: Select all
print total, t, sign


I'm not sure what part of this you don't understand.
This code does exactly what it looks like - it prints the values of total, t and sign.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 894
Joined: Thu Feb 07, 2013 3:42 pm

Re: computing pi and a line of code

Postby lorentz » Mon May 27, 2013 5:13 pm

Thanks for your reply, I can see that it prints these values when I run the program, but what is confusing me is the need to do so. I've been told that there's lots of convention used in programming languages and was wondering if having a running total of these values is useful in some way or can I leave the whole line out?

I would have thought keeping everything simple is a good thing. I'm probably over-analysing it, but since I don't know much yet I don't want to get into bad habits.
lorentz
 
Posts: 9
Joined: Mon May 27, 2013 4:46 pm

Re: computing pi and a line of code

Postby stranac » Mon May 27, 2013 5:30 pm

Oh, I see.

As you say, if you don't want the values displayed, there is no need for that line.
print doesn't change anything about the execution of the rest of the code, it simply displays some values.
At least if the code is not insane, that is.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 894
Joined: Thu Feb 07, 2013 3:42 pm

Re: computing pi and a line of code

Postby lorentz » Mon May 27, 2013 5:41 pm

Sometimes I have to run my thoughts by people to get them to make sense in my head...

I suppose displaying the list of values is useful for being able to look at where you are in the algorithm and whether the sign is +ve or -ve.

Anyway, thank you.
lorentz
 
Posts: 9
Joined: Mon May 27, 2013 4:46 pm

Re: computing pi and a line of code

Postby Mekire » Tue May 28, 2013 1:42 am

Also you should be aware that, especially in a computation intensive algorithm, print is slow slow slow (all the more so if you aren't formatting your print statement properly).

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests