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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
# mode: run
# tag: warnings
from __future__ import print_function
cimport cython
def pow_double_double(double a, double b, delta):
"""
>>> pow_double_double(2, 2, 1e-15)
soft double complex float
>>> pow_double_double(4, 0.5, 1e-15)
soft double complex float
>>> pow_double_double(-4, 0.5, 1e-15)
soft double complex complex
"""
c = a**b
# print out the Cython type, and the coerced type
print(cython.typeof(c), type(c).__name__)
object_c = (<object>a)**(<object>b)
assert abs((c/object_c) - 1) < delta
@cython.cpow(True)
def pow_double_double_cpow(double a, double b, delta=None):
"""
>>> pow_double_double_cpow(2, 2, 1e-15)
double float
>>> pow_double_double_cpow(4, 0.5, 1e-15)
double float
>>> x = pow_double_double_cpow(-4, 0.5)
double float
>>> x == x # is nan
False
"""
c = a**b
# print out the Cython type, and the coerced type
print(cython.typeof(c), type(c).__name__)
if delta is not None:
object_c = (<object>a)**(<object>b)
assert abs((c/object_c) - 1) < delta
else:
return c
cdef cfunc_taking_double(double x):
return x
def pow_double_double_coerced_directly(double a, double b):
"""
>>> pow_double_double_coerced_directly(2, 2)
8.0
>>> x = pow_double_double_coerced_directly(-2, 0.5)
>>> x == x # nan
False
"""
# Because we're assigning directly to a double assume 'cpow'
# but warn.
cdef double c = a**b
return cfunc_taking_double(a**b) + c
def pow_double_int(double a, int b):
"""
# a few variations of 'double**int'. In all cases
# Cython should realise that the result can't be complex
# and avoid going through the soft complex type
>>> pow_double_int(5, 2)
double
double
double
double
double
"""
c1 = a**b
c2 = a**2.0
c3 = a**-2.0
c4 = a**5
c5 = a**-5
print(cython.typeof(c1))
print(cython.typeof(c2))
print(cython.typeof(c3))
print(cython.typeof(c4))
print(cython.typeof(c5))
def soft_complex_coerced_to_double(double a, double b):
"""
>>> soft_complex_coerced_to_double(2, 2)
4.0
>>> soft_complex_coerced_to_double(-2, 0.25)
Traceback (most recent call last):
...
TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
"""
c = a**b
assert cython.typeof(c) == "soft double complex"
cdef double d = c # will raise if complex
return d
def soft_complex_coerced_to_complex(double a, double b):
"""
>>> soft_complex_coerced_to_complex(2, 2)
(4+0j)
>>> x = soft_complex_coerced_to_complex(-1, 0.5)
>>> abs(x.real) < 1e-15
True
>>> abs(x.imag - 1) < 1e-15
True
"""
# This is always fine, but just check it works
c = a**b
assert cython.typeof(c) == "soft double complex"
cdef double complex d = c
return d
def soft_complex_type_inference_1(double a, double b, pick):
"""
>>> soft_complex_type_inference_1(2, 1, False)
soft double complex 2.0
>>> soft_complex_type_inference_1(2, 3, True)
soft double complex 4.0
"""
# double and soft complex should infer to soft-complex
if pick:
c = a**2
else:
c = a**b
print(cython.typeof(c), c)
def soft_complex_type_inference_2(double a, double b, expected):
"""
>>> soft_complex_type_inference_2(2, 1, 1.0)
soft double complex
>>> soft_complex_type_inference_2(2, 3, 7.0)
soft double complex
"""
# double and soft complex should infer to soft-complex
c = a**b
c -= 1
print(cython.typeof(c))
delta = abs(c/expected - 1)
assert delta < 1e-15, delta
def pow_int_int(int a, int b):
"""
>>> pow_int_int(2, 2)
double 4.0
>>> pow_int_int(2, -2)
double 0.25
"""
c = a**b
print(cython.typeof(c), c)
@cython.cpow(True)
def pow_int_int_cpow(int a, int b):
"""
>>> pow_int_int_cpow(2, 2)
int 4
>>> pow_int_int_cpow(2, -2)
int 0
"""
c = a**b
print(cython.typeof(c), c)
cdef cfunc_taking_int(int x):
return x
def pow_int_int_coerced_directly(int a, int b):
"""
Generates two warnings about using cpow.
The actual behaviour isn't too easy to distinguish
without inspecting the c code though.
>>> pow_int_int_coerced_directly(2, 2)
8
"""
cdef int c = a**b
return cfunc_taking_int(a**b) + c
def pow_int_int_non_negative(int a, unsigned int b):
"""
A couple of combinations of non-negative values for the
exponent, which lets us fall back to int as a return type
>>> pow_int_int_non_negative(5, 3)
unsigned int
long
"""
c1 = a**b
c2 = a**5
print(cython.typeof(c1))
print(cython.typeof(c2))
ctypedef double f64
def pythagoras_with_typedef(double a, double b):
# see https://github.com/cython/cython/issues/5203
"""
>>> rc = pythagoras_with_typedef(2.0, 2.0)
>>> pyresult = 1.0 / (2 * 2.0 ** 2) ** 0.5
>>> pyresult - 0.001 < rc < pyresult + 0.001 or (rc, pyresult)
True
"""
cdef f64 result = a * a + b * b
result = 1.0 / result ** 0.5
return result
@cython.cpow(False)
def power_coercion_in_nogil_1(double a, double b):
"""
>>> power_coercion_in_nogil_1(2., 2.)
4.0
>>> power_coercion_in_nogil_1(-1., 0.5)
Traceback (most recent call last):
...
TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
"""
cdef double c
with nogil:
c = a**b
return c
cdef double nogil_fun(double x) nogil:
return x
def power_coercion_in_nogil_2(double a, double b):
"""
>>> power_coercion_in_nogil_2(2., 2.)
4.0
>>> power_coercion_in_nogil_2(-1., 0.5)
Traceback (most recent call last):
...
TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
"""
c = a**b
with nogil:
d = nogil_fun(c)
return d
def power_coercion_in_nogil_3(double a, double b, double c):
"""
>>> power_coercion_in_nogil_3(2., 2., 1.0)
0.25
>>> power_coercion_in_nogil_3(-1., 0.5, 1.0)
Traceback (most recent call last):
...
TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
"""
with nogil:
c /= a**b
return c
_WARNINGS = """
58:21: Treating '**' as if 'cython.cpow(True)' since it is directly assigned to a a non-complex C numeric type. This is likely to be fragile and we recommend setting 'cython.cpow' explicitly.
59:32: Treating '**' as if 'cython.cpow(True)' since it is directly assigned to a a non-complex C numeric type. This is likely to be fragile and we recommend setting 'cython.cpow' explicitly.
174:18: Treating '**' as if 'cython.cpow(True)' since it is directly assigned to a an integer C numeric type. This is likely to be fragile and we recommend setting 'cython.cpow' explicitly.
175:29: Treating '**' as if 'cython.cpow(True)' since it is directly assigned to a an integer C numeric type. This is likely to be fragile and we recommend setting 'cython.cpow' explicitly.
"""
|