Welcome, guest | Sign In | My Account | Store | Cart

This shows how to find two integer values whose ratio approximates the mathematical constant, pi, somewhat accurately.

Python, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from __future__ import print_function
import math

ratio = math.pi.as_integer_ratio()

# It can be verified with:

print(ratio[0] / ratio[1]) # for Python 3.x

# or

print(1.0 * ratio[0] / ratio[1]) # for Python 2.7x

# It gives:
# 3.141592653589793
# which is the value of pi to some decimal places.

1 comment

Created by Vasudev Ram on Sun, 26 Jun 2016 (BSD)
Python recipes (4591)
Vasudev Ram's recipes (93)

Required Modules

  • (none specified)

Other Information and Tasks