473,382 Members | 1,441 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,382 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 5586
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.