Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread Sagar Shankar
Hi Steven,

> Can I ask you to please post code using plain text rather than HTML
> email (also known as "rich text"), as HTML email messes up the
> formatting? In this case, I can reverse-engineer the correct formatting,
> so no harm done other than a waste of time, but that won't always be so
> easy.

Sure Steve, will do so from now on

>
> Three things stand out:
>
> (1) The *numerator* of each term is always 4.
>
> (2) The *denominator* of the terms are 1, 3, 5, ... or to put it another
> way, the term in position i has denominator 2*i+1 (starting with i=0).
>
> (3) The *sign* of the terms are +1, -1, +1, -1, +1, ... If you remember
> your high school maths, (-1)**0 = +1, (-1)**1 = -1, (-1)**2 = +1, etc.
> So the term in position i has denominator (-1)**i.
>
> Putting those three things together, the term in position i (starting
> with i=0) has value 4.0*(-1)**i/(2*i+1).
>
> Does that help?

It does! I wonder why this simplification never occurred to me. Will
implement this and try it out

> One other thing:
>
> > import math
> > seed = input("Please enter the number of pairs to calculate: ")
>
> In Python 2, you shouldn't use input() for user input. It was a design
> mistake, which has now been fixed in Python 3, but in Python 2 you
> should use raw_input() instead.
>
> The problem with input() is that it takes the user's input and *executes
> it as code*, which could have all sorts of bad side-effects starting
> with errors and just getting worse:
>
>  >>> name = input("What is your name? ")
> What is your name? Steven
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "", line 0, in ?
> NameError: name 'Steven' is not defined
>
> Instead, you should say this:
>
>
> seed = int(raw_input("How many terms would you like? "))
>

Absolutely. I was just following Zelle's book and he introduces
raw_input in the 4th chapter only. I will definitely use the raw_input
method, especially as using "input" alone trips up string input

Thanks a lot for the help Steve,

Sagar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread Steven D'Aprano

bob gailer wrote:

It is not crucial here - but you must recognize that your program uses floating 
point numbers, which  almost always are an approximation to the

"real" value.

For example (assuming decimal numbers):

 >>> 4/3.0
1. (followed by an unending number of 0's).


Actually no, it is not followed by an unending number of zeroes. That 
would imply that floats had infinite precision, but 4/3.0 was merely 
calculated inaccurately. That's not the case: floats have a finite 
precision, but are (or at least, should be) accurate to within the 
limitations of that precision.


The exact value for 4.0/3 is actually:

1.3332593184650249895639717578887939453125

which is the closest possible binary fraction to 4/3.



The "real" value of 4/3.0  is 1 followed by an unending number of 3's.

Each successive fraction's floating point value will be "off" by some relatively 
small value. Those errors will probably add up.


Another limitation of floating point numbers is that there is a maximum and a 
minimum exponent. Eventually the fractions will be too small to convert to 
float, raising an overflow exception.


I don't think you need to worry about that. It takes a pretty big 
denominator before overflow will occur: a googol won't do it:


>>> 4.0/10**100
4.0001e-100

Even though the series given is very slow to converge (300 terms is 
only accurate to 6 decimal places), I expect that it will converge 
before the terms overflow. It might take many hours or days of 
processing though.



Allof this raises the question - what computer algorithms successively 
approximate pi exactly?


Er, by definition you can't APPROXIMATE something EXACTLY. But see here 
for more approximations to π


http://en.wikipedia.org/wiki/Approximations_of_%CF%80



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread Dave Angel

On 10/17/2011 11:55 AM, Sagar Shankar wrote:



The "real" value of 4/3.0  is 1 followed by an unending number of 3's.

Each successive fraction's floating point value will be "off" by some
relatively small value. Those errors will probably add up.

Another limitation of floating point numbers is that there is a maximum and
a minimum exponent. Eventually the fractions will be too small to convert to
float, raising an overflow exception.

Allof this raises the question - what computer algorithms successively
approximate pi exactly?

--
Bob Gailer
919-636-4239
Chapel Hill NC




There are two separate problems with doing this operation in floats.  
One is that the final answer can be no closer than a float knows how to 
represent.  But the second is the cumulative error caused by adding a 
bunch of values of differing magnitudes.


You can at least reduce the second by adding them in reverse order. In 
other words, build a list of floats for the terms of the series, then 
reverse the list and sum it.


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread Steven D'Aprano

Sagar Shankar wrote:

Hi Bob,

I did use floating point numbers by using 4.0/1.0 - 4.0/3.0 etc., though I
did not declare it explicitly. Do you think it's better form/practice to
explicitly declare it?


Python doesn't use type declarations. 4.0 is already explicitly a float, 
there's no need to do anything else to make it a float.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread Sagar Shankar
Hi Bob,

I did use floating point numbers by using 4.0/1.0 - 4.0/3.0 etc., though I
did not declare it explicitly. Do you think it's better form/practice to
explicitly declare it?

Regards,
Sagar


On Mon, Oct 17, 2011 at 9:10 PM, bob gailer  wrote:

>  On 10/17/2011 6:31 AM, Sagar Shankar wrote:
>
> Hi, this is my first question to this group. I'm a beginner to computer
> science and programming in Python. Am currently using John Zelle's book -
> Python Programming: An introduction to computer science to teach myself.
>
>  In the book, there is an exercise to create a program that approximates
> the value of Pi by using the series [image: image.png]
>
>
> It is not crucial here - but you must recognize that your program uses
> floating point numbers, which  almost always are an approximation to the
> "real" value.
>
> For example (assuming decimal numbers):
>
> >>> 4/3.0
> 1. (followed by an unending number of 0's).
>
> The "real" value of 4/3.0  is 1 followed by an unending number of 3's.
>
> Each successive fraction's floating point value will be "off" by some
> relatively small value. Those errors will probably add up.
>
> Another limitation of floating point numbers is that there is a maximum and
> a minimum exponent. Eventually the fractions will be too small to convert to
> float, raising an overflow exception.
>
> Allof this raises the question - what computer algorithms successively
> approximate pi exactly?
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
>
<>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread bob gailer

  
  
On 10/17/2011 6:31 AM, Sagar Shankar wrote:
Hi, this is my first question to this group. I'm a
  beginner to computer science and programming in Python. Am
  currently using John Zelle's book - Python Programming: An
  introduction to computer science to teach myself.
  

  
  In the book, there is an exercise to create a program that
approximates the value of Pi by using the series 
  


It is not crucial here - but you must recognize that your program
uses floating point numbers, which  almost always are an
approximation to the
"real" value.

For example (assuming decimal numbers):

>>> 4/3.0
1. (followed by an unending number of 0's).

The "real" value of 4/3.0  is 1 followed by an unending number of
3's. 

Each successive fraction's floating point value will be "off" by
some relatively small value. Those errors will probably add up.

Another limitation of floating point numbers is that there is a
maximum and a minimum exponent. Eventually the fractions will be too
small to convert to float, raising an overflow exception.

Allof this raises the question - what computer algorithms
successively approximate pi exactly? 
-- 
Bob Gailer
919-636-4239
Chapel Hill NC
  

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way to estimate the value of Pi?

2011-10-17 Thread Steven D'Aprano

Sagar Shankar wrote:

Hi, this is my first question to this group. I'm a beginner to computer
science and programming in Python. Am currently using John Zelle's book -
Python Programming: An introduction to computer science to teach myself.


Hi Sagar, and welcome.

Can I ask you to please post code using plain text rather than HTML 
email (also known as "rich text"), as HTML email messes up the 
formatting? In this case, I can reverse-engineer the correct formatting, 
so no harm done other than a waste of time, but that won't always be so 
easy.




In the book, there is an exercise to create a program that approximates the
value of Pi by using the series [image: image.png]

Now, the only way I've been able to figure out how to do this is by creating
pairs of 4/x - 4/x+2 and calculating the series. The problem is that the
exercise asks to prompt the user for the number of terms to sum, and then
output the sum. So if an user inputs just 1, then his answer should be 4/1,
whereas in my version it will 4/1-4/3


The series looks like:

4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +- ...

Three things stand out:

(1) The *numerator* of each term is always 4.

(2) The *denominator* of the terms are 1, 3, 5, ... or to put it another 
way, the term in position i has denominator 2*i+1 (starting with i=0).


(3) The *sign* of the terms are +1, -1, +1, -1, +1, ... If you remember 
your high school maths, (-1)**0 = +1, (-1)**1 = -1, (-1)**2 = +1, etc. 
So the term in position i has denominator (-1)**i.


Putting those three things together, the term in position i (starting 
with i=0) has value 4.0*(-1)**i/(2*i+1).


Does that help?


One other thing:


import math
seed = input("Please enter the number of pairs to calculate: ")


In Python 2, you shouldn't use input() for user input. It was a design 
mistake, which has now been fixed in Python 3, but in Python 2 you 
should use raw_input() instead.


The problem with input() is that it takes the user's input and *executes 
it as code*, which could have all sorts of bad side-effects starting 
with errors and just getting worse:


>>> name = input("What is your name? ")
What is your name? Steven
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 0, in ?
NameError: name 'Steven' is not defined

Instead, you should say this:


seed = int(raw_input("How many terms would you like? "))



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor