Skip to content

escape_float loses precision compared to mysqlclient #803

Closed
@jamadden

Description

@jamadden

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions