从二进制到 base 48 转换
From binary to base 48 conversion
我创建了一个将二进制作为字符串接收然后将其转换为 base48 的函数,它适用于大多数测试用例,但是当我通过它运行“1010000001101101011000000100000001000101111010000101101010110000001100110”时
import math
def Binary2Octoquadragesimal(n):
octdict = {"0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","10":"a","11":"b","12":"c","13":"d","14":"e","15":"f","16":"g","17":"h","18":"i","19":"j","20":"k","21":"l","22":"m","23":"n","24":"o","25":"p","26":"q","27":"r","28":"s","29":"t","30":"u","31":"v","32":"w","33":"x","34":"y","35":"z","36":"A","37":"B","38":"C","39":"D","40":"E","41":"F","42":"G","43":"H","44":"I","45":"J","46":"K","47":"L",}
ans = []
if n == "":
return ""
i = int(n,2)
if i == 0:
return "0"
while i > 0:
ans.append(octdict[str(i%48)])
i = math.floor(i/48)
ans.reverse()
print (ans)
return ("".join(ans))
它返回错误的值,我仔细查看了我的代码,似乎在这个特定的测试用例中我的函数 return Dragonfly00w6 而不是 Dragonfly2026 为什么这只发生在这种情况下而不是其他的?我该如何补救
您的问题是浮点数不准确。这个数字太大了,无法用 python 浮点数表示。整数之所以有效,是因为 python 整数是任意精度的。
经过几次迭代后,问题自行解决,因为错误只发生在其余部分,除法隐藏了问题。
如果你在每次迭代中都做print(f"{i/48}, {math.floor(i/48)}, {i//48}")
,你会看到问题:
1.2330668424210091e+20, 123306684242100912128, 123306684242100908546
2.568889255043769e+18, 2568889255043768832, 2568889255043769002
5.351852614674518e+16, 53518526146745184, 53518526146745184
1114969294723858.0, 1114969294723858, 1114969294723858
23228526973413.707, 23228526973413, 23228526973413
483927645279.4375, 483927645279, 483927645279
10081825943.3125, 10081825943, 10081825943
210038040.47916666, 210038040, 210038040
4375792.5, 4375792, 4375792
91162.33333333333, 91162, 91162
1899.2083333333333, 1899, 1899
39.5625, 39, 39
0.8125, 0, 0
您可以清楚地看到,在前两行中,值太大而无法放入浮点数中,从而导致舍入问题。
因此,将 math.floor(i/48)
替换为 i//48
可以解决问题,因为您可以直接计算从任意精度到任意精度的除法,而无需在两者之间强制使用浮点数。
我创建了一个将二进制作为字符串接收然后将其转换为 base48 的函数,它适用于大多数测试用例,但是当我通过它运行“1010000001101101011000000100000001000101111010000101101010110000001100110”时
import math
def Binary2Octoquadragesimal(n):
octdict = {"0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","10":"a","11":"b","12":"c","13":"d","14":"e","15":"f","16":"g","17":"h","18":"i","19":"j","20":"k","21":"l","22":"m","23":"n","24":"o","25":"p","26":"q","27":"r","28":"s","29":"t","30":"u","31":"v","32":"w","33":"x","34":"y","35":"z","36":"A","37":"B","38":"C","39":"D","40":"E","41":"F","42":"G","43":"H","44":"I","45":"J","46":"K","47":"L",}
ans = []
if n == "":
return ""
i = int(n,2)
if i == 0:
return "0"
while i > 0:
ans.append(octdict[str(i%48)])
i = math.floor(i/48)
ans.reverse()
print (ans)
return ("".join(ans))
它返回错误的值,我仔细查看了我的代码,似乎在这个特定的测试用例中我的函数 return Dragonfly00w6 而不是 Dragonfly2026 为什么这只发生在这种情况下而不是其他的?我该如何补救
您的问题是浮点数不准确。这个数字太大了,无法用 python 浮点数表示。整数之所以有效,是因为 python 整数是任意精度的。
经过几次迭代后,问题自行解决,因为错误只发生在其余部分,除法隐藏了问题。
如果你在每次迭代中都做print(f"{i/48}, {math.floor(i/48)}, {i//48}")
,你会看到问题:
1.2330668424210091e+20, 123306684242100912128, 123306684242100908546
2.568889255043769e+18, 2568889255043768832, 2568889255043769002
5.351852614674518e+16, 53518526146745184, 53518526146745184
1114969294723858.0, 1114969294723858, 1114969294723858
23228526973413.707, 23228526973413, 23228526973413
483927645279.4375, 483927645279, 483927645279
10081825943.3125, 10081825943, 10081825943
210038040.47916666, 210038040, 210038040
4375792.5, 4375792, 4375792
91162.33333333333, 91162, 91162
1899.2083333333333, 1899, 1899
39.5625, 39, 39
0.8125, 0, 0
您可以清楚地看到,在前两行中,值太大而无法放入浮点数中,从而导致舍入问题。
因此,将 math.floor(i/48)
替换为 i//48
可以解决问题,因为您可以直接计算从任意精度到任意精度的除法,而无需在两者之间强制使用浮点数。