cython

Форк
0
/
powop.pyx 
177 строк · 3.5 Кб
1
cimport cython
2

3
def f(obj2, obj3):
4
    """
5
    >>> f(1.0, 2.95)[0] == f(1.0, 2.95)[1]
6
    True
7
    """
8
    cdef float flt1, flt2, flt3
9
    flt2, flt3 = obj2, obj3
10

11
    flt1 = flt2 ** flt3
12
    obj1 = obj2 ** obj3
13
    return flt1, obj1
14

15

16
def g(i):
17
    """
18
    >>> g(4)
19
    1024
20
    """
21
    return i ** 5
22

23

24
def h(i):
25
    """
26
    >>> h(4)
27
    625
28
    """
29
    return 5 ** i
30

31

32
def constant_py():
33
    """
34
    >>> constant_py() == 2 ** 10
35
    True
36
    """
37
    result = (<object>2) ** 10
38
    return result
39

40

41
def constant_long():
42
    """
43
    >>> constant_long() == 2 ** 36
44
    True
45
    """
46
    result = (<object>2L) ** 36
47
    return result
48

49

50
def small_int_pow(long s):
51
    """
52
    >>> small_int_pow(3)
53
    (1, 3, 9, 27, 81)
54
    >>> small_int_pow(-5)
55
    (1, -5, 25, -125, 625)
56
    """
57
    return s**0, s**1, s**2, s**3, s**4
58

59

60
@cython.cpow(True)
61
def int_pow_cpow(short a, short b):
62
    """
63
    >>> int_pow_cpow(7, 2)
64
    49
65
    >>> int_pow_cpow(5, 3)
66
    125
67
    >>> int_pow_cpow(2, 10)
68
    1024
69
    """
70
    return a**b
71

72
@cython.cpow(False)
73
def int_pow(short a, short b):
74
    """
75
    >>> int_pow(7, 2)
76
    49.0
77
    >>> int_pow(5, 3)
78
    125.0
79
    >>> int_pow(2, 10)
80
    1024.0
81
    """
82
    return a**b
83

84

85
class I(int):
86
    """
87
    Copied from CPython's test_descr.py
88

89
    >>> I(2) ** I(3)
90
    I(8)
91
    >>> 2 ** I(3)
92
    I(8)
93
    >>> I(3).pow2()
94
    I(8)
95
    """
96
    def __repr__(self):
97
        return 'I(%s)' % int(self)
98
    def __pow__(self, other, mod=None):
99
        if mod is None:
100
            return I(pow(int(self), int(other)))
101
        else:
102
            return I(pow(int(self), int(other), int(mod)))
103
    def __rpow__(self, other, mod=None):
104
        if mod is None:
105
            return I(pow(int(other), int(self), mod))
106
        else:
107
            return I(pow(int(other), int(self), int(mod)))
108

109
    def pow2(self):
110
        return 2 ** self
111

112

113
def optimised_pow2(n):
114
    """
115
    >>> optimised_pow2(0)
116
    1
117
    >>> optimised_pow2(1)
118
    2
119
    >>> optimised_pow2(10)
120
    1024
121
    >>> optimised_pow2(30)
122
    1073741824
123
    >>> print(repr(optimised_pow2(31)).rstrip('L'))
124
    2147483648
125
    >>> print(repr(optimised_pow2(32)).rstrip('L'))
126
    4294967296
127
    >>> print(repr(optimised_pow2(60)).rstrip('L'))
128
    1152921504606846976
129
    >>> print(repr(optimised_pow2(63)).rstrip('L'))
130
    9223372036854775808
131
    >>> print(repr(optimised_pow2(64)).rstrip('L'))
132
    18446744073709551616
133
    >>> print(repr(optimised_pow2(100)).rstrip('L'))
134
    1267650600228229401496703205376
135
    >>> optimised_pow2(30000) == 2 ** 30000
136
    True
137
    >>> optimised_pow2(-1)
138
    0.5
139
    >>> optimised_pow2(0.5) == 2 ** 0.5
140
    True
141
    >>> optimised_pow2('test') # doctest: +ELLIPSIS
142
    Traceback (most recent call last):
143
    TypeError: ...operand... **...
144
    """
145
    if isinstance(n, int) and 0 <= n < 1000:
146
        assert isinstance(2.0 ** n, float), 'float %s' % n
147
        assert isinstance(2 ** n, int), 'int %s' % n
148
    return 2 ** n
149

150

151
def optimised_pow2_inplace(n):
152
    """
153
    >>> optimised_pow2_inplace(0)
154
    1
155
    >>> optimised_pow2_inplace(1)
156
    2
157
    >>> optimised_pow2_inplace(10)
158
    1024
159
    >>> optimised_pow2_inplace(30)
160
    1073741824
161
    >>> print(repr(optimised_pow2_inplace(32)).rstrip('L'))
162
    4294967296
163
    >>> print(repr(optimised_pow2_inplace(100)).rstrip('L'))
164
    1267650600228229401496703205376
165
    >>> optimised_pow2_inplace(30000) == 2 ** 30000
166
    True
167
    >>> optimised_pow2_inplace(-1)
168
    0.5
169
    >>> optimised_pow2_inplace(0.5) == 2 ** 0.5
170
    True
171
    >>> optimised_pow2_inplace('test') # doctest: +ELLIPSIS
172
    Traceback (most recent call last):
173
    TypeError: ...operand... **...
174
    """
175
    x = 2
176
    x **= n
177
    return x
178

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

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

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

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