1def f(obj2, obj3): 2 """ 3 >>> f(1.0, 2.95)[0] == f(1.0, 2.95)[1] 4 True 5 """ 6 cdef float flt1, flt2, flt3 7 flt2, flt3 = obj2, obj3 8 9 flt1 = flt2 ** flt3 10 obj1 = obj2 ** obj3 11 return flt1, obj1 12 13 14def g(i): 15 """ 16 >>> g(4) 17 1024 18 """ 19 return i ** 5 20 21 22def h(i): 23 """ 24 >>> h(4) 25 625 26 """ 27 return 5 ** i 28 29 30def constant_py(): 31 """ 32 >>> constant_py() == 2 ** 10 33 True 34 """ 35 result = (<object>2) ** 10 36 return result 37 38 39def constant_long(): 40 """ 41 >>> constant_long() == 2 ** 36 42 True 43 """ 44 result = (<object>2L) ** 36 45 return result 46 47 48def small_int_pow(long s): 49 """ 50 >>> small_int_pow(3) 51 (1, 3, 9, 27, 81) 52 >>> small_int_pow(-5) 53 (1, -5, 25, -125, 625) 54 """ 55 return s**0, s**1, s**2, s**3, s**4 56 57 58def int_pow(short a, short b): 59 """ 60 >>> int_pow(7, 2) 61 49 62 >>> int_pow(5, 3) 63 125 64 >>> int_pow(2, 10) 65 1024 66 """ 67 return a**b 68 69 70class I(int): 71 """ 72 Copied from CPython's test_descr.py 73 74 >>> I(2) ** I(3) 75 I(8) 76 >>> 2 ** I(3) 77 I(8) 78 >>> I(3).pow2() 79 I(8) 80 """ 81 def __repr__(self): 82 return 'I(%s)' % int(self) 83 def __pow__(self, other, mod=None): 84 if mod is None: 85 return I(pow(int(self), int(other))) 86 else: 87 return I(pow(int(self), int(other), int(mod))) 88 def __rpow__(self, other, mod=None): 89 if mod is None: 90 return I(pow(int(other), int(self), mod)) 91 else: 92 return I(pow(int(other), int(self), int(mod))) 93 94 def pow2(self): 95 return 2 ** self 96 97 98def optimised_pow2(n): 99 """ 100 >>> optimised_pow2(0) 101 1 102 >>> optimised_pow2(1) 103 2 104 >>> optimised_pow2(10) 105 1024 106 >>> optimised_pow2(30) 107 1073741824 108 >>> print(repr(optimised_pow2(31)).rstrip('L')) 109 2147483648 110 >>> print(repr(optimised_pow2(32)).rstrip('L')) 111 4294967296 112 >>> print(repr(optimised_pow2(60)).rstrip('L')) 113 1152921504606846976 114 >>> print(repr(optimised_pow2(63)).rstrip('L')) 115 9223372036854775808 116 >>> print(repr(optimised_pow2(64)).rstrip('L')) 117 18446744073709551616 118 >>> print(repr(optimised_pow2(100)).rstrip('L')) 119 1267650600228229401496703205376 120 >>> optimised_pow2(30000) == 2 ** 30000 121 True 122 >>> optimised_pow2(-1) 123 0.5 124 >>> optimised_pow2(0.5) == 2 ** 0.5 125 True 126 >>> optimised_pow2('test') 127 Traceback (most recent call last): 128 TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str' 129 """ 130 if isinstance(n, (int, long)) and 0 <= n < 1000: 131 assert isinstance(2.0 ** n, float), 'float %s' % n 132 assert isinstance(2 ** n, (int, long)), 'int %s' % n 133 return 2 ** n 134 135 136def optimised_pow2_inplace(n): 137 """ 138 >>> optimised_pow2_inplace(0) 139 1 140 >>> optimised_pow2_inplace(1) 141 2 142 >>> optimised_pow2_inplace(10) 143 1024 144 >>> optimised_pow2_inplace(30) 145 1073741824 146 >>> print(repr(optimised_pow2_inplace(32)).rstrip('L')) 147 4294967296 148 >>> print(repr(optimised_pow2_inplace(100)).rstrip('L')) 149 1267650600228229401496703205376 150 >>> optimised_pow2_inplace(30000) == 2 ** 30000 151 True 152 >>> optimised_pow2_inplace(-1) 153 0.5 154 >>> optimised_pow2_inplace(0.5) == 2 ** 0.5 155 True 156 >>> optimised_pow2_inplace('test') #doctest: +ELLIPSIS 157 Traceback (most recent call last): 158 TypeError: unsupported operand type(s) for ...: 'int' and 'str' 159 """ 160 x = 2 161 x **= n 162 return x 163