test_half.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import platform
  2. import pytest
  3. import numpy as np
  4. from numpy import uint16, float16, float32, float64
  5. from numpy.testing import assert_, assert_equal
  6. def assert_raises_fpe(strmatch, callable, *args, **kwargs):
  7. try:
  8. callable(*args, **kwargs)
  9. except FloatingPointError as exc:
  10. assert_(str(exc).find(strmatch) >= 0,
  11. "Did not raise floating point %s error" % strmatch)
  12. else:
  13. assert_(False,
  14. "Did not raise floating point %s error" % strmatch)
  15. class TestHalf:
  16. def setup(self):
  17. # An array of all possible float16 values
  18. self.all_f16 = np.arange(0x10000, dtype=uint16)
  19. self.all_f16.dtype = float16
  20. self.all_f32 = np.array(self.all_f16, dtype=float32)
  21. self.all_f64 = np.array(self.all_f16, dtype=float64)
  22. # An array of all non-NaN float16 values, in sorted order
  23. self.nonan_f16 = np.concatenate(
  24. (np.arange(0xfc00, 0x7fff, -1, dtype=uint16),
  25. np.arange(0x0000, 0x7c01, 1, dtype=uint16)))
  26. self.nonan_f16.dtype = float16
  27. self.nonan_f32 = np.array(self.nonan_f16, dtype=float32)
  28. self.nonan_f64 = np.array(self.nonan_f16, dtype=float64)
  29. # An array of all finite float16 values, in sorted order
  30. self.finite_f16 = self.nonan_f16[1:-1]
  31. self.finite_f32 = self.nonan_f32[1:-1]
  32. self.finite_f64 = self.nonan_f64[1:-1]
  33. def test_half_conversions(self):
  34. """Checks that all 16-bit values survive conversion
  35. to/from 32-bit and 64-bit float"""
  36. # Because the underlying routines preserve the NaN bits, every
  37. # value is preserved when converting to/from other floats.
  38. # Convert from float32 back to float16
  39. b = np.array(self.all_f32, dtype=float16)
  40. assert_equal(self.all_f16.view(dtype=uint16),
  41. b.view(dtype=uint16))
  42. # Convert from float64 back to float16
  43. b = np.array(self.all_f64, dtype=float16)
  44. assert_equal(self.all_f16.view(dtype=uint16),
  45. b.view(dtype=uint16))
  46. # Convert float16 to longdouble and back
  47. # This doesn't necessarily preserve the extra NaN bits,
  48. # so exclude NaNs.
  49. a_ld = np.array(self.nonan_f16, dtype=np.longdouble)
  50. b = np.array(a_ld, dtype=float16)
  51. assert_equal(self.nonan_f16.view(dtype=uint16),
  52. b.view(dtype=uint16))
  53. # Check the range for which all integers can be represented
  54. i_int = np.arange(-2048, 2049)
  55. i_f16 = np.array(i_int, dtype=float16)
  56. j = np.array(i_f16, dtype=int)
  57. assert_equal(i_int, j)
  58. @pytest.mark.parametrize("offset", [None, "up", "down"])
  59. @pytest.mark.parametrize("shift", [None, "up", "down"])
  60. @pytest.mark.parametrize("float_t", [np.float32, np.float64])
  61. def test_half_conversion_rounding(self, float_t, shift, offset):
  62. # Assumes that round to even is used during casting.
  63. max_pattern = np.float16(np.finfo(np.float16).max).view(np.uint16)
  64. # Test all (positive) finite numbers, denormals are most interesting
  65. # however:
  66. f16s_patterns = np.arange(0, max_pattern+1, dtype=np.uint16)
  67. f16s_float = f16s_patterns.view(np.float16).astype(float_t)
  68. # Shift the values by half a bit up or a down (or do not shift),
  69. if shift == "up":
  70. f16s_float = 0.5 * (f16s_float[:-1] + f16s_float[1:])[1:]
  71. elif shift == "down":
  72. f16s_float = 0.5 * (f16s_float[:-1] + f16s_float[1:])[:-1]
  73. else:
  74. f16s_float = f16s_float[1:-1]
  75. # Increase the float by a minimal value:
  76. if offset == "up":
  77. f16s_float = np.nextafter(f16s_float, float_t(1e50))
  78. elif offset == "down":
  79. f16s_float = np.nextafter(f16s_float, float_t(-1e50))
  80. # Convert back to float16 and its bit pattern:
  81. res_patterns = f16s_float.astype(np.float16).view(np.uint16)
  82. # The above calculations tries the original values, or the exact
  83. # mid points between the float16 values. It then further offsets them
  84. # by as little as possible. If no offset occurs, "round to even"
  85. # logic will be necessary, an arbitrarily small offset should cause
  86. # normal up/down rounding always.
  87. # Calculate the expected pattern:
  88. cmp_patterns = f16s_patterns[1:-1].copy()
  89. if shift == "down" and offset != "up":
  90. shift_pattern = -1
  91. elif shift == "up" and offset != "down":
  92. shift_pattern = 1
  93. else:
  94. # There cannot be a shift, either shift is None, so all rounding
  95. # will go back to original, or shift is reduced by offset too much.
  96. shift_pattern = 0
  97. # If rounding occurs, is it normal rounding or round to even?
  98. if offset is None:
  99. # Round to even occurs, modify only non-even, cast to allow + (-1)
  100. cmp_patterns[0::2].view(np.int16)[...] += shift_pattern
  101. else:
  102. cmp_patterns.view(np.int16)[...] += shift_pattern
  103. assert_equal(res_patterns, cmp_patterns)
  104. @pytest.mark.parametrize(["float_t", "uint_t", "bits"],
  105. [(np.float32, np.uint32, 23),
  106. (np.float64, np.uint64, 52)])
  107. def test_half_conversion_denormal_round_even(self, float_t, uint_t, bits):
  108. # Test specifically that all bits are considered when deciding
  109. # whether round to even should occur (i.e. no bits are lost at the
  110. # end. Compare also gh-12721. The most bits can get lost for the
  111. # smallest denormal:
  112. smallest_value = np.uint16(1).view(np.float16).astype(float_t)
  113. assert smallest_value == 2**-24
  114. # Will be rounded to zero based on round to even rule:
  115. rounded_to_zero = smallest_value / float_t(2)
  116. assert rounded_to_zero.astype(np.float16) == 0
  117. # The significand will be all 0 for the float_t, test that we do not
  118. # lose the lower ones of these:
  119. for i in range(bits):
  120. # slightly increasing the value should make it round up:
  121. larger_pattern = rounded_to_zero.view(uint_t) | uint_t(1 << i)
  122. larger_value = larger_pattern.view(float_t)
  123. assert larger_value.astype(np.float16) == smallest_value
  124. def test_nans_infs(self):
  125. with np.errstate(all='ignore'):
  126. # Check some of the ufuncs
  127. assert_equal(np.isnan(self.all_f16), np.isnan(self.all_f32))
  128. assert_equal(np.isinf(self.all_f16), np.isinf(self.all_f32))
  129. assert_equal(np.isfinite(self.all_f16), np.isfinite(self.all_f32))
  130. assert_equal(np.signbit(self.all_f16), np.signbit(self.all_f32))
  131. assert_equal(np.spacing(float16(65504)), np.inf)
  132. # Check comparisons of all values with NaN
  133. nan = float16(np.nan)
  134. assert_(not (self.all_f16 == nan).any())
  135. assert_(not (nan == self.all_f16).any())
  136. assert_((self.all_f16 != nan).all())
  137. assert_((nan != self.all_f16).all())
  138. assert_(not (self.all_f16 < nan).any())
  139. assert_(not (nan < self.all_f16).any())
  140. assert_(not (self.all_f16 <= nan).any())
  141. assert_(not (nan <= self.all_f16).any())
  142. assert_(not (self.all_f16 > nan).any())
  143. assert_(not (nan > self.all_f16).any())
  144. assert_(not (self.all_f16 >= nan).any())
  145. assert_(not (nan >= self.all_f16).any())
  146. def test_half_values(self):
  147. """Confirms a small number of known half values"""
  148. a = np.array([1.0, -1.0,
  149. 2.0, -2.0,
  150. 0.0999755859375, 0.333251953125, # 1/10, 1/3
  151. 65504, -65504, # Maximum magnitude
  152. 2.0**(-14), -2.0**(-14), # Minimum normal
  153. 2.0**(-24), -2.0**(-24), # Minimum subnormal
  154. 0, -1/1e1000, # Signed zeros
  155. np.inf, -np.inf])
  156. b = np.array([0x3c00, 0xbc00,
  157. 0x4000, 0xc000,
  158. 0x2e66, 0x3555,
  159. 0x7bff, 0xfbff,
  160. 0x0400, 0x8400,
  161. 0x0001, 0x8001,
  162. 0x0000, 0x8000,
  163. 0x7c00, 0xfc00], dtype=uint16)
  164. b.dtype = float16
  165. assert_equal(a, b)
  166. def test_half_rounding(self):
  167. """Checks that rounding when converting to half is correct"""
  168. a = np.array([2.0**-25 + 2.0**-35, # Rounds to minimum subnormal
  169. 2.0**-25, # Underflows to zero (nearest even mode)
  170. 2.0**-26, # Underflows to zero
  171. 1.0+2.0**-11 + 2.0**-16, # rounds to 1.0+2**(-10)
  172. 1.0+2.0**-11, # rounds to 1.0 (nearest even mode)
  173. 1.0+2.0**-12, # rounds to 1.0
  174. 65519, # rounds to 65504
  175. 65520], # rounds to inf
  176. dtype=float64)
  177. rounded = [2.0**-24,
  178. 0.0,
  179. 0.0,
  180. 1.0+2.0**(-10),
  181. 1.0,
  182. 1.0,
  183. 65504,
  184. np.inf]
  185. # Check float64->float16 rounding
  186. b = np.array(a, dtype=float16)
  187. assert_equal(b, rounded)
  188. # Check float32->float16 rounding
  189. a = np.array(a, dtype=float32)
  190. b = np.array(a, dtype=float16)
  191. assert_equal(b, rounded)
  192. def test_half_correctness(self):
  193. """Take every finite float16, and check the casting functions with
  194. a manual conversion."""
  195. # Create an array of all finite float16s
  196. a_bits = self.finite_f16.view(dtype=uint16)
  197. # Convert to 64-bit float manually
  198. a_sgn = (-1.0)**((a_bits & 0x8000) >> 15)
  199. a_exp = np.array((a_bits & 0x7c00) >> 10, dtype=np.int32) - 15
  200. a_man = (a_bits & 0x03ff) * 2.0**(-10)
  201. # Implicit bit of normalized floats
  202. a_man[a_exp != -15] += 1
  203. # Denormalized exponent is -14
  204. a_exp[a_exp == -15] = -14
  205. a_manual = a_sgn * a_man * 2.0**a_exp
  206. a32_fail = np.nonzero(self.finite_f32 != a_manual)[0]
  207. if len(a32_fail) != 0:
  208. bad_index = a32_fail[0]
  209. assert_equal(self.finite_f32, a_manual,
  210. "First non-equal is half value %x -> %g != %g" %
  211. (self.finite_f16[bad_index],
  212. self.finite_f32[bad_index],
  213. a_manual[bad_index]))
  214. a64_fail = np.nonzero(self.finite_f64 != a_manual)[0]
  215. if len(a64_fail) != 0:
  216. bad_index = a64_fail[0]
  217. assert_equal(self.finite_f64, a_manual,
  218. "First non-equal is half value %x -> %g != %g" %
  219. (self.finite_f16[bad_index],
  220. self.finite_f64[bad_index],
  221. a_manual[bad_index]))
  222. def test_half_ordering(self):
  223. """Make sure comparisons are working right"""
  224. # All non-NaN float16 values in reverse order
  225. a = self.nonan_f16[::-1].copy()
  226. # 32-bit float copy
  227. b = np.array(a, dtype=float32)
  228. # Should sort the same
  229. a.sort()
  230. b.sort()
  231. assert_equal(a, b)
  232. # Comparisons should work
  233. assert_((a[:-1] <= a[1:]).all())
  234. assert_(not (a[:-1] > a[1:]).any())
  235. assert_((a[1:] >= a[:-1]).all())
  236. assert_(not (a[1:] < a[:-1]).any())
  237. # All != except for +/-0
  238. assert_equal(np.nonzero(a[:-1] < a[1:])[0].size, a.size-2)
  239. assert_equal(np.nonzero(a[1:] > a[:-1])[0].size, a.size-2)
  240. def test_half_funcs(self):
  241. """Test the various ArrFuncs"""
  242. # fill
  243. assert_equal(np.arange(10, dtype=float16),
  244. np.arange(10, dtype=float32))
  245. # fillwithscalar
  246. a = np.zeros((5,), dtype=float16)
  247. a.fill(1)
  248. assert_equal(a, np.ones((5,), dtype=float16))
  249. # nonzero and copyswap
  250. a = np.array([0, 0, -1, -1/1e20, 0, 2.0**-24, 7.629e-6], dtype=float16)
  251. assert_equal(a.nonzero()[0],
  252. [2, 5, 6])
  253. a = a.byteswap().newbyteorder()
  254. assert_equal(a.nonzero()[0],
  255. [2, 5, 6])
  256. # dot
  257. a = np.arange(0, 10, 0.5, dtype=float16)
  258. b = np.ones((20,), dtype=float16)
  259. assert_equal(np.dot(a, b),
  260. 95)
  261. # argmax
  262. a = np.array([0, -np.inf, -2, 0.5, 12.55, 7.3, 2.1, 12.4], dtype=float16)
  263. assert_equal(a.argmax(),
  264. 4)
  265. a = np.array([0, -np.inf, -2, np.inf, 12.55, np.nan, 2.1, 12.4], dtype=float16)
  266. assert_equal(a.argmax(),
  267. 5)
  268. # getitem
  269. a = np.arange(10, dtype=float16)
  270. for i in range(10):
  271. assert_equal(a.item(i), i)
  272. def test_spacing_nextafter(self):
  273. """Test np.spacing and np.nextafter"""
  274. # All non-negative finite #'s
  275. a = np.arange(0x7c00, dtype=uint16)
  276. hinf = np.array((np.inf,), dtype=float16)
  277. hnan = np.array((np.nan,), dtype=float16)
  278. a_f16 = a.view(dtype=float16)
  279. assert_equal(np.spacing(a_f16[:-1]), a_f16[1:]-a_f16[:-1])
  280. assert_equal(np.nextafter(a_f16[:-1], hinf), a_f16[1:])
  281. assert_equal(np.nextafter(a_f16[0], -hinf), -a_f16[1])
  282. assert_equal(np.nextafter(a_f16[1:], -hinf), a_f16[:-1])
  283. assert_equal(np.nextafter(hinf, a_f16), a_f16[-1])
  284. assert_equal(np.nextafter(-hinf, a_f16), -a_f16[-1])
  285. assert_equal(np.nextafter(hinf, hinf), hinf)
  286. assert_equal(np.nextafter(hinf, -hinf), a_f16[-1])
  287. assert_equal(np.nextafter(-hinf, hinf), -a_f16[-1])
  288. assert_equal(np.nextafter(-hinf, -hinf), -hinf)
  289. assert_equal(np.nextafter(a_f16, hnan), hnan[0])
  290. assert_equal(np.nextafter(hnan, a_f16), hnan[0])
  291. assert_equal(np.nextafter(hnan, hnan), hnan)
  292. assert_equal(np.nextafter(hinf, hnan), hnan)
  293. assert_equal(np.nextafter(hnan, hinf), hnan)
  294. # switch to negatives
  295. a |= 0x8000
  296. assert_equal(np.spacing(a_f16[0]), np.spacing(a_f16[1]))
  297. assert_equal(np.spacing(a_f16[1:]), a_f16[:-1]-a_f16[1:])
  298. assert_equal(np.nextafter(a_f16[0], hinf), -a_f16[1])
  299. assert_equal(np.nextafter(a_f16[1:], hinf), a_f16[:-1])
  300. assert_equal(np.nextafter(a_f16[:-1], -hinf), a_f16[1:])
  301. assert_equal(np.nextafter(hinf, a_f16), -a_f16[-1])
  302. assert_equal(np.nextafter(-hinf, a_f16), a_f16[-1])
  303. assert_equal(np.nextafter(a_f16, hnan), hnan[0])
  304. assert_equal(np.nextafter(hnan, a_f16), hnan[0])
  305. def test_half_ufuncs(self):
  306. """Test the various ufuncs"""
  307. a = np.array([0, 1, 2, 4, 2], dtype=float16)
  308. b = np.array([-2, 5, 1, 4, 3], dtype=float16)
  309. c = np.array([0, -1, -np.inf, np.nan, 6], dtype=float16)
  310. assert_equal(np.add(a, b), [-2, 6, 3, 8, 5])
  311. assert_equal(np.subtract(a, b), [2, -4, 1, 0, -1])
  312. assert_equal(np.multiply(a, b), [0, 5, 2, 16, 6])
  313. assert_equal(np.divide(a, b), [0, 0.199951171875, 2, 1, 0.66650390625])
  314. assert_equal(np.equal(a, b), [False, False, False, True, False])
  315. assert_equal(np.not_equal(a, b), [True, True, True, False, True])
  316. assert_equal(np.less(a, b), [False, True, False, False, True])
  317. assert_equal(np.less_equal(a, b), [False, True, False, True, True])
  318. assert_equal(np.greater(a, b), [True, False, True, False, False])
  319. assert_equal(np.greater_equal(a, b), [True, False, True, True, False])
  320. assert_equal(np.logical_and(a, b), [False, True, True, True, True])
  321. assert_equal(np.logical_or(a, b), [True, True, True, True, True])
  322. assert_equal(np.logical_xor(a, b), [True, False, False, False, False])
  323. assert_equal(np.logical_not(a), [True, False, False, False, False])
  324. assert_equal(np.isnan(c), [False, False, False, True, False])
  325. assert_equal(np.isinf(c), [False, False, True, False, False])
  326. assert_equal(np.isfinite(c), [True, True, False, False, True])
  327. assert_equal(np.signbit(b), [True, False, False, False, False])
  328. assert_equal(np.copysign(b, a), [2, 5, 1, 4, 3])
  329. assert_equal(np.maximum(a, b), [0, 5, 2, 4, 3])
  330. x = np.maximum(b, c)
  331. assert_(np.isnan(x[3]))
  332. x[3] = 0
  333. assert_equal(x, [0, 5, 1, 0, 6])
  334. assert_equal(np.minimum(a, b), [-2, 1, 1, 4, 2])
  335. x = np.minimum(b, c)
  336. assert_(np.isnan(x[3]))
  337. x[3] = 0
  338. assert_equal(x, [-2, -1, -np.inf, 0, 3])
  339. assert_equal(np.fmax(a, b), [0, 5, 2, 4, 3])
  340. assert_equal(np.fmax(b, c), [0, 5, 1, 4, 6])
  341. assert_equal(np.fmin(a, b), [-2, 1, 1, 4, 2])
  342. assert_equal(np.fmin(b, c), [-2, -1, -np.inf, 4, 3])
  343. assert_equal(np.floor_divide(a, b), [0, 0, 2, 1, 0])
  344. assert_equal(np.remainder(a, b), [0, 1, 0, 0, 2])
  345. assert_equal(np.divmod(a, b), ([0, 0, 2, 1, 0], [0, 1, 0, 0, 2]))
  346. assert_equal(np.square(b), [4, 25, 1, 16, 9])
  347. assert_equal(np.reciprocal(b), [-0.5, 0.199951171875, 1, 0.25, 0.333251953125])
  348. assert_equal(np.ones_like(b), [1, 1, 1, 1, 1])
  349. assert_equal(np.conjugate(b), b)
  350. assert_equal(np.absolute(b), [2, 5, 1, 4, 3])
  351. assert_equal(np.negative(b), [2, -5, -1, -4, -3])
  352. assert_equal(np.positive(b), b)
  353. assert_equal(np.sign(b), [-1, 1, 1, 1, 1])
  354. assert_equal(np.modf(b), ([0, 0, 0, 0, 0], b))
  355. assert_equal(np.frexp(b), ([-0.5, 0.625, 0.5, 0.5, 0.75], [2, 3, 1, 3, 2]))
  356. assert_equal(np.ldexp(b, [0, 1, 2, 4, 2]), [-2, 10, 4, 64, 12])
  357. def test_half_coercion(self):
  358. """Test that half gets coerced properly with the other types"""
  359. a16 = np.array((1,), dtype=float16)
  360. a32 = np.array((1,), dtype=float32)
  361. b16 = float16(1)
  362. b32 = float32(1)
  363. assert_equal(np.power(a16, 2).dtype, float16)
  364. assert_equal(np.power(a16, 2.0).dtype, float16)
  365. assert_equal(np.power(a16, b16).dtype, float16)
  366. assert_equal(np.power(a16, b32).dtype, float16)
  367. assert_equal(np.power(a16, a16).dtype, float16)
  368. assert_equal(np.power(a16, a32).dtype, float32)
  369. assert_equal(np.power(b16, 2).dtype, float64)
  370. assert_equal(np.power(b16, 2.0).dtype, float64)
  371. assert_equal(np.power(b16, b16).dtype, float16)
  372. assert_equal(np.power(b16, b32).dtype, float32)
  373. assert_equal(np.power(b16, a16).dtype, float16)
  374. assert_equal(np.power(b16, a32).dtype, float32)
  375. assert_equal(np.power(a32, a16).dtype, float32)
  376. assert_equal(np.power(a32, b16).dtype, float32)
  377. assert_equal(np.power(b32, a16).dtype, float16)
  378. assert_equal(np.power(b32, b16).dtype, float32)
  379. @pytest.mark.skipif(platform.machine() == "armv5tel",
  380. reason="See gh-413.")
  381. def test_half_fpe(self):
  382. with np.errstate(all='raise'):
  383. sx16 = np.array((1e-4,), dtype=float16)
  384. bx16 = np.array((1e4,), dtype=float16)
  385. sy16 = float16(1e-4)
  386. by16 = float16(1e4)
  387. # Underflow errors
  388. assert_raises_fpe('underflow', lambda a, b:a*b, sx16, sx16)
  389. assert_raises_fpe('underflow', lambda a, b:a*b, sx16, sy16)
  390. assert_raises_fpe('underflow', lambda a, b:a*b, sy16, sx16)
  391. assert_raises_fpe('underflow', lambda a, b:a*b, sy16, sy16)
  392. assert_raises_fpe('underflow', lambda a, b:a/b, sx16, bx16)
  393. assert_raises_fpe('underflow', lambda a, b:a/b, sx16, by16)
  394. assert_raises_fpe('underflow', lambda a, b:a/b, sy16, bx16)
  395. assert_raises_fpe('underflow', lambda a, b:a/b, sy16, by16)
  396. assert_raises_fpe('underflow', lambda a, b:a/b,
  397. float16(2.**-14), float16(2**11))
  398. assert_raises_fpe('underflow', lambda a, b:a/b,
  399. float16(-2.**-14), float16(2**11))
  400. assert_raises_fpe('underflow', lambda a, b:a/b,
  401. float16(2.**-14+2**-24), float16(2))
  402. assert_raises_fpe('underflow', lambda a, b:a/b,
  403. float16(-2.**-14-2**-24), float16(2))
  404. assert_raises_fpe('underflow', lambda a, b:a/b,
  405. float16(2.**-14+2**-23), float16(4))
  406. # Overflow errors
  407. assert_raises_fpe('overflow', lambda a, b:a*b, bx16, bx16)
  408. assert_raises_fpe('overflow', lambda a, b:a*b, bx16, by16)
  409. assert_raises_fpe('overflow', lambda a, b:a*b, by16, bx16)
  410. assert_raises_fpe('overflow', lambda a, b:a*b, by16, by16)
  411. assert_raises_fpe('overflow', lambda a, b:a/b, bx16, sx16)
  412. assert_raises_fpe('overflow', lambda a, b:a/b, bx16, sy16)
  413. assert_raises_fpe('overflow', lambda a, b:a/b, by16, sx16)
  414. assert_raises_fpe('overflow', lambda a, b:a/b, by16, sy16)
  415. assert_raises_fpe('overflow', lambda a, b:a+b,
  416. float16(65504), float16(17))
  417. assert_raises_fpe('overflow', lambda a, b:a-b,
  418. float16(-65504), float16(17))
  419. assert_raises_fpe('overflow', np.nextafter, float16(65504), float16(np.inf))
  420. assert_raises_fpe('overflow', np.nextafter, float16(-65504), float16(-np.inf))
  421. assert_raises_fpe('overflow', np.spacing, float16(65504))
  422. # Invalid value errors
  423. assert_raises_fpe('invalid', np.divide, float16(np.inf), float16(np.inf))
  424. assert_raises_fpe('invalid', np.spacing, float16(np.inf))
  425. assert_raises_fpe('invalid', np.spacing, float16(np.nan))
  426. # These should not raise
  427. float16(65472)+float16(32)
  428. float16(2**-13)/float16(2)
  429. float16(2**-14)/float16(2**10)
  430. np.spacing(float16(-65504))
  431. np.nextafter(float16(65504), float16(-np.inf))
  432. np.nextafter(float16(-65504), float16(np.inf))
  433. np.nextafter(float16(np.inf), float16(0))
  434. np.nextafter(float16(-np.inf), float16(0))
  435. np.nextafter(float16(0), float16(np.nan))
  436. np.nextafter(float16(np.nan), float16(0))
  437. float16(2**-14)/float16(2**10)
  438. float16(-2**-14)/float16(2**10)
  439. float16(2**-14+2**-23)/float16(2)
  440. float16(-2**-14-2**-23)/float16(2)
  441. def test_half_array_interface(self):
  442. """Test that half is compatible with __array_interface__"""
  443. class Dummy:
  444. pass
  445. a = np.ones((1,), dtype=float16)
  446. b = Dummy()
  447. b.__array_interface__ = a.__array_interface__
  448. c = np.array(b)
  449. assert_(c.dtype == float16)
  450. assert_equal(a, c)