摘要:哪个 Python 包或 C 库是非常高精度算术运算的最佳选择?
我有一些转换小数天数的函数(0.0-0.99999..
)转换为人类可读的格式(小时、分钟、秒;但更重要的是:毫秒、微秒、纳秒)。
转换是通过以下函数完成的:
(请注意,我还没有实施时区校正)
d = lambda x: decimal.Decimal(str(x))
cdef object fractional2hms(double fractional, double timezone):
cdef object total, hms, ms_mult
cdef int i
hms = [0,0,0,0,0,0]
ms_mult = (d(3600000000000), d(60000000000), d(1000000000), d(1000000), d(1000), d(1))
# hms = [0,0,0,0,0]
total = d(fractional) * d(86400000000000)
for i in range(len(ms_mult)):
hms[i] = (total - (total % ms_mult[i])) / ms_mult[i]
total = d(total % ms_mult[i])
return ([int(x) for x in hms])
和小数:
def to_fractional(self):
output = (self.hour / d(24.0)) + (self.minute / d(1440.0))
output += (self.second / d(86400.0)) + (self.millisecond / d(86400000.0))
output += self.microsecond / d(86400000000.0)
output += self.nanosecond * (d(8.64) * d(10)**d(-9))
return output
然而,我的来回转换结果并不准确:
jdatetime.DayTime.fromfractional(d(0.567784356873)).to_fractional()
Decimal('0.56779150214342592592592592592592592592592592592592592592592592592592592592592592592592592592592592592592592592592')
# Difference in-out: Decimal('0.000007145270')
当我改变时d()
返回一个常规的 Python 浮点数:
# Difference in-out: 7.1452704258900823e-06 (same)
因此,我的问题是:哪个 Python 包或 C 库能够更准确地做到这一点?