Skip to content

Commit

Permalink
small optimization, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pat-schmitt committed Oct 1, 2024
1 parent e773037 commit 48d7551
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions oggm/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ def test_floatyear_to_date(self):
r = utils.floatyear_to_date(yr)
assert r == (1998, 2)

# tests for floating point precision
yr = 1 + 1/12 - 1/12
r = utils.floatyear_to_date(yr)
assert r == (1, 1)

for i in range(12):
yr = 2000
r = utils.floatyear_to_date(yr + i / 12)
assert r == (yr, i + 1)

def test_date_to_floatyear(self):

r = utils.date_to_floatyear(0, 1)
Expand Down
8 changes: 4 additions & 4 deletions oggm/utils/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,13 @@ def floatyear_to_date(yr):
yr = np.array([yr], dtype=np.float64)

# check if year is inside machine precision to next higher int
yr_ceil = np.ceil(yr)
yr = np.where(np.isclose(yr,
np.ceil(yr),
# larger numbers have a smaller precision
rtol=np.finfo(np.float64).eps * np.max(yr),
yr_ceil,
rtol=np.finfo(np.float64).eps,
atol=0
),
np.ceil(yr),
yr_ceil,
yr)

out_y, remainder = np.divmod(yr, 1)
Expand Down

0 comments on commit 48d7551

Please sign in to comment.