下面的代码示例我的问题不会发生在 10 次方 10 和 10 次方 11 之间,但对于代码中给出的示例及其上方的示例。
我看不到我的代码中有哪些地方没有正确处理原始值的检索。可能是我错过了一些简单的事情。
我需要确保我可以x
从log x
各种基地中恢复过来。与其依赖诸如 之类的库函数gmpy2
,是否有任何反向反对数算法可以保证2**log2(x)
它会给出x
.
我可以看到如何直接开发日志,但看不到如何返回,例如,泰勒级数需要很多术语...我如何自己编写幂函数?和@dan04 回复。代码如下。
from gmpy2 import gcd, floor, next_prime, is_prime
from gmpy2 import factorial, sqrt, exp, log,log2,log10,exp2,exp10
from gmpy2 import mpz, mpq, mpfr, mpc, f_mod, c_mod,lgamma
from time import clock
import random
from decimal import getcontext
x=getcontext().prec=1000 #also tried 56, 28
print(getcontext())
def rint():#check accuracy of exp(log(x))
e=exp(1)
l2=log(2)
l10=log(10)
#x=random.randint(10**20,10**21) --replaced with an actual value on next line
x=481945878080003762113
# logs to different bases
x2=log2(x)
x10=log10(x)
xe=log(x)
# logs back to base e
x2e=xe/l2
x10e=xe/l10
#
e2=round(2**x2)
e10=round(10**x10)
ex=round(e**xe)
#
ex2e=round(2**x2e)
ex10e=round(10**x10e)
error=5*x-(e2+e10+ex+ex2e+ex10e)
print(x,"error sum",error)
#print(x,x2,x10,xe)
#print(x2e,x10e)
print(e2,e10,ex)
print(ex2e,ex10e)
rint()