# pypy / pypy / rlib / special_value.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169``` ```import math from pypy.rlib.rfloat import isnan, isinf, copysign # code to deal with special values (infinities, NaNs, ...) # # The special types can be: ST_NINF = 0 # negative infinity ST_NEG = 1 # negative finite number (nonzero) ST_NZERO = 2 # -0. ST_PZERO = 3 # +0. ST_POS = 4 # positive finite number (nonzero) ST_PINF = 5 # positive infinity ST_NAN = 6 # Not a Number def special_type(d): if isnan(d): return ST_NAN elif isinf(d): if d > 0.0: return ST_PINF else: return ST_NINF else: if d != 0.0: if d > 0.0: return ST_POS else: return ST_NEG else: if copysign(1., d) == 1.: return ST_PZERO else: return ST_NZERO P = math.pi P14 = 0.25 * math.pi P12 = 0.5 * math.pi P34 = 0.75 * math.pi INF = 1e200 * 1e200 N = INF / INF U = -9.5426319407711027e33 # unlikely value, used as placeholder Z = 0.0 # defined here instead of in the tuples below, because of a bug # in pypy releases < 1.5 NAN = N def build_table(lst): table = [] assert len(lst) == 49 it = iter(lst) for j in range(7): row = [] for i in range(7): (x, y) = it.next() row.append((x, y)) table.append(row) return table acos_special_values = build_table([ (P34,INF), (P,INF), (P,INF), (P,-INF), (P,-INF), (P34,-INF), (N,INF), (P12,INF), (U,U), (U,U), (U,U), (U,U), (P12,-INF), (N,N), (P12,INF), (U,U), (P12,Z), (P12,-Z), (U,U), (P12,-INF), (P12,N), (P12,INF), (U,U), (P12,Z), (P12,-Z), (U,U), (P12,-INF), (P12,N), (P12,INF), (U,U), (U,U), (U,U), (U,U), (P12,-INF), (N,N), (P14,INF), (Z,INF), (Z,INF), (Z,-INF), (Z,-INF), (P14,-INF), (N,INF), (N,INF), (N,N), (N,N), (N,N), (N,N), (N,-INF), (N,N), ]) acosh_special_values = build_table([ (INF,-P34), (INF,-P), (INF,-P), (INF,P), (INF,P), (INF,P34), (INF,N), (INF,-P12), (U,U), (U,U), (U,U), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (Z,-P12), (Z,P12), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (Z,-P12), (Z,P12), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (U,U), (U,U), (U,U), (INF,P12), (N,N), (INF,-P14), (INF,-Z), (INF,-Z), (INF,Z), (INF,Z), (INF,P14), (INF,N), (INF,N), (N,N), (N,N), (N,N), (N,N), (INF,N), (N,N), ]) asinh_special_values = build_table([ (-INF,-P14), (-INF,-Z), (-INF,-Z),(-INF,Z), (-INF,Z), (-INF,P14), (-INF,N), (-INF,-P12), (U,U), (U,U), (U,U), (U,U), (-INF,P12), (N,N), (-INF,-P12), (U,U), (-Z,-Z), (-Z,Z), (U,U), (-INF,P12), (N,N), (INF,-P12), (U,U), (Z,-Z), (Z,Z), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (U,U), (U,U), (U,U), (INF,P12), (N,N), (INF,-P14), (INF,-Z), (INF,-Z), (INF,Z), (INF,Z), (INF,P14), (INF,N), (INF,N), (N,N), (N,-Z), (N,Z), (N,N), (INF,N), (N,N), ]) atanh_special_values = build_table([ (-Z,-P12), (-Z,-P12), (-Z,-P12), (-Z,P12), (-Z,P12), (-Z,P12), (-Z,N), (-Z,-P12), (U,U), (U,U), (U,U), (U,U), (-Z,P12), (N,N), (-Z,-P12), (U,U), (-Z,-Z), (-Z,Z), (U,U), (-Z,P12), (-Z,N), (Z,-P12), (U,U), (Z,-Z), (Z,Z), (U,U), (Z,P12), (Z,N), (Z,-P12), (U,U), (U,U), (U,U), (U,U), (Z,P12), (N,N), (Z,-P12), (Z,-P12), (Z,-P12), (Z,P12), (Z,P12), (Z,P12), (Z,N), (Z,-P12), (N,N), (N,N), (N,N), (N,N), (Z,P12), (N,N), ]) log_special_values = build_table([ (INF,-P34), (INF,-P), (INF,-P), (INF,P), (INF,P), (INF,P34), (INF,N), (INF,-P12), (U,U), (U,U), (U,U), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (-INF,-P), (-INF,P), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (-INF,-Z), (-INF,Z), (U,U), (INF,P12), (N,N), (INF,-P12), (U,U), (U,U), (U,U), (U,U), (INF,P12), (N,N), (INF,-P14), (INF,-Z), (INF,-Z), (INF,Z), (INF,Z), (INF,P14), (INF,N), (INF,N), (N,N), (N,N), (N,N), (N,N), (INF,N), (N,N), ]) sqrt_special_values = build_table([ (INF,-INF), (Z,-INF), (Z,-INF), (Z,INF), (Z,INF), (INF,INF), (N,INF), (INF,-INF), (U,U), (U,U), (U,U), (U,U), (INF,INF), (N,N), (INF,-INF), (U,U), (Z,-Z), (Z,Z), (U,U), (INF,INF), (N,N), (INF,-INF), (U,U), (Z,-Z), (Z,Z), (U,U), (INF,INF), (N,N), (INF,-INF), (U,U), (U,U), (U,U), (U,U), (INF,INF), (N,N), (INF,-INF), (INF,-Z), (INF,-Z), (INF,Z), (INF,Z), (INF,INF), (INF,N), (INF,-INF), (N,N), (N,N), (N,N), (N,N), (INF,INF), (N,N), ]) exp_special_values = build_table([ (Z,Z), (U,U), (Z,-Z), (Z,Z), (U,U), (Z,Z), (Z,Z), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (N,N), (U,U), (1.,-Z), (1.,Z), (U,U), (N,N), (N,N), (N,N), (U,U), (1.,-Z), (1.,Z), (U,U), (N,N), (N,N), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (INF,N), (U,U), (INF,-Z), (INF,Z), (U,U), (INF,N), (INF,N), (N,N), (N,N), (N,-Z), (N,Z), (N,N), (N,N), (N,N), ]) cosh_special_values = build_table([ (INF,N), (U,U), (INF,Z), (INF,-Z), (U,U), (INF,N), (INF,N), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (N,Z), (U,U), (1.,Z), (1.,-Z), (U,U), (N,Z), (N,Z), (N,Z), (U,U), (1.,-Z), (1.,Z), (U,U), (N,Z), (N,Z), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (INF,N), (U,U), (INF,-Z), (INF,Z), (U,U), (INF,N), (INF,N), (N,N), (N,N), (N,Z), (N,Z), (N,N), (N,N), (N,N), ]) sinh_special_values = build_table([ (INF,N), (U,U), (-INF,-Z), (-INF,Z), (U,U), (INF,N), (INF,N), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (Z,N), (U,U), (-Z,-Z), (-Z,Z), (U,U), (Z,N), (Z,N), (Z,N), (U,U), (Z,-Z), (Z,Z), (U,U), (Z,N), (Z,N), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (INF,N), (U,U), (INF,-Z), (INF,Z), (U,U), (INF,N), (INF,N), (N,N), (N,N), (N,-Z), (N,Z), (N,N), (N,N), (N,N), ]) tanh_special_values = build_table([ (-1.,Z), (U,U), (-1.,-Z), (-1.,Z), (U,U), (-1.,Z), (-1.,Z), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (N,N), (U,U), (-Z,-Z), (-Z,Z), (U,U), (N,N), (N,N), (N,N), (U,U), (Z,-Z), (Z,Z), (U,U), (N,N), (N,N), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (1.,Z), (U,U), (1.,-Z), (1.,Z), (U,U), (1.,Z), (1.,Z), (N,N), (N,N), (N,-Z), (N,Z), (N,N), (N,N), (N,N), ]) rect_special_values = build_table([ (INF,N), (U,U), (-INF,Z), (-INF,-Z), (U,U), (INF,N), (INF,N), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (Z,Z), (U,U), (-Z,Z), (-Z,-Z), (U,U), (Z,Z), (Z,Z), (Z,Z), (U,U), (Z,-Z), (Z,Z), (U,U), (Z,Z), (Z,Z), (N,N), (U,U), (U,U), (U,U), (U,U), (N,N), (N,N), (INF,N), (U,U), (INF,-Z), (INF,Z), (U,U), (INF,N), (INF,N), (N,N), (N,N), (N,Z), (N,Z), (N,N), (N,N), (N,N), ]) assert copysign(1., acosh_special_values[5][2][1]) == -1. ```