Skip to content

Commit dd2b7f7

Browse files
committed
BF - high value comparisons hit float error
Found using the round trip tester - high values for min or max were hitting floating point error in comparisons, and comparing equal when they were not.
1 parent b6ea8a6 commit dd2b7f7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

nibabel/arraywriters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ def _do_scaling(self):
304304
# (u)int to (u)int
305305
info = np.iinfo(out_dtype)
306306
out_max, out_min = info.max, info.min
307-
if mx <= out_max and mn >= out_min: # already in range
307+
# If left as int64, uint64, comparisons will default to floats, and
308+
# these are inexact for > 2**53 - so convert to int
309+
if (as_int(mx) <= as_int(out_max) and
310+
as_int(mn) >= as_int(out_min)):
311+
# already in range
308312
return
309313
# (u)int to (u)int scaling
310314
self._iu2iu()

nibabel/tests/test_arraywriters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ def test_special_rt():
194194
assert_array_equal(round_trip(aw), 0)
195195

196196

197+
def test_high_int2uint():
198+
# Need to take care of high values when testing whether values are already
199+
# in range. There was a bug here were the comparison was in floating point,
200+
# and therefore not exact, and 2**63 appeared to be in range for np.int64
201+
arr = np.array([2**63], dtype=np.uint64)
202+
out_type = np.int64
203+
aw = SlopeInterArrayWriter(arr, out_type)
204+
assert_equal(aw.inter, 2**64)
205+
206+
197207
def test_slope_inter_castable():
198208
# Test scaling for arraywriter instances
199209
# Test special case of all zeros

0 commit comments

Comments
 (0)