python怎么处理非常大的数的计算?

会奔跑的bug edited 3 年,8 月前

进行比较小的数的计算时没有问题,但是计算大的数时就不准确了。比如下面的数值
‘-999999962927782243 -999999945341416211 -999999903595552532 -999999978590083794’

s_list = list(map(int,input().split(' ')))
x1,y1,x2,y2 = s_list[0],s_list[1],s_list[2],s_list[3]
if y1==y2:
    #当为直线时
    if abs(x1-x2)%2 == 0:
        print(int((x1+x2)/2),y1)
    else:
        print('NO')
elif x1==x2:
    # 当为竖线时
    if abs(y1-y2)%2 == 0:
        print(x1,int((y1+y2)/2))
    else:
        print('NO')
else:
    if (x1+x2)%2 == 0 and (y1+y2)%2==0:
        # 普通情况且中间点为整数
        print(int((x1+x2)/2),int((y1+y2)/2))
    else:
        # 普通情况中间点不为整数
        # 通过平移来减小对应的数值
        x3 = round((x1+x2)/2)
        y3 = round((y1+y2)/2)
        x1 = x1-x3
        x2 = x2-x3
        y1= y1-y3
        y2= y2-y3
        x5 = (x1+x2)/2
        y5 = (y1+y2)/2
        # 这个地方可能是隐患,造成的计算不准确,这个地方不是整数,等到后面差异就会变大
        a = (x1-x2)/(y1-y2)*-1
        b = y5-a*x5
        x4 = (x1+x2)//2
        ab_len_1 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
        while True:
            y4 = a*x4 + b
            # ab_len_2 = (x1-x4)*(x1-x4)+(y1-y4)*(y1-y4)
            # if int(y4) == y4:
            #     ab_len_3 = (x2-x4)*(x2-x4)+(y2-y4)*(y2-y4)
            #     if ab_len_2 <= ab_len_1 and ab_len_2==ab_len_3:
            #         print(x4+x3,int(y4)+y3)
            #         break
            #     elif ab_len_2 >= ab_len_1:
            #         print('NO')
            #         break
            # if ab_len_2 >= ab_len_1:
            #     print('NO')
            #     break
            # x4 = x4-1
            # continue
            y4 = round(y4,4)
            ab_len_2 = (x1-x4)*(x1-x4)+(y1-y4)*(y1-y4)
            ab_len_3 = (x2-x4)*(x2-x4)+(y2-y4)*(y2-y4)
            if ab_len_2 <= ab_len_1:
                if int(y4) == y4 and ab_len_2==ab_len_3 :
                    print(x4+x3,int(y4)+y3)
                    break
                else:
                    x4 = x4-1
                    continue
            else:
                print('NO')
                break
        

Comments

友利奈绪

6666