/ Published in: Python
                    
                                        
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.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
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
URL: negabase2decimal
Comments
 Subscribe to comments
                    Subscribe to comments
                
                