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

Always floor time instead of rounding up #49

Merged
merged 1 commit into from
Apr 27, 2020

Conversation

villebro
Copy link
Contributor

@villebro villebro commented Apr 19, 2020

Currently the rounding logic is slightly broken. For instance, in the unit tests, 95543ms, ie. 1 min 35 sec 543 ms rounded to 3 second decimal digits with colonNotation=true yields 1:35.580 (this should obviously be 1:35.543). I also propose that time rounding should always floor the value, not regular rounding up when the remainder is >=0.5, as otherwise the the interval between 1-2 seconds will only be 0.5 seconds long, as it is rounded to 2 seconds after as time passes 1500 ms.

This PR changes rounding so that it always rounds down. For example, with zero second precision, 1999 ms is 1 second, not 2 seconds. A new flooring function is introduced that adds an epsilon term prior to flooring to avoid floating point errors, which replaces the current logic. Without it, one the verbose unit tests for 5254ms rounded to 4 second decimal places would yield 5.2539 seconds, not 5.2540 seconds).

@sindresorhus
Copy link
Owner

@explodingcamera @villebro Thoughts on this?

@villebro
Copy link
Contributor Author

In the absence of further comments I propose merging this, as I feel the fix is fairly critical.

@sindresorhus sindresorhus changed the title fix: always floor time instead of rounding up Always floor time instead of rounding up Apr 27, 2020
@sindresorhus sindresorhus merged commit 20cbdaf into sindresorhus:master Apr 27, 2020
@villebro villebro deleted the fix_rounding branch April 28, 2020 06:39
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.

2 participants