8000 Incorrect Int Conversion · Issue #70 · foutaise/texttable · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Incorrect Int Conversion #70
Closed
Closed
@sanjit-bhat

Description

@sanjit-bhat

Overview

I think I found a bug in texttable's integer conversion logic. Specifically, the intermediate conversion to a floating-point produces an incorrect output for large integers due to floating-point imprecision.

Minimum example:

from texttable import Texttable
tt = Texttable()
tt.set_cols_dtype(['i'])  # dtype 'i' is the problem here
tt.add_rows([['hello'], [18014398509481983]])
print(tt.draw())

Expected output:

+-------------------+
|       hello       |
+===================+
| 18014398509481983 |
+-------------------+

Actual output:

+-------------------+
|       hello       |
+===================+
| 18014398509481984 |
+-------------------+

Explanation of bug

This problem is with texttable's integer (see dtype(['i']) above) conversion code in texttable.py:

@classmethod
def _fmt_int(cls, x, **kw):
    """Integer formatting class-method.
    - x will be float-converted and then used.
    """
    return str(int(round(cls._to_float(x))))

The cls._to_float(x) converts the input to a float, which produces the wrong value for large ints. E.g., float(18014398509481983) = 18014398509481984.

Possible solution idea:

Is the float conversion really necessary? If not, str(int(round(18014398509481983))) = 18014398509481983 produces the right answer.

Environment configuration:

Texttable version: 1.6.2
Python version: 3.8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0