Return to Snippet

Revision: 11323
at February 1, 2009 00:42 by gnrfan


Initial Code
def negabase(number, base):
    """ Calculates the decimal value of a number
        in a given negative base """
    hbase = 1
    sign = 1
    result = 0
    while number>0:
        digit = number % 10
        result += sign*hbase*digit
        number = int(number / 10)
        sign = -1*sign
        hbase *= base
    return result

if __name__ == '__main__':
    # See http://en.wikipedia.org/wiki/Negative_base
    print negabase(12243, 10) # 8163

Initial URL
negabase2decimal

Initial Description
A python function to convert a number from a negative-base system to decimal. Learn more about negative base non-standard positional numeral systems on the Wikipedia.

Initial Title
Convert a number from a negative base system to decimal in Python

Initial Tags

                                

Initial Language
Python