Skip to content

Conversation

@xs
Copy link

@xs xs commented Feb 16, 2017

After we convert the microseconds string to a float and multiply by 1e6, it's possible that we lose a microsecond when casting to an integer again:

Example:

In [1]: usecs = float('0.524226') * 1e6

In [2]: usecs
Out[2]: 524225.99999999994

In [3]: int(usecs)
Out[3]: 524225

Test code:

old_errors = 0
new_errors = 0

for expected in range(1000000):
    usecs_string = '0.' + str(expected).zfill(6)

    usecs = float(usecs_string) * 1e6

    old_actual = int(usecs)
    new_actual = int(round(usecs))

    if old_actual != expected:
        old_errors += 1

    if new_actual != expected:
        new_errors += 1


print 'old_errors: ' + str(old_errors)
print 'new_errors: ' + str(new_errors)

Test output:

old_errors: 11549
new_errors: 0

@methane
Copy link
Member

methane commented Feb 16, 2017

Please check PyMySQL's code before trying fix by yourself.
Otherwise, many different implementations are born. I can't maintain them.

@methane methane closed this Mar 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants