我需要nanosec的当前时间。 作为字符串"%.9f" % float_time
它是1502872986.6536936 。最后你可以看到76。 如果我尝试写的同时,INT int(float_time*10**9)
,这将是15028729866536936 和96底。为什么?哪个是获得纳米格式的正确途径?字符串和int格式的浮点差异。为什么?
from time import time
float_time = time()
print("float_time:", float_time)
int_nano_time = int(float_time*10**9)
print("int_nano_time:", int_nano_time)
str_nano_time = "%.9f" % float_time
print("str_nano_time:", str_nano_time)
float_time:1502872986.6536937
int_nano_time:1502872986653693696
str_nano_time:1502872986.653693676
解决方案:
time.monotonic()
float_time:536596.296
int_nano_time:5365962.96亿
str_nano_time:536596.296000000
[Python使用IEEE-754双精度(https://docs.python.org/3.3/tutorial/floatingpoint.html),因此具有在有效数部分只有53位,它们是约15〜17个十进制数字(1502872986.6536936有17位有效数字,其余只是噪声) –
@Elena很可能返回的'time()'不正确到纳秒精度。 –
@AnttiHaapala是否有可能在Python中获得int int的nanosec?在C++世界里,他们没有问题,因为他们在64位int中得到它。他们不符合IEEE754。 – Elena