13
13
14
14
from nose .tools import assert_true
15
15
16
+ from numpy .testing import assert_array_equal , assert_almost_equal
17
+
16
18
DEBUG = True
17
19
18
20
def round_trip (arr , out_dtype ):
@@ -48,6 +50,8 @@ def big_bad_ulp(arr):
48
50
I haven't thought about whether the vectorized log2 here could lead to
49
51
incorrect rounding; this only needs to be ballpark
50
52
53
+ This function might be used in nipy/io/tests/test_image_io.py
54
+
51
55
Parameters
52
56
----------
53
57
arr : array
@@ -60,15 +64,29 @@ def big_bad_ulp(arr):
60
64
"""
61
65
# Assumes array is floating point
62
66
arr = np .asarray (arr )
67
+ info = type_info (arr .dtype )
63
68
working_arr = np .abs (arr .astype (BFT ))
64
69
# Log2 for numpy < 1.3
65
- l2 = np .log (working_arr ) / LOGe2
66
- fl2 = np .floor (l2 )
67
- info = type_info (arr .dtype )
70
+ fl2 = np .zeros_like (working_arr ) + info ['minexp' ]
71
+ # Avoid divide by zero error for log of 0
72
+ nzs = working_arr > 0
73
+ fl2 [nzs ] = np .floor (np .log (working_arr [nzs ]) / LOGe2 )
68
74
fl2 = np .clip (fl2 , info ['minexp' ], np .inf )
69
75
return 2 ** (fl2 - info ['nmant' ])
70
76
71
77
78
+ def test_big_bad_ulp ():
79
+ for ftype in (np .float32 , np .float64 ):
80
+ ti = type_info (ftype )
81
+ fi = np .finfo (ftype )
82
+ min_ulp = 2 ** (ti ['minexp' ] - ti ['nmant' ])
83
+ in_arr = np .zeros ((10 ,), dtype = ftype )
84
+ in_arr = np .array ([0 , 0 , 1 , 2 , 4 , 5 , - 5 , - np .inf , np .inf ], dtype = ftype )
85
+ out_arr = [min_ulp , min_ulp , fi .eps , fi .eps * 2 , fi .eps * 4 ,
86
+ fi .eps * 4 , fi .eps * 4 , np .inf , np .inf ]
87
+ assert_array_equal (big_bad_ulp (in_arr ), out_arr )
88
+
89
+
72
90
BIG_FLOAT = np .float64
73
91
74
92
def test_round_trip ():
0 commit comments