patternpythonMinor
Converting float from arbitrary base to arbitrary base
Viewed 0 times
floatarbitraryconvertingfrombase
Problem
I needed to write a small program to convert floats from any base to any other base. The thing is, it got big. I know there are other ways to implement the conversions (such as doing subtractions instead of divisions), but isn't there a more simple way to do the whole process? I feel like I'm missing something. Any optimization tips are welcome as well.
```
import string
symbols = string.digits + string.uppercase
def _int_from_base(number, original_base):
return int(number, original_base)
def _int_to_base(number, new_base):
# Uses "the division method"
sign = -1 if number < 0 else 1
number *= sign
ans = ''
while number:
ans += symbols[number % new_base]
number //= new_base
if sign == -1:
ans += '-'
return ans[::-1]
def _fractional_from_base(number, original_base):
# The value of a symbol at position i after the decimal point is, by
# definition, the value of that symbol * b^-i
ans = 0
for i in xrange(1, len(number)+1):
ans += symbols.index(number[i-1]) * original_base**-i
return ans
def _fractional_to_base(number, new_base, precision=5):
# I don't know what this method is called
ans = ''
for i in xrange(precision):
tmp = number * new_base
itmp = int(tmp)
ans += str(symbols[itmp])
number = tmp - itmp
return ans
def convert(number, original_base, new_base, precision=None):
"""Converts any number from any base to any other base (2 <= base <= 36).
number should be a string representing a float in any base, e.g. '1.23'.
original_base, new_base should be integers representing the desired bases.
precision should be an integer representing how many digits after the
decimal point will be calculated on the conversion. Default is the same
number as the number of digits after the decimal point on number.
"""
try:
integer_part, fractional_part = number.split('.')
precision = len(fracti
```
import string
symbols = string.digits + string.uppercase
def _int_from_base(number, original_base):
return int(number, original_base)
def _int_to_base(number, new_base):
# Uses "the division method"
sign = -1 if number < 0 else 1
number *= sign
ans = ''
while number:
ans += symbols[number % new_base]
number //= new_base
if sign == -1:
ans += '-'
return ans[::-1]
def _fractional_from_base(number, original_base):
# The value of a symbol at position i after the decimal point is, by
# definition, the value of that symbol * b^-i
ans = 0
for i in xrange(1, len(number)+1):
ans += symbols.index(number[i-1]) * original_base**-i
return ans
def _fractional_to_base(number, new_base, precision=5):
# I don't know what this method is called
ans = ''
for i in xrange(precision):
tmp = number * new_base
itmp = int(tmp)
ans += str(symbols[itmp])
number = tmp - itmp
return ans
def convert(number, original_base, new_base, precision=None):
"""Converts any number from any base to any other base (2 <= base <= 36).
number should be a string representing a float in any base, e.g. '1.23'.
original_base, new_base should be integers representing the desired bases.
precision should be an integer representing how many digits after the
decimal point will be calculated on the conversion. Default is the same
number as the number of digits after the decimal point on number.
"""
try:
integer_part, fractional_part = number.split('.')
precision = len(fracti
Solution
It is not necessary to deal separately with the integral and fractional parts.
Here's how you can implement
- When converting from string, remove the point, convert as integer and scale according to the position where the point was.
- When converting to string, scale to integer, convert and insert point in the right place.
Here's how you can implement
convert with _int_to_base as the only helper function:def convert(number, original_base, new_base, precision=None):
#from original_base
integral, point, fractional = number.strip().partition('.')
num = int(integral + fractional, original_base) * original_base ** -len(fractional)
#to new_base
precision = len(fractional) if precision is None else precision
s = _int_to_base(int(round(num / new_base ** -precision)), new_base)
if precision:
return s[:-precision] + '.' + s[-precision:]
else:
return sCode Snippets
def convert(number, original_base, new_base, precision=None):
#from original_base
integral, point, fractional = number.strip().partition('.')
num = int(integral + fractional, original_base) * original_base ** -len(fractional)
#to new_base
precision = len(fractional) if precision is None else precision
s = _int_to_base(int(round(num / new_base ** -precision)), new_base)
if precision:
return s[:-precision] + '.' + s[-precision:]
else:
return sContext
StackExchange Code Review Q#44553, answer score: 2
Revisions (0)
No revisions yet.