|
|
|||||
|
|
|||||
|
|
Getting Started and Having Fun with GMPY
OverviewGMPY is the General Multiprecision Python project, which is a set of Pythonwrappers to the Gnu Multiple Precision (GMP)arithmetic library. This document gives a casual introduction to GMPY, since it's one of my favorite Python modules, and is intended for people who want to investigate some of the features that the module makes available. If you are a serious mathematician you will probably provide the discussion herein lacking, and should probably refer to the GMP documentation.
What is multiprecision arithmetic?To be completed.
What types and functions does GMPY provide?There are three basic types that GMPY provides:
The examples described here will use these types with a few other functions, including
Examples of GMPY use
Computing piThere are numerous methods of computing the number pi; Googling "compute pi" will bring up a large number of sites. But there are many very simple methods forcomputing pi that can do so with surprising accuracy.
GMPY has a builtin method for computing pi. The documentation shows that pi(n): returns pi with n bits of precision in an mpf objectso we can, for example, call >>> pi(100)and obtain a fairly good approximation to pi. Using a higher precision, of course, gives more digits. The GMPY pi function is the best way to get a large number of digits for pi.
However, there are also a number of simple methods for computing pi, and, if you're interested in understanding how these constants are computed, GMPY can give you a nice set of tools for exploring thesetechniques.
The simplest algorithm for computing pi is from gmpy import * The problem with this algorithm is that, unless you use a ridiculous number of points, you would be just as well off using normal Python floats, ints, and random numbers.
Another much, much better method of computing pi relies on Ramanujan's sequence
from gmpy import * def pi_rama(npts=100,prec=1000): Given enough steps and a high enough precision, this function will match the GMPY version of pi digit for digit.
Computing eWe can compute e using the Taylor seriesfrom gmpy import *which will again give a large number of digits of e. |