Search code examples
pythonpython-3.xprecisionfloating-accuracynanotime

Float difference in string and int format. Why?


I need the current time with nanosec. As string "%.9f" % float_time it is 1502872986.653693676. You can see 76 at the end. If I try to write the same time as int int(float_time*10**9), it will be 1502872986653693696 and 96 at the end. Why? And which is the right way to get nano format?

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

Solution:

time.monotonic()

float_time: 536596.296

int_nano_time: 536596296000000

str_nano_time: 536596.296000000


Solution

  • Both are correct within the IEEE754 double precision of ~15.7 decimal digits. The reason is that the multiples of ten cannot be produced exactly, so while multiplying by 2 ** 10 is exact, multiplying by 5 ** 10 will have rounding errors again, and that's why the last numbers differ.

    '1502872986.653693675994873046875000000000'
    >>> "%.30f" % (1502872986.653693676 * 10 ** 9)
    '1502872986653693696.000000000000000000000000000000'
    

    The hex representations of these 2 floating point numbers are wildly different:

    >>> (1502872986.653693676 * 10 ** 9).hex()
    '0x1.4db4704d00617p+60'
    >>> (1502872986.653693676).hex()
    '0x1.6650166a9d61ep+30'
    

    As for the time returned from the time() call - well it is highly probable that should the computer have a timer that gives a timestamp with high enough precision any time value will be between the exact time time() was called and when it returned:

    >>> (time() - time()) * 10 ** 9
    -715.2557373046875
    >>> (time() - time()) * 10 ** 9
    -953.67431640625
    >>> (time() - time()) * 10 ** 9
    -953.67431640625
    

    your error was 20 nanoseconds, but the time difference between 2 consecutive timestamps returned from time() call is 700-100 nanoseconds on my computer.