473,289 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Re: Why is math.pi slightly wrong?

Mensanator wrote:
On May 22, 11:32 am, "Dutton, Sam" <Sam.Dut...@itn.co.ukwrote:
>I've noticed that the value of math.pi -- just entering it at the interactive prompt -- is returned as 3.1415926535897931, whereas (as every pi-obsessive knows) the value is 3.1415926535897932... (Note the 2 at the end.)

Is this a precision issue, or from the underlying C, or something else? How is math.pi calculated?

If you actually need that many digits, use a different library.
>>>import gmpy
>>>print gmpy.pi(64) # 64 bit precision
3.14159265358979323846
>>>print gmpy.pi(128) # 128 bit precision
3.141592653589793238462643383279502884197
>>>print gmpy.pi(16384) # 16384 bit precision
3.141592653589793238462643383279502884197169399375 10582097494459
23....
Heh!
I wonder who needs that many digits?
Certainly not number theorists (they need a LOT more). Certainly not
physicists -- they need about 30 digits to be within 1% of any
measurement from molecules to galaxies. Certainly not engineers, they
need half the digits that physicists need. Cryptographers are making a
dire mistake if they are using PI in any computations (possible
exception for elliptic curves -- see number theorists, above) ... so --
other than PI-philes, who needs PI to thousands of digits?
Jun 27 '08 #1
1 5572
On May 23, 12:39*am, Andrew Lee <fiacre.patr...@gmail.comwrote:
Mensanator wrote:
On May 22, 11:32 am, "Dutton, Sam" <Sam.Dut...@itn.co.ukwrote:
I've noticed that the value of math.pi -- just entering it at the interactive prompt -- is returned as 3.1415926535897931, whereas (as every pi-obsessive knows) the value is 3.1415926535897932... (Note the 2 at the end.)
Is this a precision issue, or from the underlying C, or something else?How is math.pi calculated?
If you actually need that many digits, use a different library.
>>import gmpy
>>print gmpy.pi(64) # 64 bit precision
3.14159265358979323846
>>print gmpy.pi(128) # 128 bit precision
3.141592653589793238462643383279502884197
>>print gmpy.pi(16384) # 16384 bit precision
3.141592653589793238462643383279502884197169399375 10582097494459
23....

Heh!

I wonder who needs that many digits?
Pi digits specifically, or that many digits
in general? I generally don't use pi, but
I routinely work with numbers having that
many digits.
>
Certainly not number theorists (they need a LOT more).
That depends on the problem. What's important
to the number theorist is not how many digits
there are, but the fact that every single one
of them is significant. If pi is needed, the
mantissa must have as many digits as the number
to avoid loss of significance.

For example, in the Collatz Conjecture, there
is a nice closed form equation to tell you the
ith, kth Generation Type [1,2] Mersenne Hailstone:

a(i,k) = 2**(6*((i-1)*9**(k-1)+(9**(k-1)-1)/2+1)-1)-1

Which for a modest request like a(1,6) returns
a number with 53,338 decimal digits. And you
can't even compute the 11th generation without
getting an "Outrageous Exponent" exception
(the exponent exceeded 32 bits).

Now, it's true you can't count up to such a
number, but you can readily determine its Collatz
Sequence as they are logarithmic (for a(1,6) the
sequence contains 854,697 odd numbers and about
twice that many even numbers).

And since a(1,6) has only 177,149 bits, it's
easy to verify that it is, indeed, the first
of the infinite members of the 6th generation.
Certainly not
physicists -- they need about 30 digits to be within 1% of any
measurement from molecules to galaxies. *Certainly not engineers, they
need half the digits that physicists need. *Cryptographers are making a
dire mistake if they are using PI in any computations (possible
exception for elliptic curves -- see number theorists, above) ... so --
other than PI-philes, who needs PI to thousands of digits?- Hide quoted text -

- Show quoted text -
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Craig Bailey | last post by:
Someone please explain what alternate universe I fell into this afternoon when PHP started telling me that 2 doesn't equal 2. Not sure about you, but when I run this, it tells me 59001.31 doesn't...
4
by: Typpo | last post by:
Hi, Maybe I'm missing something: <begin code> MessageBox.Show("a = " + a + ", b = " + c + ", c = " + c); //message 1 double top = (Math.Pow(a, 2) + Math.Pow(b, 2) - Math.Pow(c, 2)); double...
2
by: mbelew | last post by:
I'm seeing a very strange behavior with double precision subtraction. I'm using csUnit for testing. If I run the test by itself, the test passes. When I run the batch of tests, the test fails. ...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
1
by: William Stacey [MVP] | last post by:
int sqrft = 1335; decimal pricePerSqr = 18.33m; decimal pricePerYear = Math.Round(pricePerSqr * sqrft, 2, MidpointRounding.AwayFromZero); decimal pricePerMonth = Math.Round(pricePerYear / 12, 2,...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
5
by: Nondisclosure007 | last post by:
Hello. If this is the wrong group for this, please let me know. I'll post it somewhere else. I've been doing data imports into MS Excel (ver 2007) and using the CORREL function. What I was...
5
by: aguirre.adolfo | last post by:
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve...
0
by: Mark Dickinson | last post by:
On May 22, 12:32 pm, "Dutton, Sam" <Sam.Dut...@itn.co.ukwrote: There are two effects here: (1) pi isn't exactly representable as a floating-point number, so math.pi is merely an approximation...
17
by: Lenni | last post by:
Hi, I'm currently writing a web application for bicycle wheel builders that calculate the spoke length for all sorts of variations of hubs and rims. I have translated the formula from an...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.