首页 > 解决方案 > python中的十进制表示

问题描述

当给定输入时,我得到像 1.0 这样的结果,但我需要它是 1.000000000000 我应该如何在hypot中修改?

small=1000000000
from math import hypot
def pairwise(iterable):
    a, b = iter(iterable), iter(iterable)
    next(b, None)
    return zip(a, b)
n=int(input())
l1=[]
l2=[]
for i in range(0,n):
    c,d=list(map(int,input().split()))
    l1.append(c)
    l2.append(d)
a=tuple(l1)
b=tuple(l2)
dist = [float(hypot(p2[0]-p1[0], p2[1]-p1[1])) for p1, p2 in pairwise(tuple(zip(a, b)))]
for x in dist:
    if x<small:
        small=x
print(small)
    

标签: pythonmathfloating-pointprecisioneuclidean-distance

解决方案


您可以使用如下format方法:

format(math.pi, '.12g') # Give 12 significant digits of pi

format(math.pi, '.2f') # Give 2 digits of pi after decimal point

推荐阅读