Compute pi to base 12 using Python?

Dick Moores rdm at rcblue.com
Wed Apr 13 07:56:04 EDT 2005


Dan Bishop wrote at 04:07 4/13/2005:
>(3) A function for converting numbers to their base-12 representation.
>
>For integers, this can be done with:
>
>DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>def itoa(num, radix=10):
>    is_negative = False
>    if num < 0:
>       is_negative = True
>       num = -num
>    digits = []
>    while num >= radix:
>       num, last_digit = divmod(num, radix)
>       digits.append(DIGITS[last_digit])
>    digits.append(DIGITS[num])
>    if is_negative:
>       digits.append("-")
>    digits.reverse()
>    return ''.join(digits)

I see this works perfectly for integers. Thanks!

>For a floating-point number x, the representation with d "decimal"
>places count be found by taking the representation of int(round(x *
>radix ** d)) and inserting a "." d places from the right.

But I'm sorry, but I can't follow you. I do have the first 10000 or so 
places of pi base 10 (<http://mathwithmrherte.com/pi_digits.htm>), but 
could you show me what to do with, say, just 3.14159?

I apologize for being so dense.

Dick Moores
rdm at rcblue.com





More information about the Python-list mailing list