diff --git a/dcaf/shared/time.py b/dcaf/shared/time.py index 4c1da22..c658ea6 100644 --- a/dcaf/shared/time.py +++ b/dcaf/shared/time.py @@ -24,7 +24,15 @@ from dcaf.shared.validation import validate_non_negative -_FLOAT_TOLERANCE = 1e-12 +# A floating point absolute error tolerance of 1e-8 is chosen because this is on the order +# of <1 second over a year +# 1e-8 * 365 days = 0.315 sec +# and <1 ms over a day +# 1e-8 * 1 day = 0.864 ms +# While floating point arithmetic has several orders of magnitude more precision than 1e-8, +# this is already a very strict tolerance for the domain setting; DCAF uses the day as the +# base unit of time. +_FLOAT_TOLERANCE = 1e-8 class PeriodTruncationWarning(UserWarning): diff --git a/tests/unit/test_time_helpers.py b/tests/unit/test_time_helpers.py index a292d2c..76b8560 100644 --- a/tests/unit/test_time_helpers.py +++ b/tests/unit/test_time_helpers.py @@ -57,6 +57,8 @@ def test_is_month_end_when_next_day_is_start_of_next_month(day): # === _whole_days_with_truncation === +# NOTE: The function implementation uses a floating point tolerance of 1e-8, corresponding to +# <1 second per year and <1 ms per day. @pytest.mark.parametrize( @@ -66,8 +68,10 @@ def test_is_month_end_when_next_day_is_start_of_next_month(day): (15.5, (15, True)), (0.5, (0, True)), (0.0, (0, False)), - (14.0 + 1e-13, (14, False)), # within tolerance of a whole day - (14.0 - 1e-13, (14, False)), + (14.0 + 1e-12, (14, False)), # within tolerance of a whole day + (14.0 - 1e-12, (14, False)), + (14.0 - 5e-9, (14, False)), # just inside the 1e-8 tolerance + (14.0 - 2e-8, (13, True)), # just outside the 1e-8 tolerance (14.0 - 1e-6, (13, True)), # outside tolerance, so the partial day is dropped (14.0 + 1e-6, (14, True)), # outside tolerance, so the partial day is dropped ],