Python - 如何避免gmpy2中x的基数y ** log y的差异

时间:2016-07-27 10:08:43

标签: python logarithm gmpy

以下代码示例我的问题在10次幂10和10次幂11之间不会发生,但是对于代码及其上面给出的示例。

我无法看到我的代码中的哪个位置无法正确处理原始值的检索。可能是我错过了一些简单的事情。

我需要确保我可以从x恢复log x各种基础。是否存在任何反向反日志算法,而不是依赖于gmpy2之类的库函数,这保证说2**log2(x)它会给x。< / p>

我可以看到如何直接开发日志,但不知道如何取回,例如,泰勒系列需要很多术语...... How can I write a power function myself?和@ 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()

3 个答案:

答案 0 :(得分:1)

只要设置十进制模块精度,通常的建议是使用十进制数据类型

bringSubviewToFront

对于不同的基础,您可能需要使用对数公式:

from decimal import Decimal, getcontext

getcontext().prec = 1000

# Just a different method to get the random number:
x = Decimal(round(10**20 * (1 + 9 * random.random()))) 

x10 = Decimal.log10(x)
e10 = 10**x10

e10 - x
#outputs: Decimal('5.2E-978')

答案 1 :(得分:1)

注意:我维护gmpy2库。

在您的示例中,您使用的是getcontext()模块中的decimal。您没有更改gmpy2使用的精度。由于gmpy2的默认精度为53位,而x的值需要69位,因此预计会出现错误。

以下是您的示例的更正版本,说明了在您提高精度时累积错误的变化情况。

import gmpy2

def rint(n):
    gmpy2.get_context().precision = n
    # check accuracy of exp(log(x))
    e = gmpy2.exp(1)
    l2 = gmpy2.log(2)
    l10 = gmpy2.log(10)
    x = 481945878080003762113
    # logs to different bases
    x2 = gmpy2.log2(x)
    x10 = gmpy2.log10(x)
    xe = gmpy2.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("precision", n, "value", x, "error sum", error)

for n in range(65, 81):
    rint(n)

以下是结果。

precision 65 value 481945878080003762113 error sum 1061
precision 66 value 481945878080003762113 error sum 525
precision 67 value 481945878080003762113 error sum -219
precision 68 value 481945878080003762113 error sum 181
precision 69 value 481945878080003762113 error sum -79
precision 70 value 481945878080003762113 error sum 50
precision 71 value 481945878080003762113 error sum -15
precision 72 value 481945878080003762113 error sum -14
precision 73 value 481945878080003762113 error sum 0
precision 74 value 481945878080003762113 error sum -2
precision 75 value 481945878080003762113 error sum 1
precision 76 value 481945878080003762113 error sum 0
precision 77 value 481945878080003762113 error sum 0
precision 78 value 481945878080003762113 error sum 0
precision 79 value 481945878080003762113 error sum 0
precision 80 value 481945878080003762113 error sum 0

答案 2 :(得分:0)

正如承认的那样,阿奎解决了我的问题。 我没有考虑到需要超过15位数的精度。 对另一个问题的回答涵盖了这一点。 gmpy2 log2 not accurate after 16 digits

相关问题