这是我的代码,我正在尝试使用数学日志找到整数的对数。 我尝试了大部分方法来解决不准确的日志,但似乎没有什么能解决我的代码:
num = 810333333333333333253432343224234
print(num)
getcontext().prec = 100 # set the precision (no. of digits)
getcontext().rounding = ROUND_DOWN # this will effectively truncate
logA = Decimal(math.log(num,2))
print(logA)
#find num using component of logs
seedfrac = Decimal(logA - int(logA))
nonfrac = 2**int(logA)
frac = Decimal(2**seedfrac)
print(Decimal(frac*nonfrac))
#find num directly
print(Decimal(2**logA ))
输出检查值:
^{pr2}$请回复,如果你知道任何解决办法,谢谢。在
math.log
只对float起作用,所以当你使用Decimal(math.log(num,2))
时,你只是对将{首先将数字转换为十进制实例,保持精度,然后使用其对数方法之一(为便于阅读,在下面插入新行):
相关问题 更多 >
编程相关推荐