Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
"""
converts a power of ten to a power of two (or the reverse)
e.g. given x, 10^x = 2^y, solves for y
to convert 10^39 to a power of two: tentotwo.py 39
to convert 2^128 to a power of ten: tentotwo.py -r 128
https://tylerkerr.ca/b/2015/12/converting-powers-of-ten-to-powers-of-two
"""
import sys
from math import log
try:
if sys.argv[1] == "-r":
ptwo = int(sys.argv[2])
pten = log(2 ** ptwo) / log(10)
print("2 ^ %s = 10 ^ %s ≈ 10 ^ %s" % (ptwo, pten, round(pten)))
else:
pten = int(sys.argv[1])
ptwo = log(10 ** pten) / log(2)
print("10 ^ %s = 2 ^ %s ≈ 2 ^ %s" % (pten, ptwo, round(ptwo)))
except:
print("usage: %s [-r] [exponent]" % sys.argv[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.