Sunday, June 26, 2016

A Pythonic ratio for pi (or, Py for pi :)

By Vasudev Ram


Pi image attribution

A Python built-in method can be used to find a ratio (of two integers) that equals the mathematical constant pi. [1]

This is how:
from __future__ import print_function
dir(0.0) # or dir(float)
gives (some lines truncated):
'__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 
'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
from which we see that as_integer_ratio is a method of float objects. (Floats are objects, so they can have methods.) So:
>>> import math

>>> tup = math.pi.as_integer_ratio()
>>> tup
(884279719003555, 281474976710656)

>>> tup[0] / tup[1]

>>> print(sys.version)
3.6.0a2 (v3.6.0a2:378893423552, Jun 14 2016, 01:21:40) [MSC v.1900 64 bit (AMD64
I was using Python 3.6 above. If you do this in Python 2.7, the "/" causes integer division (when used with integers). So you have to multiply by a float to cause float division to happen:
>>> print(sys.version)
2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)]

>>> tup[0] / tup[1]
>>> 1.0 * tup[0] / tup[1]
[1] There are many Wikipedia topics related to pi.
Also check out a few of my earlier math-related posts (including the one titled "Bhaskaracharya and the man who found zero" :)

The second post in the series on the uses of randomness will be posted in a couple of days - sorry for the delay.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes


Anonymous said...

RE: "to find a ratio (of two integers) that equals the mathematical constant pi"

Just to be pedantic, no, you can't find a ratio of two integers equal to pi, because pi is irrational and by definition unable to be expressed as a/b where a and b are integers. Proof of this has been known for quite some time:

What you have here are two integers a and b where a/b is a very good approximation of pi.

Vasudev Ram said...

@Anonymous: You are right. Thanks for pointing it out. Actually, I think I remembered that point - that pi (also e, and some other constants) are irrational numbers, but some time after publishing the post. Hey, it was taught in school, but that was some time ago ...

For those who might not know what irrational numbers are - informally, they are numbers that cannot be expressed exactly as the ratio of two other integers, no matter to how many decimals you calculate them. I.e. pi is an irrational number because its exact value cannot be expressed with a finite number of decimal digits, no matter how long. e.g. whether you use 3.14 or 3.14159 or many more digits, it will still be inexact (though close, and closer the more digits you use), because the true value of pi is a floating point number that starts with 3.14 and the remaining digits after the decimal point, go on infinitely. Same for the constant e (with a different value, of course, starting with 2.71828).

Another property of irrational numbers is that even the infinite sequence of their digits, does not consist of an infinite repetition of some finite repeating sequence. Because of that part of the definition, even though 1/3 equals 0.33333... (with an infinite number of digits 3 in the decimal part), it is not an irrational number, because it can be represented as the ratio of two other integers, i.e. 1 and 3.

Other interesting related topics:

More formal definition of irrational number than what I gave above (and pi is shown as the main example :)


Transcendental numbers: