Skip to content

Use rounding instead of // in convert_str_time#138

Merged
hejamu merged 9 commits into
MDAnalysis:mainfrom
DamienTOQUER:patch-1
May 27, 2026
Merged

Use rounding instead of // in convert_str_time#138
hejamu merged 9 commits into
MDAnalysis:mainfrom
DamienTOQUER:patch-1

Conversation

@DamienTOQUER
Copy link
Copy Markdown
Contributor

@DamienTOQUER DamienTOQUER commented May 21, 2026

Change integer division to rounding for str to frame conversion. Solve #137


📚 Documentation preview 📚: https://mdacli--138.org.readthedocs.build/en/138/

Change integer division to rounding for time conversion.
@hejamu
Copy link
Copy Markdown
Collaborator

hejamu commented May 21, 2026

Thanks for catching this @DamienTOQUER! This fix would however change the floor behaviour of the code.

Example:
convert_str_time("1ps", 0.1): old = 9, PR = 10
convert_str_time("10ps", 1): old = 10, PR = 10
convert_str_time("12.7ps", 1): old = 12, PR = 13 <-- here
convert_str_time("0.5ps", 1): old = 0, PR = 0

A safer way would be to do:

return int(np.floor(np.round(val / dt, 9)))

Change return type to int and use floor rounding.
@hejamu
Copy link
Copy Markdown
Collaborator

hejamu commented May 21, 2026

@DamienTOQUER can you add a test for this? Also feel free to add yourself to AUTHORS.rst

@PicoCentauri can you take a look at the cases above if this is now all expected behavior?

Copy link
Copy Markdown
Collaborator

@PicoCentauri PicoCentauri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix. I think we might fix the tests as well.

Comment thread src/mdacli/utils.py
Comment thread src/mdacli/utils.py
Comment thread tests/test_utils.py
Comment thread docs/AUTHORS.rst Outdated
@PicoCentauri
Copy link
Copy Markdown
Collaborator

Merge when your are happy @hejamu

@hejamu
Copy link
Copy Markdown
Collaborator

hejamu commented May 26, 2026

@DamienTOQUER Sorry for nitpicking, just one more thing. The docs look weird now (https://mdacli--138.org.readthedocs.build/en/138/api/utils.html)

Try something like

    Notes
    -----
    If we don't use the 9 digits then the floating point behaviour of np.floor would be unstable, i.e.:

    >>> np.floor(0.9999999999999999)
    0.0

@DamienTOQUER
Copy link
Copy Markdown
Contributor Author

The note was a bit weird in the docs, so I tried to update it to make it clearer

@hejamu hejamu merged commit 0c1ff76 into MDAnalysis:main May 27, 2026
11 checks passed
@DamienTOQUER DamienTOQUER deleted the patch-1 branch May 27, 2026 00:11
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