Last active
July 3, 2017 13:42
-
-
Save olligobber/94b7033d785921f4cd72edb5c88a02b4 to your computer and use it in GitHub Desktop.
Converts a number to base pi, demonstrating its uselessness for most numbers, example usage at top of file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Command line use: | |
$ python3 base_pi.py -3 -1 0 2 3.141592653589793238462643383279502 -9.8696044010893586188344909998761511353136 4 0.5 0.333333333333333333333 | |
-3 | |
-1 | |
0 | |
2 | |
10 | |
-100 | |
10.220122021121110301 | |
0.11211202102012230001 | |
0.1001111101020123002 | |
$ python3 base_pi.py 10 | |
100.01022122221121122001 | |
Python use: | |
>>> base_pi(7) | |
'20.20211200210000003002' | |
>>> base_pi(-math.pi, trailing_zeroes=True) | |
'-10.00000000000000000000' | |
>>> base_pi(0.1, precision=40) | |
'0.00300301012122112202211222000010211123' | |
""" | |
import math | |
import sys | |
# Should be enough | |
math.pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744 | |
def base_pi(x, precision=20, trailing_zeroes=False): | |
# Assure input is valid | |
x = float(x) | |
precision = int(precision) | |
assert(precision >= 0) | |
trailing_zeroes = bool(trailing_zeroes) | |
# Compute sign and make x positive | |
sign = "" | |
if (x<0): | |
sign = "-" | |
x = -x | |
# Compute digits to left of point | |
left_digits = "" | |
if (x < 1): | |
# Numbers smaller than 1 should be 0.something | |
left_digits = "0" | |
else: | |
# Calculate the starting position | |
pos = int(math.log(x, math.pi)) | |
# For each position [floor(log_pi(x)), ..., 1, 0] | |
while (pos >= 0): | |
# What digit goes in this position | |
digit = int(x/(math.pi**pos)) | |
# Update x | |
x -= digit*(math.pi**pos) | |
# Update the left digits | |
left_digits += str(digit) | |
# Decrement pos to move right | |
pos -= 1 | |
# Compute digits to right of point | |
right_digits = "" | |
# For each position [-1, -2, ..., -precision] | |
for pos in range(-1,-precision-1,-1): | |
# What digit goes in this position | |
digit = int(x/(math.pi**pos)) | |
# Update x | |
x -= digit*(math.pi**pos) | |
# Update the right digits | |
right_digits += str(digit) | |
# Trim trailing zeroes | |
if not trailing_zeroes: | |
while len(right_digits) > 0: | |
if right_digits == '0': | |
right_digits = '' | |
break | |
if right_digits[-1] == '0': | |
right_digits = right_digits[:-1] | |
else: | |
break | |
# Format output | |
if (right_digits): | |
return sign+left_digits+"."+right_digits | |
else: | |
return sign+left_digits | |
if __name__ == "__main__" and len(sys.argv)>1: | |
for i in sys.argv[1:]: | |
print(base_pi(i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment