python中的精度高达50位小数

python中的精度高达50位小数,python,precision,floating-point-precision,double-precision,Python,Precision,Floating Point Precision,Double Precision,我试着编码,以找出内圆的半径,代码运行良好,但我没有得到所需的精度,我希望答案被截断到50位 请建议如何获得更精确的计算结果 如何获得50位数的精度 import math t=input() while t>0: t-=1 r1,r2,r3=map(int,raw_input().split()) k1=1.0/r1 k2=1.0/r2 k3=1.0/r3 k4=k1+k2+k3+2*math.sqrt(k1*k2+k2*k3+k3*k1)

我试着编码,以找出内圆的半径,代码运行良好,但我没有得到所需的精度,我希望答案被截断到50位 请建议如何获得更精确的计算结果 如何获得50位数的精度

import math
t=input()
while t>0:
    t-=1
    r1,r2,r3=map(int,raw_input().split())
    k1=1.0/r1
    k2=1.0/r2
    k3=1.0/r3
    k4=k1+k2+k3+2*math.sqrt(k1*k2+k2*k3+k3*k1)
    r4=1.0/k4
    print r4
使用模块。将使用的所有变量存储为
decimal.decimal
对象

更新代码:

from decimal import *
import math
context = Context(prec=1000)
setcontext(context)
t=input()
while t>0:
    t-=1
    r1,r2,r3=map(Decimal,raw_input().split())
    k1=Decimal(1.0)/Decimal(r1)
    k2=Decimal(1.0)/Decimal(r2)
    k3=Decimal(1.0)/Decimal(r3)
    k4=k1+k2+k3+2*(k1*k2+k2*k3+k3*k1).sqrt()
    r4=Decimal(1.0)/Decimal(k4)
    print r4
使用模块。将使用的所有变量存储为
decimal.decimal
对象

更新代码:

from decimal import *
import math
context = Context(prec=1000)
setcontext(context)
t=input()
while t>0:
    t-=1
    r1,r2,r3=map(Decimal,raw_input().split())
    k1=Decimal(1.0)/Decimal(r1)
    k2=Decimal(1.0)/Decimal(r2)
    k3=Decimal(1.0)/Decimal(r3)
    k4=k1+k2+k3+2*(k1*k2+k2*k3+k3*k1).sqrt()
    r4=Decimal(1.0)/Decimal(k4)
    print r4

如果更多的迭代应该产生更好的结果,那么您可以忽略输入
t
参数并进行迭代,直到结果在当前精度范围内收敛:

import decimal

#NOTE: separate user input from the algorithm
#      no input inside `radius()` function
def radius(r1, r2, r3):
    with decimal.localcontext() as ctx:
        ctx.prec += 2 # increase precision for intermediate calculations
        prev = None # previous value
        k1, k2, k3 = [1 / decimal.Decimal(r) for r in [r1, r2, r3]]
        while True: 
            # change some parameters to simulate converging algorithm
            #NOTE: you don't need to wrap anything using `Decimal()`
            k1 = k1 + k2 + k3 + 2*(k1*k2 + k2*k3 + k3*k1).sqrt()
            r = 1 / k1
            if prev == r: # compare using enhanced precision
                break
            prev = r # save previous value
    return +r # `+` applies current precision

decimal.getcontext().prec = 50 # set desired precision
print(radius(*map(int, raw_input().split())))

对于演示此技术的工作示例,请参见计算Pi最多100位。

如果更多的迭代应产生更好的结果,则可以忽略输入
t
参数并迭代,直到结果在当前精度范围内收敛:

import decimal

#NOTE: separate user input from the algorithm
#      no input inside `radius()` function
def radius(r1, r2, r3):
    with decimal.localcontext() as ctx:
        ctx.prec += 2 # increase precision for intermediate calculations
        prev = None # previous value
        k1, k2, k3 = [1 / decimal.Decimal(r) for r in [r1, r2, r3]]
        while True: 
            # change some parameters to simulate converging algorithm
            #NOTE: you don't need to wrap anything using `Decimal()`
            k1 = k1 + k2 + k3 + 2*(k1*k2 + k2*k3 + k3*k1).sqrt()
            r = 1 / k1
            if prev == r: # compare using enhanced precision
                break
            prev = r # save previous value
    return +r # `+` applies current precision

decimal.getcontext().prec = 50 # set desired precision
print(radius(*map(int, raw_input().split())))

有关演示此技术的工作示例,请参阅计算Pi高达100位。

为什么这给出的运行时错误几乎正确。我马上更新你的代码。啊,唯一的错误是你需要在函数的开头添加
import decimal
,或者删除第3行和第4行的
decimal.
。它现在应该可以工作了。你能在这里添加更新的代码吗?thanksIt给出了运行时错误。为什么这给出的运行时错误几乎是正确的。我马上更新你的代码。啊,唯一的错误是你需要在函数的开头添加
import decimal
,或者删除第3行和第4行的
decimal.
。它现在应该可以工作了。你能在这里添加更新后的代码吗,谢谢你给出了运行时错误