Skip to content

escape_float loses precision compared to mysqlclient #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamadden opened this issue Jul 25, 2019 · 1 comment · Fixed by #806
Closed

escape_float loses precision compared to mysqlclient #803

jamadden opened this issue Jul 25, 2019 · 1 comment · Fixed by #806

Comments

@jamadden
Copy link
Contributor

The default float converter, defined in pymysql.converters.escape_float looks like this:

def escape_float(value, mapping=None):
    return ('%.15g' % value)

For floating point values with more than 15 digits, this loses information. The mysqlclient equivalent preserves it:

def Float2Str(o, d):
    s = repr(o)
    if s in ('inf', 'nan'):
        raise ProgrammingError("%s can not be used with MySQL" % s)
    if 'e' not in s:
        s += 'e0'
    return s

Losing this precision can arise, for example, in sending the output of time.time() to the database, and sometimes those final digits make a difference. Here's one example, using a timestamp of 1564063129.1277142:

$ python3.7 -c 'from MySQLdb.converters import Float2Str; print(Float2Str(1564063129.1277142, None))'
1564063129.1277142e0
$ python3.7 -c 'from pymysql.converters import escape_float; print(escape_float(1564063129.1277142))'
1564063129.12771

Notice PyMySQL lops off the final two digits.

This was easy enough to fix by overriding the conv dict to the connect() method, but maybe the default could be to preserve all the digits, like MySQLdb does?

@jamadden
Copy link
Contributor Author

Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant