cython

Форк
0
/
builtin_abs.pyx 
281 строка · 8.6 Кб
1
# mode: run
2
# ticket: t698
3
# distutils: extra_compile_args=-fwrapv
4

5
cdef extern from *:
6
    int INT_MAX
7
    long LONG_MAX
8

9
max_int = INT_MAX
10
max_long = LONG_MAX
11
max_long_long = (<object>2) ** (sizeof(long long) * 8 - 1) - 1
12

13

14
cimport cython
15

16
def abs_as_name():
17
    """
18
    >>> _abs = abs_as_name()
19
    >>> _abs(-5)
20
    5
21
    """
22
    x = abs
23
    return x
24

25
def py_abs(a):
26
    """
27
    >>> py_abs(-5)
28
    5
29
    >>> py_abs(-5.5)
30
    5.5
31
    """
32
    return abs(a)
33

34
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
35
                                "//ReturnStatNode//NameNode[@entry.cname = 'abs']")
36
def sub_abs(int a):
37
    """
38
    >>> sub_abs(5)
39
    (-5, 95)
40
    >>> sub_abs(105)
41
    (-105, -5)
42
    """
43
    return -abs(a), 100 - abs(a)
44

45
@cython.overflowcheck(True)
46
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
47
                                "//ReturnStatNode//NameNode[@entry.cname = 'abs']")
48
def int_abs(int a):
49
    """
50
    >>> int_abs(-5) == 5
51
    True
52
    >>> int_abs(-5.1) == 5
53
    True
54
    >>> int_abs(-max_int-1)     #doctest: +ELLIPSIS
55
    Traceback (most recent call last):
56
        ...
57
    OverflowError: ...
58
    >>> int_abs(max_int) == abs(max_int)         or (max_int, int_abs(max_int), abs(max_int))
59
    True
60
    """
61
    return abs(a)
62

63
@cython.overflowcheck(True)
64
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
65
                                "//ReturnStatNode//NameNode[@entry.cname = 'abs']")
66
cdef int c_int_abs(int a) except * nogil:
67
    return abs(a)
68

69
def test_c_int_abs(int a):
70
    """
71
    >>> test_c_int_abs(-5) == 5
72
    True
73
    >>> test_c_int_abs(-5.1) == 5
74
    True
75
    >>> test_c_int_abs(-max_int-1)     #doctest: +ELLIPSIS
76
    Traceback (most recent call last):
77
        ...
78
    OverflowError: ...
79
    >>> test_c_int_abs(max_int) == abs(max_int)  or (max_int, test_c_int_abs(max_int), abs(max_int))
80
    True
81
    """
82
    return c_int_abs(a)
83

84
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']")
85
@cython.test_fail_if_path_exists("//ReturnStatNode//NameNode[@entry.cname = 'abs']",
86
                                 "//ReturnStatNode//NameNode[@entry.cname = 'labs']")
87
def uint_abs(unsigned int a):
88
    """
89
    >>> uint_abs(max_int) == abs(max_int)         or (max_int, uint_abs(max_int), abs(max_int))
90
    True
91
    """
92
    return abs(a)
93

94
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']")
95
@cython.test_fail_if_path_exists("//ReturnStatNode//NameNode[@entry.cname = 'abs']",
96
                                 "//ReturnStatNode//NameNode[@entry.cname = 'labs']")
97
cdef unsigned int c_uint_abs(unsigned int a) nogil:
98
    return abs(a)
99

100
def test_c_uint_abs(unsigned int a):
101
    """
102
    >>> test_c_uint_abs(max_int) == abs(max_int)  or (max_int, test_c_uint_abs(max_int), abs(max_int))
103
    True
104
    """
105
    return c_uint_abs(a)
106

107
@cython.overflowcheck(True)
108
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
109
                                "//ReturnStatNode//NameNode[@entry.cname = 'labs']")
110
def long_abs(long a):
111
    """
112
    >>> long_abs(-5) == 5
113
    True
114
    >>> long_abs(-5.1) == 5
115
    True
116
    >>> long_abs(-max_long-1)     #doctest: +ELLIPSIS
117
    Traceback (most recent call last):
118
        ...
119
    OverflowError: ...
120
    >>> long_abs(max_long) == abs(max_long)         or (max_long, long_abs(max_long), abs(max_long))
121
    True
122
    """
123
    return abs(a)
124

125
@cython.overflowcheck(True)
126
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
127
                                "//ReturnStatNode//NameNode[@entry.cname = 'labs']")
128
cdef long c_long_abs(long a) except * nogil:
129
    return abs(a)
130

131
def test_c_long_abs(long a):
132
    """
133
    >>> test_c_long_abs(-5) == 5
134
    True
135
    >>> test_c_long_abs(-5.1) == 5
136
    True
137
    >>> test_c_long_abs(-max_long-1)     #doctest: +ELLIPSIS
138
    Traceback (most recent call last):
139
        ...
140
    OverflowError: ...
141
    >>> test_c_long_abs(max_long) == abs(max_long)  or (max_long, test_c_long_abs(max_long), abs(max_long))
142
    True
143
    """
144
    return c_long_abs(a)
145

146
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']")
147
@cython.test_fail_if_path_exists("//ReturnStatNode//NameNode[@entry.cname = 'abs']",
148
                                 "//ReturnStatNode//NameNode[@entry.cname = 'labs']")
149
def ulong_abs(unsigned long a):
150
    """
151
    >>> ulong_abs(max_long) == abs(max_long)         or (max_int, ulong_abs(max_long), abs(max_long))
152
    True
153
    >>> ulong_abs(max_long + 5) == abs(max_long + 5)         or (max_long + 5, ulong_abs(max_long + 5), abs(max_long + 5))
154
    True
155
    """
156
    return abs(a)
157

158
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']")
159
@cython.test_fail_if_path_exists("//ReturnStatNode//NameNode[@entry.cname = 'abs']",
160
                                 "//ReturnStatNode//NameNode[@entry.cname = 'labs']")
161
cdef unsigned long c_ulong_abs(unsigned long a) nogil:
162
    return abs(a)
163

164
def test_c_ulong_abs(unsigned long a):
165
    """
166
    >>> test_c_ulong_abs(max_long) == abs(max_long)  or (max_int, test_c_ulong_abs(max_long), abs(max_long))
167
    True
168
    >>> test_c_ulong_abs(max_long + 5) == abs(max_long + 5)  or (max_long + 5, test_c_ulong_abs(max_long + 5), abs(max_long + 5))
169
    True
170
    """
171
    return c_ulong_abs(a)
172

173
@cython.overflowcheck(True)
174
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
175
                                "//ReturnStatNode//NameNode[@entry.cname = '__Pyx_abs_longlong']")
176
def long_long_abs(long long a):
177
    """
178
    >>> long_long_abs(-(2**33)) == 2**33
179
    True
180
    >>> long_long_abs(-max_long_long-1)     #doctest: +ELLIPSIS
181
    Traceback (most recent call last):
182
        ...
183
    OverflowError: ...
184
    >>> long_long_abs(max_long_long) == abs(max_long_long)        or (max_long_long, long_long_abs(max_long_long), abs(max_long_long))
185
    True
186
    """
187
    return abs(a)
188

189
@cython.overflowcheck(True)
190
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
191
                                "//ReturnStatNode//NameNode[@entry.cname = '__Pyx_abs_longlong']")
192
cdef long long c_long_long_abs(long long a) except * nogil:
193
    return abs(a)
194

195
def test_c_long_long_abs(long long a):
196
    """
197
    >>> test_c_long_long_abs(-(2**33)) == 2**33
198
    True
199
    >>> test_c_long_long_abs(-max_long_long-1)     #doctest: +ELLIPSIS
200
    Traceback (most recent call last):
201
        ...
202
    OverflowError: ...
203
    >>> test_c_long_long_abs(max_long_long) == abs(max_long_long) or (max_long_long, test_c_long_long_abs(max_long_long), abs(max_long_long))
204
    True
205
    """
206
    return c_long_long_abs(a)
207

208
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
209
                                "//ReturnStatNode//NameNode[@entry.cname = 'fabs']")
210
def double_abs(double a):
211
    """
212
    >>> double_abs(-5)
213
    5.0
214
    >>> double_abs(-5.5)
215
    5.5
216
    """
217
    return abs(a)
218

219
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
220
                                "//ReturnStatNode//NameNode[@entry.cname = 'fabs']")
221
cdef double c_double_abs(double a) nogil:
222
    return abs(a)
223

224
def test_c_double_abs(double a):
225
    """
226
    >>> test_c_double_abs(-5)
227
    5.0
228
    >>> test_c_double_abs(-5.5)
229
    5.5
230
    """
231
    return c_double_abs(a)
232

233
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
234
                                "//ReturnStatNode//NameNode[@entry.cname = 'fabsf']")
235
def float_abs(float a):
236
    """
237
    >>> float_abs(-5)
238
    5.0
239
    >>> float_abs(-5.5)
240
    5.5
241
    """
242
    return abs(a)
243

244
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
245
                                "//ReturnStatNode//NameNode[@entry.cname = 'fabsf']")
246
cdef float c_float_abs(float a) nogil:
247
    return abs(a)
248

249
def test_c_float_abs(float a):
250
    """
251
    >>> test_c_float_abs(-5)
252
    5.0
253
    >>> test_c_float_abs(-5.5)
254
    5.5
255
    """
256
    return c_float_abs(a)
257

258
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
259
                                "//ReturnStatNode//NameNode[@entry.cname = '__Pyx_c_abs_double']")
260
def complex_abs(complex a):
261
    """
262
    >>> complex_abs(-5j)
263
    5.0
264
    >>> complex_abs(-5.5j)
265
    5.5
266
    """
267
    return abs(a)
268

269
@cython.test_assert_path_exists("//ReturnStatNode//NameNode[@entry.name = 'abs']",
270
                                "//ReturnStatNode//NameNode[@entry.cname = '__Pyx_c_abs_double']")
271
cdef double c_complex_abs(complex a) nogil:
272
    return abs(a)
273

274
def test_c_complex_abs(complex a):
275
    """
276
    >>> test_c_complex_abs(-5j)
277
    5.0
278
    >>> test_c_complex_abs(-5.5j)
279
    5.5
280
    """
281
    return c_complex_abs(a)
282

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.