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

[Request] Max Error parameter for rounding #156

Closed
supRy opened this issue Sep 13, 2020 · 1 comment
Closed

[Request] Max Error parameter for rounding #156

supRy opened this issue Sep 13, 2020 · 1 comment

Comments

@supRy
Copy link

supRy commented Sep 13, 2020

The examples I am providing are based on the following use case, but I believe this applies more generally across the tool. I am using naturaldelta to describe the duration of a route (for example Miami, FL to Atlanta, GA is 9 hours).

One concern is the rounding (2 hours 59 minutes becomes 2 hours), but #154 seems to resolve that concern.

My second concern is the precision in the range of about 1-10 hours. Currently, 2 hours 29 minutes becomes 2 hours. I feel like that misrepresents the actual duration given that it is off by 20%. I am wondering if "2 and a half hours" would be a better result. I'm not sure on the most natural language way to write this, "2 hours 30 minutes" is probably the most generalized form.

Initially, I was thinking this would be connected to the rounding, perhaps as an alternative option rounding to half values. So 45 minutes to 1 hour 15 minutes would round to 1 hour. 1 hour 15 minutes to 1 hour 45 minutes would round to 1.5 hour. However, thinking more broadly different applications will have different precision requirements. Allowing users to set a max_error and then using that to determine the best rounding would be a good solution.

So considering a max_error of 10%, 8,880 seconds (2 hours, 28 minutes) would become 2 hours and 30 minutes. This is because 2 hours is 19% off (greater than 10%). So we drop to the next smallest unit (30 minutes) and that results in an error of 1.4%, so we stop here. Now thinking about 9,660 seconds (2 hours 41 minutes) and a max error of 5%. This gets rounded to 3 hours, but has an error of 12%, so we cut to the next increment (30 minutes) and round to 2 hours 30 minutes. That has an error of 7%, so still too much. The next increment would be 15 minutes, so we round to 2 hours 45 minutes and stop there with an error of 2%.

Using a max_error parameter also resolves the statement I made earlier "My second concern is the precision in the range of about 1-10 hours". From this statement I can see that I care about the precision of 30 minutes up to 9 hours 30 minutes, so my desired precision is 5% (0.5 hour/9.5 hour). Beyond that, I would only get hours.

I'm thinking these increments (for timedelta) would be:

  • millenium
  • century
  • decade
  • years
  • 6 months
  • months
  • days
  • hours
  • 30 minutes
  • 15 minutes
  • 5 minutes
  • 1 minute
  • 30 seconds
  • 15 seconds
  • seconds
  • milliseconds
  • microseconds

Other functions would need to have their own increments developed.

@hugovk
Copy link
Collaborator

hugovk commented May 3, 2022

🚀 Development has moved to https://github.com/python-humanize/humanize 🚀

Please open new issues at https://github.com/python-humanize/humanize/issues

@hugovk hugovk closed this as completed May 3, 2022
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

No branches or pull requests

2 participants