|
2 | 2 | """
|
3 | 3 | import numpy as np
|
4 | 4 |
|
5 |
| -from ..casting import floor_exact, flt2nmant, as_int, FloatingError |
| 5 | +from ..casting import (floor_exact, flt2nmant, as_int, FloatingError, |
| 6 | + int_to_float, floor_log2) |
6 | 7 |
|
7 | 8 | from nose import SkipTest
|
8 | 9 | from nose.tools import assert_equal, assert_raises
|
@@ -36,15 +37,64 @@ def test_as_int():
|
36 | 37 | assert_equal(as_int(-2.1, False), -2)
|
37 | 38 | v = np.longdouble(2**64)
|
38 | 39 | assert_equal(as_int(v), 2**64)
|
39 |
| - # Have all long doubles got this precision? Windows 32-bit longdouble |
40 |
| - # appears to have 52 bit precision, but we avoid that by checking for known |
41 |
| - # precisions that are less than that required |
| 40 | + # Have all long doubles got 63+1 binary bits of precision? Windows 32-bit |
| 41 | + # longdouble appears to have 52 bit precision, but we avoid that by checking |
| 42 | + # for known precisions that are less than that required |
42 | 43 | try:
|
43 | 44 | nmant = flt2nmant(np.longdouble)
|
44 | 45 | except FloatingError:
|
45 |
| - nmant = None # Unknown precision, test and hope |
46 |
| - if nmant is None or nmant >= 63: |
47 |
| - assert_equal(as_int(v+2), 2**64+2) |
| 46 | + nmant = 63 # Unknown precision, let's hope it's at least 63 |
| 47 | + v = np.longdouble(2) ** (nmant + 1) - 1 |
| 48 | + assert_equal(as_int(v), 2**(nmant + 1) -1) |
| 49 | + # Check for predictable overflow |
| 50 | + nexp64 = floor_log2(np.finfo(np.float64).max) |
| 51 | + val = np.longdouble(2**nexp64) * 2 # outside float64 range |
| 52 | + assert_raises(OverflowError, as_int, val) |
| 53 | + assert_raises(OverflowError, as_int, -val) |
| 54 | + |
| 55 | + |
| 56 | +def test_int_to_float(): |
| 57 | + # Concert python integer to floating point |
| 58 | + # Standard float types just return cast value |
| 59 | + for ie3 in IEEE_floats: |
| 60 | + nmant = flt2nmant(ie3) |
| 61 | + for p in range(nmant + 3): |
| 62 | + i = 2**p+1 |
| 63 | + assert_equal(int_to_float(i, ie3), ie3(i)) |
| 64 | + assert_equal(int_to_float(-i, ie3), ie3(-i)) |
| 65 | + # IEEEs in this case are binary formats only |
| 66 | + nexp = floor_log2(np.finfo(ie3).max) |
| 67 | + # Values too large for the format |
| 68 | + smn, smx = -2**(nexp+1), 2**(nexp+1) |
| 69 | + if ie3 is np.float64: |
| 70 | + assert_raises(OverflowError, int_to_float, smn, ie3) |
| 71 | + assert_raises(OverflowError, int_to_float, smx, ie3) |
| 72 | + else: |
| 73 | + assert_equal(int_to_float(smn, ie3), ie3(smn)) |
| 74 | + assert_equal(int_to_float(smx, ie3), ie3(smx)) |
| 75 | + # Longdoubles do better than int, we hope |
| 76 | + LD = np.longdouble |
| 77 | + # up to integer precision of float64 nmant, we get the same result as for |
| 78 | + # casting directly |
| 79 | + for p in range(flt2nmant(np.float64)+2): # implicit |
| 80 | + i = 2**p-1 |
| 81 | + assert_equal(int_to_float(i, LD), LD(i)) |
| 82 | + assert_equal(int_to_float(-i, LD), LD(-i)) |
| 83 | + # Above max of float64, we're hosed |
| 84 | + nexp64 = floor_log2(np.finfo(np.float64).max) |
| 85 | + smn64, smx64 = -2**(nexp64+1), 2**(nexp64+1) |
| 86 | + # The algorithm here implemented goes through float64, so supermax and |
| 87 | + # supermin will cause overflow errors |
| 88 | + assert_raises(OverflowError, int_to_float, smn64, LD) |
| 89 | + assert_raises(OverflowError, int_to_float, smx64, LD) |
| 90 | + try: |
| 91 | + nmant = flt2nmant(np.longdouble) |
| 92 | + except FloatingError: # don't know where to test |
| 93 | + return |
| 94 | + # Assuming nmant is greater than that for float64, test we recover precision |
| 95 | + i = 2**(nmant+1)-1 |
| 96 | + assert_equal(as_int(int_to_float(i, LD)), i) |
| 97 | + assert_equal(as_int(int_to_float(-i, LD)), -i) |
48 | 98 |
|
49 | 99 |
|
50 | 100 | def test_floor_exact_16():
|
|
0 commit comments