float最多能表示小于8388607的小数点后7位,但绝对能保证的为6位,也即float的十进制的精度为为6~7位。
double数据类型绝对值最小可以取到2^-1024,精度则为2^52-1=4503599627370495,为16位。所以精度最高位16位,一定可以保证15位。
未规定long double的确切精度。(不少于double的精度,但不会大多少)
python decimal模块:(十进制浮点运算)
包:from decimal import *
浮点数据转换为Decimal类型
Decimal.from_float(float)
设定有效数字
getcontext().prec = num
四舍五入,保留几位小数
Decimal('123.13623').quantize(Decimal('0.00'))
result:123.14
Decimal 转string
str(Decimal('123.13623').quantize(Decimal('0.00')))
https://ac.nowcoder.com/acm/contest/874/D
from decimal import *
t = int(input())
for case in range(t):
with localcontext() as ctx:
ctx.prec = 50
n = int(input())
a = Decimal(sum(map(int,input().strip().split())))
b = Decimal(sum(map(int,input().strip().split())))
ans = a*b/n
print(format(ans,".30f"))