Skip to content
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

Fix over-precise float formatting on Python 3 #1241

Merged
merged 4 commits into from
Apr 2, 2019

Conversation

mgedmin
Copy link
Contributor

@mgedmin mgedmin commented Apr 2, 2019

Sometimes tox would tell me things like

✔ OK py27 in 4.1370000000000005 seconds
✔ OK py36 in 14.5 seconds
✔ OK py37 in 15.029 seconds
✔ OK py35 in 17.519 seconds
✔ OK py34 in 18.85 seconds

the almost-rounded-to-3-decimals value was both funny and sad, but mostly irritating.

This was happening because on Python 3 float.__str__ was changed to be more precise, and now

>>> print(4 + 0.137)
4.1370000000000005

We can avoid the problem if we round the result of the addition instead of adding a rounded number to the integer number of seconds:

>>> print(4.137)
4.137

I've also corrected the plural logic because "0.5 second" is wrong:
https://ell.stackexchange.com/questions/7817/singular-or-plural-for-seconds

Contribution checklist:

(also see CONTRIBUTING.rst for details)

  • wrote descriptive pull request text
  • added/updated test(s)
  • updated/extended the documentation -- not applicable, I think
  • added relevant issue keyword
    in message body
    -- I've filed no issue and found no existing issue for this
  • added news fragment in changelog folder --
    • fragment name: <issue number>.<type>.rst for example (588.bugfix.rst)
    • <type> is must be one of bugfix, feature, deprecation,breaking, doc, misc
    • if pr has no issue: consider creating one first or change it to the pr number after creating the pr
    • "sign" fragment with "by @"
    • please use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files - by :user:superuser."
    • also see examples
  • added yourself to CONTRIBUTORS (preserving alphabetical order)

Sometimes tox would tell me things like

    ✔ OK py27 in 4.1370000000000005 seconds
    ✔ OK py36 in 14.5 seconds
    ✔ OK py37 in 15.029 seconds
    ✔ OK py35 in 17.519 seconds
    ✔ OK py34 in 18.85 seconds

the almost-rounded-to-3-decimals value was both funny and sad, but
mostly irritating.

This was happening because on Python 3 float.__str__ was changed to be
more precise, and now

    >>> print(4 + 0.137)
    4.1370000000000005

We can avoid the problem if we round the result of the addition instead
of adding a rounded number to the integer number of seconds:

    >>> print(4.137)
    4.137

I've also corrected the plural logic because "0.5 second" is wrong[*]:
https://ell.stackexchange.com/questions/7817/singular-or-plural-for-seconds
mgedmin added 3 commits April 2, 2019 16:13
Also sorted the CONTRIBUTORS file to make sure I've preserved the
alphabetical order, which adjusted some existing names.
@gaborbernat gaborbernat merged commit 513bc00 into tox-dev:master Apr 2, 2019
@mgedmin mgedmin deleted the float-formatting branch April 2, 2019 14:10
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.

3 participants