@@ -136,3 +136,76 @@ def test_int_abs():
136
136
assert_equal (int_abs (mx ), mx )
137
137
assert_equal (int_abs (mn ), e_mn )
138
138
assert_array_equal (int_abs (in_arr ), [e_mn , mx ])
139
+
140
+
141
+ def test_able_int_type ():
142
+ # The integer type cabable of containing values
143
+ for vals , exp_out in (
144
+ ([0 , 1 ], np .uint8 ),
145
+ ([0 , 255 ], np .uint8 ),
146
+ ([- 1 , 1 ], np .int8 ),
147
+ ([0 , 256 ], np .uint16 ),
148
+ ([- 1 , 128 ], np .int16 ),
149
+ ([0.1 , 1 ], None ),
150
+ ([0 , 2 ** 16 ], np .uint32 ),
151
+ ([- 1 , 2 ** 15 ], np .int32 ),
152
+ ([0 , 2 ** 32 ], np .uint64 ),
153
+ ([- 1 , 2 ** 31 ], np .int64 ),
154
+ ([- 1 , 2 ** 64 - 1 ], None ),
155
+ ([0 , 2 ** 64 - 1 ], np .uint64 ),
156
+ ([0 , 2 ** 64 ], None )):
157
+ assert_equal (able_int_type (vals ), exp_out )
158
+
159
+
160
+ def test_able_casting ():
161
+ # Check the able_int_type function guesses numpy out type
162
+ types = np .sctypes ['int' ] + np .sctypes ['uint' ]
163
+ for in_type in types :
164
+ in_info = np .iinfo (in_type )
165
+ in_mn , in_mx = in_info .min , in_info .max
166
+ A = np .zeros ((1 ,), dtype = in_type )
167
+ for out_type in types :
168
+ out_info = np .iinfo (out_type )
169
+ out_mn , out_mx = out_info .min , out_info .max
170
+ B = np .zeros ((1 ,), dtype = out_type )
171
+ ApBt = (A + B ).dtype .type
172
+ able_type = able_int_type ([in_mn , in_mx , out_mn , out_mx ])
173
+ if able_type is None :
174
+ assert_equal (ApBt , np .float64 )
175
+ continue
176
+ # Use str for comparison to avoid int32/64 vs intp comparison
177
+ # failures
178
+ assert_equal (np .dtype (ApBt ).str , np .dtype (able_type ).str )
179
+
180
+
181
+ def test_best_float ():
182
+ # Finds the most capable floating point type
183
+ # The only time this isn't np.longdouble is when np.longdouble has float64
184
+ # precision.
185
+ best = best_float ()
186
+ end_of_ints = np .float64 (2 ** 53 )
187
+ # float64 has continuous integers up to 2**53
188
+ assert_equal (end_of_ints , end_of_ints + 1 )
189
+ # longdouble may have more, but not on 32 bit windows, at least
190
+ end_of_ints = np .longdouble (2 ** 53 )
191
+ if end_of_ints == (end_of_ints + 1 ): # off continuous integers
192
+ assert_equal (best , np .float64 )
193
+ else :
194
+ assert_equal (best , np .longdouble )
195
+
196
+
197
+ def test_eps ():
198
+ assert_equal (eps (), np .finfo (np .float64 ).eps )
199
+ assert_equal (eps (1.0 ), np .finfo (np .float64 ).eps )
200
+ assert_equal (eps (np .float32 (1.0 )), np .finfo (np .float32 ).eps )
201
+ assert_equal (eps (np .float32 (1.999 )), np .finfo (np .float32 ).eps )
202
+ # Integers always return 1
203
+ assert_equal (eps (1 ), 1 )
204
+ assert_equal (eps (2 ** 63 - 1 ), 1 )
205
+ # negative / positive same
206
+ assert_equal (eps (- 1 ), 1 )
207
+ assert_equal (eps (7.999 ), eps (4.0 ))
208
+ assert_equal (eps (- 7.999 ), eps (4.0 ))
209
+ assert_equal (eps (np .float64 (2 ** 54 - 2 )), 2 )
210
+ assert_equal (eps (np .float64 (2 ** 54 )), 4 )
211
+ assert_equal (eps (np .float64 (2 ** 54 )), 4 )
0 commit comments